Skip to content

Commit

Permalink
grep: stop leaking line strings with -f
Browse files Browse the repository at this point in the history
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 <rene.scharfe@lsrfire.ath.cx>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
René Scharfe authored and Junio C Hamano committed May 21, 2012
1 parent 526a858 commit ec83061
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions builtin/grep.c
Original file line number Diff line number Diff line change
Expand Up @@ -681,15 +681,12 @@ static int file_callback(const struct option *opt, const char *arg, int unset)
if (!patterns)
die_errno(_("cannot open '%s'"), arg);
while (strbuf_getline(&sb, patterns, '\n') == 0) {
char *s;
size_t len;

/* ignore empty line like grep does */
if (sb.len == 0)
continue;

s = strbuf_detach(&sb, &len);
append_grep_pat(grep_opt, s, len, arg, ++lno, GREP_PATTERN);
append_grep_pat(grep_opt, sb.buf, sb.len, arg, ++lno,
GREP_PATTERN);
}
if (!from_stdin)
fclose(patterns);
Expand Down

0 comments on commit ec83061

Please sign in to comment.