Skip to content

Commit 513f2b0

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 custom allocators (e.g. nedmalloc). This problem became obvious when we tried to plug a memory leak by `free()`ing a data structure allocated by PCRE2, triggering a segfault in Windows (where we use nedmalloc by default). 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.c` 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. 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 custom allocators 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: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 57d4660 commit 513f2b0

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-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: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ 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+
static void *pcre2_malloc(PCRE2_SIZE size, MAYBE_UNUSED void *memory_data)
23+
{
24+
return malloc(size);
25+
}
26+
27+
static void pcre2_free(void *pointer, MAYBE_UNUSED void *memory_data)
28+
{
29+
return free(pointer);
30+
}
31+
#endif
32+
1933
static const char *color_grep_slots[] = {
2034
[GREP_COLOR_CONTEXT] = "context",
2135
[GREP_COLOR_FILENAME] = "filename",
@@ -153,12 +167,20 @@ int grep_config(const char *var, const char *value, void *cb)
153167
*
154168
* If using PCRE, make sure that the library is configured
155169
* to use the same allocator as Git (e.g. nedmalloc on Windows).
170+
*
171+
* Any allocated memory needs to be released in grep_destroy().
156172
*/
157173
void grep_init(struct grep_opt *opt, struct repository *repo, const char *prefix)
158174
{
159175
struct grep_opt *def = &grep_defaults;
160176
int i;
161177

178+
#if defined(USE_LIBPCRE2)
179+
if (!pcre2_global_context)
180+
pcre2_global_context = pcre2_general_context_create(
181+
pcre2_malloc, pcre2_free, NULL);
182+
#endif
183+
162184
#ifdef USE_LIBPCRE1
163185
pcre_malloc = malloc;
164186
pcre_free = free;
@@ -186,6 +208,13 @@ void grep_init(struct grep_opt *opt, struct repository *repo, const char *prefix
186208
color_set(opt->colors[i], def->colors[i]);
187209
}
188210

211+
void grep_destroy(void)
212+
{
213+
#ifdef USE_LIBPCRE2
214+
pcre2_general_context_free(pcre2_global_context);
215+
#endif
216+
}
217+
189218
static void grep_set_pattern_type_option(enum grep_pattern_type pattern_type, struct grep_opt *opt)
190219
{
191220
/*
@@ -505,9 +534,12 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
505534

506535
p->pcre2_compile_context = NULL;
507536

537+
/* pcre2_global_context is initialized in append_grep_pattern */
508538
if (opt->ignore_case) {
509539
if (has_non_ascii(p->pattern)) {
510-
character_tables = pcre2_maketables(NULL);
540+
if (!pcre2_global_context)
541+
BUG("pcre2_global_context uninitialized");
542+
character_tables = pcre2_maketables(pcre2_global_context);
511543
p->pcre2_compile_context = pcre2_compile_context_create(NULL);
512544
pcre2_set_character_tables(p->pcre2_compile_context, character_tables);
513545
}

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)