Skip to content

Commit

Permalink
fixup??? grep: make PCRE2 aware of custom allocator
Browse files Browse the repository at this point in the history
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 <johannes.schindelin@gmx.de>
  • Loading branch information
dscho authored and Git for Windows Build Agent committed Oct 3, 2019
1 parent e23f2de commit 719beb8
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions grep.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ static struct grep_opt grep_defaults;
#ifdef USE_LIBPCRE2
static pcre2_general_context *pcre2_global_context;

#ifdef USE_NED_ALLOCATOR
static void *pcre2_malloc(PCRE2_SIZE size, MAYBE_UNUSED void *memory_data)
{
return malloc(size);
Expand All @@ -30,7 +29,6 @@ static void pcre2_free(void *pointer, MAYBE_UNUSED void *memory_data)
return free(pointer);
}
#endif
#endif

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

#if defined(USE_LIBPCRE2)
if (!pcre2_global_context)
pcre2_global_context = pcre2_general_context_create(
pcre2_malloc, pcre2_free, NULL);
#endif

#ifdef USE_NED_ALLOCATOR
#ifdef USE_LIBPCRE1
pcre_malloc = malloc;
Expand Down Expand Up @@ -343,11 +347,6 @@ void append_header_grep_pattern(struct grep_opt *opt,
void append_grep_pattern(struct grep_opt *opt, const char *pat,
const char *origin, int no, enum grep_pat_token t)
{
#if defined(USE_LIBPCRE2) && defined(USE_NED_ALLOCATOR)
if (!pcre2_global_context && opt->ignore_case && has_non_ascii(pat))
pcre2_global_context = pcre2_general_context_create(
pcre2_malloc, pcre2_free, NULL);
#endif
append_grep_pat(opt, pat, strlen(pat), origin, no, t);
}

Expand Down

0 comments on commit 719beb8

Please sign in to comment.