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 <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
May 21, 2012 at 18:10 UTC
ec83061156e18ce600384d3c57f90729a9295488
1 file changed
+2
-5
builtin/grep.c
+2
-5
@@ -681,15 +681,12 @@ static int file_callback(const struct option *opt, const char *arg, int unset)
681
if (!patterns)
682
die_errno(_("cannot open '%s'"), arg);
683
while (strbuf_getline(&sb, patterns, '\n') == 0) {
684
- char *s;
685
- size_t len;
686
-
684
/* ignore empty line like grep does */
685
if (sb.len == 0)
686
continue;
687
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);
690
}
691
if (!from_stdin)
692
fclose(patterns);