Skip to content

Commit 0217947

Browse files
committed
fixup??? grep: make PCRE2 aware of custom allocator
It is _not_ nedmalloc-specific! Never was, never will. As soon as you use e.g. the LD_PRELOAD trick to use jemalloc, the very same issues will arise as I reported earlier: a block that was allocated via PCRE2 wants to be `free()`d via the custom allocator, which will cause serious issues unless we make sure that PCRE2 uses the same allocator as Git. Besides, the location where `pcre2_global_context` was initialized is completely wrong. That code path was not even close to being hit in t7816.48, however, that test case really needs that context. Let's stop trying to be smart (and failing at it), and instead just initialize the global context in `grep_init()` and be done with these problems once and for all. The chase stopped being fun a month ago. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 9268ab1 commit 0217947

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

grep.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ static struct grep_opt grep_defaults;
1919
#ifdef USE_LIBPCRE2
2020
static pcre2_general_context *pcre2_global_context;
2121

22-
#ifdef USE_NED_ALLOCATOR
2322
static void *pcre2_malloc(PCRE2_SIZE size, MAYBE_UNUSED void *memory_data)
2423
{
2524
return malloc(size);
@@ -30,7 +29,6 @@ static void pcre2_free(void *pointer, MAYBE_UNUSED void *memory_data)
3029
return free(pointer);
3130
}
3231
#endif
33-
#endif
3432

3533
static const char *color_grep_slots[] = {
3634
[GREP_COLOR_CONTEXT] = "context",
@@ -176,6 +174,12 @@ void grep_init(struct grep_opt *opt, struct repository *repo, const char *prefix
176174
struct grep_opt *def = &grep_defaults;
177175
int i;
178176

177+
#if defined(USE_LIBPCRE2)
178+
if (!pcre2_global_context)
179+
pcre2_global_context = pcre2_general_context_create(
180+
pcre2_malloc, pcre2_free, NULL);
181+
#endif
182+
179183
#ifdef USE_NED_ALLOCATOR
180184
#ifdef USE_LIBPCRE1
181185
pcre_malloc = malloc;
@@ -343,11 +347,6 @@ void append_header_grep_pattern(struct grep_opt *opt,
343347
void append_grep_pattern(struct grep_opt *opt, const char *pat,
344348
const char *origin, int no, enum grep_pat_token t)
345349
{
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
351350
append_grep_pat(opt, pat, strlen(pat), origin, no, t);
352351
}
353352

0 commit comments

Comments
 (0)