summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--misc/tests/cregex_test.c32
-rw-r--r--src/cregex.c8
2 files changed, 22 insertions, 18 deletions
diff --git a/misc/tests/cregex_test.c b/misc/tests/cregex_test.c
index ac9636dc..ac7e958f 100644
--- a/misc/tests/cregex_test.c
+++ b/misc/tests/cregex_test.c
@@ -203,11 +203,11 @@ CTEST(cregex, search_all)
ASSERT_NE(res, CREG_OK);
}
}
-// TEST FAILS sanitize!!!
-CTEST_SKIP(cregex, captures_len)
+
+CTEST(cregex, captures_len)
{
c_AUTO (cregex, re) {
- cregex re = cregex_from("(ab(cd))(ef)");
+ re = cregex_from("(ab(cd))(ef)");
ASSERT_EQ(cregex_captures(&re), 4);
}
}
@@ -250,33 +250,33 @@ CTEST(cregex, replace)
const char* input = "start date: 2015-12-31, end date: 2022-02-28";
c_AUTO (cstr, str) {
- /* replace with a fixed string, extended all-in-one call: */
+ // replace with a fixed string, extended all-in-one call:
cstr_take(&str, cregex_replace_pattern(pattern, input, "YYYY-MM-DD"));
- ASSERT_TRUE(cstr_equals(&str, "start date: YYYY-MM-DD, end date: YYYY-MM-DD"));
+ ASSERT_STREQ(cstr_str(&str), "start date: YYYY-MM-DD, end date: YYYY-MM-DD");
- /* US date format, and add 10 years to dates: */
+ // US date format, and add 10 years to dates:
cstr_take(&str, cregex_replace_pattern(pattern, input, "$1/$3/$2", 0, add_10_years, CREG_DEFAULT));
- ASSERT_TRUE(cstr_equals(&str, "start date: 2025/31/12, end date: 2032/28/02"));
+ ASSERT_STREQ(cstr_str(&str), "start date: 2025/31/12, end date: 2032/28/02");
- /* Wrap first date inside []: */
+ // Wrap first date inside []:
cstr_take(&str, cregex_replace_pattern(pattern, input, "[$0]", 1));
- ASSERT_TRUE(cstr_equals(&str, "start date: [2015-12-31], end date: 2022-02-28"));
+ ASSERT_STREQ(cstr_str(&str), "start date: [2015-12-31], end date: 2022-02-28");
- /* Wrap all words in ${} */
+ // Wrap all words in ${}
cstr_take(&str, cregex_replace_pattern("[a-z]+", "52 apples and 31 mangoes", "$${$0}"));
- ASSERT_TRUE(cstr_equals(&str, "52 ${apples} ${and} 31 ${mangoes}"));
+ ASSERT_STREQ(cstr_str(&str), "52 ${apples} ${and} 31 ${mangoes}");
- /* Compile RE separately */
+ // Compile RE separately
c_WITH (cregex re = cregex_from(pattern), cregex_drop(&re)) {
ASSERT_EQ(cregex_captures(&re), 4);
- /* European date format. */
+ // European date format.
cstr_take(&str, cregex_replace(&re, input, "$3.$2.$1"));
- ASSERT_TRUE(cstr_equals(&str, "start date: 31.12.2015, end date: 28.02.2022"));
+ ASSERT_STREQ(cstr_str(&str), "start date: 31.12.2015, end date: 28.02.2022");
- /* Strip out everything but the matches */
+ // Strip out everything but the matches
cstr_take(&str, cregex_replace_sv(&re, csview_from(input), "$3.$2.$1;", 0, NULL, CREG_R_STRIP));
- ASSERT_TRUE(cstr_equals(&str, "31.12.2015;28.02.2022;"));
+ ASSERT_STREQ(cstr_str(&str), "31.12.2015;28.02.2022;");
}
}
}
diff --git a/src/cregex.c b/src/cregex.c
index 7789ee6e..0688d9e1 100644
--- a/src/cregex.c
+++ b/src/cregex.c
@@ -347,6 +347,7 @@ typedef struct _Parser
bool lastwasand; /* Last token was _operand */
short nbra;
short nclass;
+ size_t instcap;
_Rune yyrune; /* last lex'd rune */
_Reclass *yyclassp; /* last lex'd class */
_Reclass* classp;
@@ -546,6 +547,9 @@ _optimize(_Parser *par, _Reprog *pp)
* necessary. Reallocate to the actual space used
* and then relocate the code.
*/
+ if ((par->freep - pp->firstinst)*2 > (ptrdiff_t)par->instcap)
+ return pp;
+
intptr_t ipp = (intptr_t)pp;
size_t size = sizeof(_Reprog) + (size_t)(par->freep - pp->firstinst)*sizeof(_Reinst);
_Reprog *npp = (_Reprog *)c_realloc(pp, size);
@@ -843,8 +847,8 @@ _regcomp1(_Reprog *progp, _Parser *par, const char *s, int cflags)
_Token token;
/* get memory for the program. estimated max usage */
- const size_t instcap = 5 + 6*strlen(s);
- _Reprog* pp = (_Reprog *)c_realloc(progp, sizeof(_Reprog) + instcap*sizeof(_Reinst));
+ par->instcap = 5U + 6*strlen(s);
+ _Reprog* pp = (_Reprog *)c_realloc(progp, sizeof(_Reprog) + par->instcap*sizeof(_Reinst));
if (pp == NULL) {
par->error = CREG_OUTOFMEMORY;
c_free(progp);