@matheusbsbs / matheus-bagliano / commits / 10da030ab7
Fork 0

grep: avoid leak of chartables in PCRE2

94da9193a6 ("grep: add support for PCRE v2", 2017-06-01) introduced a small memory leak visible with valgrind in t7813. Complete the creation of a PCRE2 specific variable that was missing from the original change and free the generated table just like it is done for PCRE1. Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Carlo Marcelo Arenas Belón committed Oct 16, 2019 at 12:10 UTC 10da030ab757c38a01507bf18e2484698000b791
2 files changed +5 -3
grep.c
+4 -3
@@ -525,7 +525,6 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
525 PCRE2_UCHAR errbuf[256];
526 PCRE2_SIZE erroffset;
527 int options = PCRE2_MULTILINE;
528 - const uint8_t *character_tables = NULL;
528 int jitret;
529 int patinforet;
530 size_t jitsizearg;
@@ -539,9 +538,10 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
538 if (has_non_ascii(p->pattern)) {
539 if (!pcre2_global_context)
540 BUG("pcre2_global_context uninitialized");
542 - character_tables = pcre2_maketables(pcre2_global_context);
541 + p->pcre2_tables = pcre2_maketables(pcre2_global_context);
542 p->pcre2_compile_context = pcre2_compile_context_create(NULL);
544 - pcre2_set_character_tables(p->pcre2_compile_context, character_tables);
543 + pcre2_set_character_tables(p->pcre2_compile_context,
544 + p->pcre2_tables);
545 }
546 options |= PCRE2_CASELESS;
547 }
@@ -645,6 +645,7 @@ static void free_pcre2_pattern(struct grep_pat *p)
645 pcre2_match_data_free(p->pcre2_match_data);
646 pcre2_jit_stack_free(p->pcre2_jit_stack);
647 pcre2_match_context_free(p->pcre2_match_context);
648 + free((void *)p->pcre2_tables);
649 }
650 #else /* !USE_LIBPCRE2 */
651 static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt)
grep.h
+1
@@ -96,6 +96,7 @@ struct grep_pat {
96 pcre2_compile_context *pcre2_compile_context;
97 pcre2_match_context *pcre2_match_context;
98 pcre2_jit_stack *pcre2_jit_stack;
99 + const uint8_t *pcre2_tables;
100 uint32_t pcre2_jit_on;
101 kwset_t kws;
102 unsigned fixed:1;