Skip to content

Commit 55101da

Browse files
committed
fixed regex memory leak
1 parent ace20e5 commit 55101da

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

lib/stdlib/source/DDP/regex.c

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ typedef struct TrefferList {
1919
ddpint cap;
2020
} TrefferList;
2121

22-
static pcre2_code *compile_regex(PCRE2_SPTR pattern, PCRE2_SPTR subject, ddpstring *muster) {
22+
static pcre2_code *compile_regex(PCRE2_SPTR pattern) {
2323
int errornumber;
2424
size_t erroroffset;
2525

@@ -37,7 +37,7 @@ static pcre2_code *compile_regex(PCRE2_SPTR pattern, PCRE2_SPTR subject, ddpstri
3737
if (re == NULL) {
3838
PCRE2_UCHAR buffer[256];
3939
pcre2_get_error_message(errornumber, buffer, sizeof(buffer));
40-
ddp_error("Regex-Feher in '" DDP_STRING_FMT "' bei Spalte %d: %s\n", false, muster->str, (int)erroroffset, buffer);
40+
ddp_error("Regex-Feher in '" DDP_STRING_FMT "' bei Spalte %d: %s\n", false, pattern, (int)erroroffset, buffer);
4141
}
4242

4343
return re;
@@ -85,7 +85,7 @@ void Regex_Erster_Treffer(Treffer *ret, ddpstring *muster, ddpstring *text) {
8585
PCRE2_SPTR pattern = (PCRE2_SPTR)muster->str; // The regex pattern
8686
PCRE2_SPTR subject = (PCRE2_SPTR)text->str; // The string to match against
8787

88-
pcre2_code *re = compile_regex(pattern, subject, muster);
88+
pcre2_code *re = compile_regex(pattern);
8989
if (re == NULL) {
9090
ret->text = DDP_EMPTY_STRING;
9191
ddp_ddpstringlist_from_constants(&ret->gruppen, 0);
@@ -140,7 +140,7 @@ void Regex_N_Treffer(TrefferList *ret, ddpstring *muster, ddpstring *text, ddpin
140140
// Initialize an empty list into ret
141141
*ret = DDP_EMPTY_LIST(TrefferList);
142142

143-
pcre2_code *re = compile_regex(pattern, subject, muster);
143+
pcre2_code *re = compile_regex(pattern);
144144
if (re == NULL) {
145145
return;
146146
}
@@ -205,7 +205,7 @@ static void substitute(ddpstring *ret, ddpstring *muster, ddpstring *text, ddpst
205205
PCRE2_SPTR replacement = (PCRE2_SPTR)ersatz->str; // The replacement string
206206

207207
*ret = DDP_EMPTY_STRING;
208-
pcre2_code *re = compile_regex(pattern, subject, muster);
208+
pcre2_code *re = compile_regex(pattern);
209209
if (re == NULL) {
210210
return;
211211
}
@@ -271,7 +271,7 @@ void Regex_Spalten(ddpstringlist *ret, ddpstring *muster, ddpstring *text) {
271271
// Initialize an empty list into ret
272272
ddp_ddpstringlist_from_constants(ret, 0);
273273

274-
pcre2_code *re = compile_regex(pattern, subject, muster);
274+
pcre2_code *re = compile_regex(pattern);
275275
if (re == NULL) {
276276
return;
277277
}
@@ -284,12 +284,13 @@ void Regex_Spalten(ddpstringlist *ret, ddpstring *muster, ddpstring *text) {
284284
}
285285

286286
PCRE2_SIZE start_offset = 0;
287+
const size_t text_u8_len = utf8_strlen(text->str);
287288
// Perform the match
288289
while (true) {
289290
int rc = pcre2_match(
290-
re, // the compiled pattern
291-
subject, // the subject string
292-
utf8_strlen(text->str), // the length of the subject
291+
re, // the compiled pattern
292+
subject, // the subject string
293+
text_u8_len, // the length of the subject
293294
start_offset,
294295
0, // default options
295296
match_data, // block for storing the result
@@ -318,11 +319,11 @@ void Regex_Spalten(ddpstringlist *ret, ddpstring *muster, ddpstring *text) {
318319
if (ret->len == ret->cap) {
319320
ddpint old_cap = ret->cap;
320321
ret->cap = DDP_GROW_CAPACITY(ret->cap);
321-
ret->arr = ddp_reallocate(ret->arr, old_cap * sizeof(Treffer), ret->cap * sizeof(Treffer));
322+
ret->arr = ddp_reallocate(ret->arr, old_cap * sizeof(ddpstring), ret->cap * sizeof(ddpstring));
322323
}
323324

324325
// append new element
325-
memcpy(&((uint8_t *)ret->arr)[ret->len * sizeof(ddpstring)], &r, sizeof(ddpstring));
326+
ret->arr[ret->len] = r;
326327
ret->len++;
327328

328329
start_offset = pcre2_get_ovector_pointer(match_data)[1];
@@ -332,18 +333,19 @@ void Regex_Spalten(ddpstringlist *ret, ddpstring *muster, ddpstring *text) {
332333
if (ret->len == ret->cap) {
333334
ddpint old_cap = ret->cap;
334335
ret->cap = DDP_GROW_CAPACITY(ret->cap);
335-
ret->arr = ddp_reallocate(ret->arr, old_cap * sizeof(Treffer), ret->cap * sizeof(Treffer));
336+
ret->arr = ddp_reallocate(ret->arr, old_cap * sizeof(ddpstring), ret->cap * sizeof(ddpstring));
336337
}
337338

339+
const ddpint text_len = ddp_strlen(text);
338340
ddpstring r;
339-
r.str = DDP_ALLOCATE(char, ddp_strlen(text) - start_offset + 1);
340-
strncpy(r.str, text->str + start_offset, ddp_strlen(text) - start_offset);
341+
r.str = DDP_ALLOCATE(char, text_len - start_offset + 1);
342+
strncpy(r.str, text->str + start_offset, text_len - start_offset);
341343

342-
r.str[ddp_strlen(text) - start_offset] = '\0';
343-
r.cap = ddp_strlen(text) - start_offset + 1;
344+
r.str[text_len - start_offset] = '\0';
345+
r.cap = text_len - start_offset + 1;
344346

345347
// append new element
346-
memcpy(&((uint8_t *)ret->arr)[ret->len * sizeof(ddpstring)], &r, sizeof(ddpstring));
348+
ret->arr[ret->len] = r;
347349
ret->len++;
348350

349351
// Free up the regular expression and match data

0 commit comments

Comments
 (0)