Skip to content

Commit 7d3bf76

Browse files
carenasgitster
authored andcommitted
grep: avoid leak of chartables in PCRE2
94da919 ("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. The table cleanup use free as there is no global context defined when it was created (pcre2_maketables is passed a NULL pointer) but if that gets ever changed will need to be updated in tandem. Signed-off-by: Carlo Marcelo Arenas Belón <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 51cf315 commit 7d3bf76

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

grep.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,6 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
488488
PCRE2_UCHAR errbuf[256];
489489
PCRE2_SIZE erroffset;
490490
int options = PCRE2_MULTILINE;
491-
const uint8_t *character_tables = NULL;
492491
int jitret;
493492
int patinforet;
494493
size_t jitsizearg;
@@ -499,9 +498,10 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
499498

500499
if (opt->ignore_case) {
501500
if (has_non_ascii(p->pattern)) {
502-
character_tables = pcre2_maketables(NULL);
501+
p->pcre2_tables = pcre2_maketables(NULL);
503502
p->pcre2_compile_context = pcre2_compile_context_create(NULL);
504-
pcre2_set_character_tables(p->pcre2_compile_context, character_tables);
503+
pcre2_set_character_tables(p->pcre2_compile_context,
504+
p->pcre2_tables);
505505
}
506506
options |= PCRE2_CASELESS;
507507
}
@@ -605,6 +605,7 @@ static void free_pcre2_pattern(struct grep_pat *p)
605605
pcre2_match_data_free(p->pcre2_match_data);
606606
pcre2_jit_stack_free(p->pcre2_jit_stack);
607607
pcre2_match_context_free(p->pcre2_match_context);
608+
free((void *)p->pcre2_tables);
608609
}
609610
#else /* !USE_LIBPCRE2 */
610611
static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt)

grep.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ struct grep_pat {
9696
pcre2_compile_context *pcre2_compile_context;
9797
pcre2_match_context *pcre2_match_context;
9898
pcre2_jit_stack *pcre2_jit_stack;
99+
const uint8_t *pcre2_tables;
99100
uint32_t pcre2_jit_on;
100101
kwset_t kws;
101102
unsigned fixed:1;

0 commit comments

Comments
 (0)