Skip to content

Fix a problem with PCRE2 and nedmalloc, found via Azure Pipelines #306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions grep.c
Original file line number Diff line number Diff line change
Expand Up @@ -482,13 +482,33 @@ static void free_pcre1_regexp(struct grep_pat *p)
#endif /* !USE_LIBPCRE1 */

#ifdef USE_LIBPCRE2
static void *pcre2_malloc(PCRE2_SIZE size, void *memory_data)
{
return malloc(size);
}

static void pcre2_free(void *pointer, void *memory_data)
{
return free(pointer);
}

static pcre2_general_context *get_pcre2_context(void)
{
static pcre2_general_context *context;

if (!context)
context = pcre2_general_context_create(pcre2_malloc,
pcre2_free, NULL);

return context;
}

static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt)
{
int error;
PCRE2_UCHAR errbuf[256];
PCRE2_SIZE erroffset;
int options = PCRE2_MULTILINE;
const uint8_t *character_tables = NULL;
int jitret;
int patinforet;
size_t jitsizearg;
Expand All @@ -499,9 +519,11 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt

if (opt->ignore_case) {
if (has_non_ascii(p->pattern)) {
character_tables = pcre2_maketables(NULL);
p->pcre2_compile_context = pcre2_compile_context_create(NULL);
pcre2_set_character_tables(p->pcre2_compile_context, character_tables);
p->pcre2_tables = pcre2_maketables(get_pcre2_context());
p->pcre2_compile_context =
pcre2_compile_context_create(get_pcre2_context());
pcre2_set_character_tables(p->pcre2_compile_context,
p->pcre2_tables);
}
options |= PCRE2_CASELESS;
}
Expand All @@ -513,7 +535,8 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
p->pcre2_compile_context);

if (p->pcre2_pattern) {
p->pcre2_match_data = pcre2_match_data_create_from_pattern(p->pcre2_pattern, NULL);
p->pcre2_match_data =
pcre2_match_data_create_from_pattern(p->pcre2_pattern, get_pcre2_context());
if (!p->pcre2_match_data)
die("Couldn't allocate PCRE2 match data");
} else {
Expand Down Expand Up @@ -550,7 +573,7 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
return;
}

p->pcre2_jit_stack = pcre2_jit_stack_create(1, 1024 * 1024, NULL);
p->pcre2_jit_stack = pcre2_jit_stack_create(1, 1024 * 1024, get_pcre2_context());
if (!p->pcre2_jit_stack)
die("Couldn't allocate PCRE2 JIT stack");
p->pcre2_match_context = pcre2_match_context_create(NULL);
Expand Down Expand Up @@ -605,6 +628,7 @@ static void free_pcre2_pattern(struct grep_pat *p)
pcre2_match_data_free(p->pcre2_match_data);
pcre2_jit_stack_free(p->pcre2_jit_stack);
pcre2_match_context_free(p->pcre2_match_context);
free((void *)p->pcre2_tables);
}
#else /* !USE_LIBPCRE2 */
static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt)
Expand Down
1 change: 1 addition & 0 deletions grep.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ struct grep_pat {
pcre2_compile_context *pcre2_compile_context;
pcre2_match_context *pcre2_match_context;
pcre2_jit_stack *pcre2_jit_stack;
const uint8_t *pcre2_tables;
uint32_t pcre2_jit_on;
kwset_t kws;
unsigned fixed:1;
Expand Down