Skip to content

Commit ec83061

Browse files
René Scharfegitster
authored andcommitted
grep: stop leaking line strings with -f
When reading patterns from a file, we pass the lines as allocated string buffers to append_grep_pat() and never free them. That's not a problem because they are needed until the program ends anyway. However, now that the function duplicates the pattern string, we can reuse the strbuf after calling that function. This simplifies the code a bit and plugs a minor memory leak. Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 526a858 commit ec83061

File tree

1 file changed

+2
-5
lines changed

1 file changed

+2
-5
lines changed

builtin/grep.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -681,15 +681,12 @@ static int file_callback(const struct option *opt, const char *arg, int unset)
681681
if (!patterns)
682682
die_errno(_("cannot open '%s'"), arg);
683683
while (strbuf_getline(&sb, patterns, '\n') == 0) {
684-
char *s;
685-
size_t len;
686-
687684
/* ignore empty line like grep does */
688685
if (sb.len == 0)
689686
continue;
690687

691-
s = strbuf_detach(&sb, &len);
692-
append_grep_pat(grep_opt, s, len, arg, ++lno, GREP_PATTERN);
688+
append_grep_pat(grep_opt, sb.buf, sb.len, arg, ++lno,
689+
GREP_PATTERN);
693690
}
694691
if (!from_stdin)
695692
fclose(patterns);

0 commit comments

Comments
 (0)