Skip to content

Commit

Permalink
Fix pcre memory leak (#262)
Browse files Browse the repository at this point in the history
* Fix memory leak

The context is not needed as the default depth limit is fine.

Since pcre 10.30, pcre2_match no longer uses recursive function calls,
so this setting should no longer be needed.

* Clean up
  • Loading branch information
tobil4sk authored Mar 26, 2023
1 parent adc9215 commit 84a45ee
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions libs/regexp/regexp.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ DEFINE_KIND(k_regexp);

static field id_pos;
static field id_len;
static pcre2_match_context *match_context;

/**
<doc>
Expand All @@ -64,8 +63,8 @@ static void free_regexp( value p ) {
pcre2_match_data_free( PCRE(p)->match_data );
}

static int do_exec( pcredata *d, const char *str, int len, int pos ) {
int res = pcre2_match(d->regex,str,len,pos,0,d->match_data,match_context);
static int do_match( pcredata *d, const char *str, int len, int pos ) {
int res = pcre2_match(d->regex,str,len,pos,0,d->match_data,NULL);
if( res >= 0 )
return 1;
d->str = val_null; // empty string prevents trying to access the data after a failed match
Expand Down Expand Up @@ -134,7 +133,7 @@ static value regexp_new_options( value s, value opt ) {
pdata->regex = p;
pdata->str = val_null;
pdata->n_groups = 0;
pcre2_pattern_info(p,PCRE2_INFO_CAPTURECOUNT,&pdata->n_groups);
pcre2_pattern_info(pdata->regex,PCRE2_INFO_CAPTURECOUNT,&pdata->n_groups);
pdata->n_groups++;
pdata->match_data = pcre2_match_data_create_from_pattern(pdata->regex,NULL);
val_gc(v,free_regexp);
Expand Down Expand Up @@ -167,7 +166,7 @@ static value regexp_match( value o, value s, value p, value len ) {
if( pp < 0 || ll < 0 || pp > val_strlen(s) || pp + ll > val_strlen(s) )
neko_error();
d = PCRE(o);
if( do_exec(d,val_string(s),ll+pp,pp) ) {
if( do_match(d,val_string(s),ll+pp,pp) ) {
d->str = s;
return val_true;
} else {
Expand All @@ -190,7 +189,7 @@ static value do_replace( value o, value s, value s2, bool all ) {
int pos = 0;
size_t *matches;
// TODO: Consider pcre2_substitute()
while( do_exec(d,str,len,pos) ) {
while( do_match(d,str,len,pos) ) {
matches = pcre2_get_ovector_pointer(d->match_data);
buffer_append_sub(b,str+pos,matches[0] - pos);
buffer_append_sub(b,str2,len2);
Expand Down Expand Up @@ -236,7 +235,7 @@ static value regexp_replace_fun( value o, value s, value f ) {
const char *str = val_string(s);
size_t *matches;
d->str = s;
while( do_exec(d,str,len,pos) ) {
while( do_match(d,str,len,pos) ) {
matches = pcre2_get_ovector_pointer(d->match_data);
buffer_append_sub(b,str+pos,matches[0] - pos);
val_buffer(b,val_call1(f,o));
Expand Down Expand Up @@ -303,9 +302,6 @@ static value regexp_matched_pos( value o, value n ) {
void regexp_main() {
id_pos = val_id("pos");
id_len = val_id("len");

match_context = pcre2_match_context_create(NULL);
pcre2_set_depth_limit(match_context,3500); // adapted based on Windows 1MB stack size
}

DEFINE_PRIM(regexp_new,1);
Expand Down

0 comments on commit 84a45ee

Please sign in to comment.