Skip to content

Commit cf8d36f

Browse files
carenasgitster
authored andcommitted
grep: make PCRE2 aware of custom allocator
94da919 (grep: add support for PCRE v2, 2017-06-01) didn't include a way to override the system allocator, and so it is incompatible with USE_NED_ALLOCATOR. The problem was made visible when an attempt to avoid a leak in a data structure that is created by the library was passed to NED's free for disposal triggering a segfault in Windows. PCRE2 requires the use of a general context to override the allocator and therefore, there is a lot more code needed than in PCRE1, including a couple of wrapper functions. Extend the grep API with a "destructor" that could be called to cleanup any objects that were created and used globally. Update builtin/grep to use that new API, but any other future users should make sure to have matching grep_init/grep_destroy calls if they are using the pattern matching functionality (currently only relevant when using both NED and PCRE2) Move some of the logic that was before done per thread (in the workers) into an earlier phase to avoid degrading performance, but as the use of PCRE2 with NED is better understood it is expected more of its functions will be instructed to use the custom allocator as well as was done in the original code[1] this work was based on. [1] https://public-inbox.org/git/3397e6797f872aedd18c6d795f4976e1c579514b.1565005867.git.gitgitgadget@gmail.com/ Reported-by: Johannes Schindelin <[email protected]> Signed-off-by: Carlo Marcelo Arenas Belón <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 770584d commit cf8d36f

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

builtin/grep.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,5 +1145,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
11451145
run_pager(&opt, prefix);
11461146
clear_pathspec(&pathspec);
11471147
free_grep_patterns(&opt);
1148+
grep_destroy();
11481149
return !hit;
11491150
}

grep.c

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ static int grep_source_is_binary(struct grep_source *gs,
1616

1717
static struct grep_opt grep_defaults;
1818

19+
#ifdef USE_LIBPCRE2
20+
static pcre2_general_context *pcre2_global_context;
21+
22+
#ifdef USE_NED_ALLOCATOR
23+
static void *pcre2_malloc(PCRE2_SIZE size, MAYBE_UNUSED void *memory_data)
24+
{
25+
return malloc(size);
26+
}
27+
28+
static void pcre2_free(void *pointer, MAYBE_UNUSED void *memory_data)
29+
{
30+
return free(pointer);
31+
}
32+
#endif
33+
#endif
34+
1935
static const char *color_grep_slots[] = {
2036
[GREP_COLOR_CONTEXT] = "context",
2137
[GREP_COLOR_FILENAME] = "filename",
@@ -153,6 +169,7 @@ int grep_config(const char *var, const char *value, void *cb)
153169
*
154170
* If using PCRE make sure that the library is configured
155171
* to use the right allocator (ex: NED)
172+
* if any object is created it should be cleaned up in grep_destroy()
156173
*/
157174
void grep_init(struct grep_opt *opt, struct repository *repo, const char *prefix)
158175
{
@@ -188,6 +205,13 @@ void grep_init(struct grep_opt *opt, struct repository *repo, const char *prefix
188205
color_set(opt->colors[i], def->colors[i]);
189206
}
190207

208+
void grep_destroy(void)
209+
{
210+
#ifdef USE_LIBPCRE2
211+
pcre2_general_context_free(pcre2_global_context);
212+
#endif
213+
}
214+
191215
static void grep_set_pattern_type_option(enum grep_pattern_type pattern_type, struct grep_opt *opt)
192216
{
193217
/*
@@ -319,6 +343,11 @@ void append_header_grep_pattern(struct grep_opt *opt,
319343
void append_grep_pattern(struct grep_opt *opt, const char *pat,
320344
const char *origin, int no, enum grep_pat_token t)
321345
{
346+
#if defined(USE_LIBPCRE2) && defined(USE_NED_ALLOCATOR)
347+
if (!pcre2_global_context && opt->ignore_case && has_non_ascii(pat))
348+
pcre2_global_context = pcre2_general_context_create(
349+
pcre2_malloc, pcre2_free, NULL);
350+
#endif
322351
append_grep_pat(opt, pat, strlen(pat), origin, no, t);
323352
}
324353

@@ -507,9 +536,14 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
507536

508537
p->pcre2_compile_context = NULL;
509538

539+
/* pcre2_global_context is initialized in append_grep_pattern */
510540
if (opt->ignore_case) {
511541
if (has_non_ascii(p->pattern)) {
512-
character_tables = pcre2_maketables(NULL);
542+
#ifdef USE_NED_ALLOCATOR
543+
if (!pcre2_global_context)
544+
BUG("pcre2_global_context uninitialized");
545+
#endif
546+
character_tables = pcre2_maketables(pcre2_global_context);
513547
p->pcre2_compile_context = pcre2_compile_context_create(NULL);
514548
pcre2_set_character_tables(p->pcre2_compile_context, character_tables);
515549
}

grep.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ struct grep_opt {
189189
void init_grep_defaults(struct repository *);
190190
int grep_config(const char *var, const char *value, void *);
191191
void grep_init(struct grep_opt *, struct repository *repo, const char *prefix);
192+
void grep_destroy(void);
192193
void grep_commit_pattern_type(enum grep_pattern_type, struct grep_opt *opt);
193194

194195
void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t);

0 commit comments

Comments
 (0)