Skip to content

Commit 7aa0f32

Browse files
authored
Fixed sVal initialization and some potential memory leaks. (#1)
1 parent 0d12ca0 commit 7aa0f32

2 files changed

Lines changed: 47 additions & 23 deletions

File tree

.idea/workspace.xml

Lines changed: 36 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/c/JavaPcre.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ RegexStruct pcre2_single_jmatch(char *b, pcre2_code *re, int offset, MatchOption
263263
PCRE2_SIZE subject_length;
264264
PCRE2_SIZE *ovector;
265265

266+
RegexStruct sVal;
267+
266268
// constructing the uint32_t option0 parameter for match function from MatchOptionsStruct values.
267269
uint32_t option0 = 0;
268270
if (temp->JPCRE2_ANCHORED != 0) {
@@ -349,7 +351,6 @@ RegexStruct pcre2_single_jmatch(char *b, pcre2_code *re, int offset, MatchOption
349351
*/
350352
default: printf("Matching error %d\n", rc); break;
351353
}
352-
RegexStruct sVal;
353354
sVal.numVals = 0;
354355
sVal.vals = (char**)malloc(sizeof(char*) * 1);
355356
sVal.names = (char**)malloc(sizeof(char*) * 1);
@@ -377,7 +378,7 @@ RegexStruct pcre2_single_jmatch(char *b, pcre2_code *re, int offset, MatchOption
377378

378379
ovector = pcre2_get_ovector_pointer(match_data);
379380

380-
RegexStruct sVal;
381+
381382
sVal.numVals = rc;
382383
sVal.vals = (char**)malloc(sizeof(char*) * sVal.numVals);
383384
sVal.ovector = (int*)malloc(sizeof(int) * (2 + (sVal.numVals * 2)));
@@ -417,6 +418,14 @@ RegexStruct pcre2_single_jmatch(char *b, pcre2_code *re, int offset, MatchOption
417418

418419
if (namecount == 0){
419420
sVal.namescount = namecount;
421+
sVal.names = (char**)malloc(sizeof(char*) * 1);
422+
sVal.namesnum = (int*)malloc(sizeof(int) * 1);
423+
if (sVal.names == NULL || sVal.namesnum == NULL) {
424+
printf("Error: Out of memory\r\n");
425+
exit(-1);
426+
}
427+
memset(sVal.names, 0, sizeof(char*) * 1);
428+
memset(sVal.namesnum, 0, sizeof(int) * 1);
420429
} else
421430
{
422431
PCRE2_SPTR tabptr;

0 commit comments

Comments
 (0)