Skip to content

Commit 54aef14

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. Signed-off-by: Carlo Marcelo Arenas Belón <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cf8d36f commit 54aef14

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
@@ -527,7 +527,6 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
527527
PCRE2_UCHAR errbuf[256];
528528
PCRE2_SIZE erroffset;
529529
int options = PCRE2_MULTILINE;
530-
const uint8_t *character_tables = NULL;
531530
int jitret;
532531
int patinforet;
533532
size_t jitsizearg;
@@ -543,9 +542,10 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
543542
if (!pcre2_global_context)
544543
BUG("pcre2_global_context uninitialized");
545544
#endif
546-
character_tables = pcre2_maketables(pcre2_global_context);
545+
p->pcre2_tables = pcre2_maketables(pcre2_global_context);
547546
p->pcre2_compile_context = pcre2_compile_context_create(NULL);
548-
pcre2_set_character_tables(p->pcre2_compile_context, character_tables);
547+
pcre2_set_character_tables(p->pcre2_compile_context,
548+
p->pcre2_tables);
549549
}
550550
options |= PCRE2_CASELESS;
551551
}
@@ -649,6 +649,7 @@ static void free_pcre2_pattern(struct grep_pat *p)
649649
pcre2_match_data_free(p->pcre2_match_data);
650650
pcre2_jit_stack_free(p->pcre2_jit_stack);
651651
pcre2_match_context_free(p->pcre2_match_context);
652+
free((void *)p->pcre2_tables);
652653
}
653654
#else /* !USE_LIBPCRE2 */
654655
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)