From: Mikhail Pchelin Subject: fix for memory leak in read_ignores() To: gameoftrees@openbsd.org Date: Sun, 26 Jul 2026 16:08:13 +0300 While working on other patch I came across a memory leak in read_ignores(). When pattern in ignore file is repeated, RB_INSERT() will return pointer to already existing element, not inserting new one, in this case we leak the pattern pointer. Inlined patch free()s the variable on error (previously this wasn't handled, 'goto done;' works only for successfully inserted elements, we don't introduce double-free here) and in case of duplicate RB_INSERT() entry, later is signaled by got_pathlist_insert() returning NULL via the first argument. diff b3611acb7c736d2f45369e1eea2806450e9d11b2 95b1ad92ba77b368821e7c63730e5db889364fb6 commit - b3611acb7c736d2f45369e1eea2806450e9d11b2 commit + 95b1ad92ba77b368821e7c63730e5db889364fb6 blob - fca04b3b62b7ecd7035e827c334fa35ad977b7a3 blob + ae9b26c3bfc40490366064a1c54511804fe030d9 --- lib/worktree.c +++ lib/worktree.c @@ -3794,7 +3794,7 @@ static const struct got_error * read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f) { const struct got_error *err = NULL; - struct got_pathlist_entry *pe = NULL; + struct got_pathlist_entry *pe = NULL, *ipe = NULL; struct got_pathlist_head *ignorelist; char *line = NULL, *pattern, *dirpath = NULL; size_t linesize = 0; @@ -3826,7 +3826,13 @@ read_ignores(struct got_pathlist_head *ignores, const err = got_error_from_errno("asprintf"); goto done; } - err = got_pathlist_insert(NULL, ignorelist, pattern, NULL); + err = got_pathlist_insert(&ipe, ignorelist, pattern, NULL); + /* + * got_pathlist_insert() silently rejects duplicated pattern; + * free it. + */ + if (err || ipe == NULL) + free(pattern); if (err) goto done; }