aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--jimregexp.c20
-rw-r--r--jimregexp.h3
-rw-r--r--tests/regexp.test9
3 files changed, 23 insertions, 9 deletions
diff --git a/jimregexp.c b/jimregexp.c
index 3f2711b..e04d3a4 100644
--- a/jimregexp.c
+++ b/jimregexp.c
@@ -719,7 +719,7 @@ static int regatom(regex_t *preg, int *flagp)
pattern++;
}
- while (*pattern && *pattern != ']') {
+ while (*pattern != ']') {
/* Is this a range? a-z */
int start;
int end;
@@ -731,6 +731,11 @@ static int regatom(regex_t *preg, int *flagp)
};
int cc;
+ if (!*pattern) {
+ preg->err = REG_ERR_UNMATCHED_BRACKET;
+ return 0;
+ }
+
pattern += reg_utf8_tounicode_case(pattern, &start, nocase);
if (start == '\\') {
/* First check for class shorthand escapes */
@@ -754,6 +759,10 @@ static int regatom(regex_t *preg, int *flagp)
preg->err = REG_ERR_NULL_CHAR;
return 0;
}
+ if (start == '\\' && *pattern == 0) {
+ preg->err = REG_ERR_INVALID_ESCAPE;
+ return 0;
+ }
}
if (pattern[0] == '-' && pattern[1] && pattern[1] != ']') {
/* skip '-' */
@@ -765,6 +774,10 @@ static int regatom(regex_t *preg, int *flagp)
preg->err = REG_ERR_NULL_CHAR;
return 0;
}
+ if (start == '\\' && *pattern == 0) {
+ preg->err = REG_ERR_INVALID_ESCAPE;
+ return 0;
+ }
}
reg_addrange(preg, start, end);
@@ -869,7 +882,7 @@ cc_switch:
ch = *preg->regparse++;
switch (ch) {
case '\0':
- preg->err = REG_ERR_TRAILING_BACKSLASH;
+ preg->err = REG_ERR_INVALID_ESCAPE;
return 0;
case 'A':
ret = regnode(preg, BOLX);
@@ -1875,9 +1888,10 @@ size_t regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_s
"nested count",
"internal error",
"count follows nothing",
- "trailing backslash",
+ "invalid escape \\ sequence",
"corrupted program",
"contains null char",
+ "brackets [] not balanced",
};
const char *err;
diff --git a/jimregexp.h b/jimregexp.h
index b7598d4..c8bf8c3 100644
--- a/jimregexp.h
+++ b/jimregexp.h
@@ -91,9 +91,10 @@ enum {
REG_ERR_NESTED_COUNT,
REG_ERR_INTERNAL,
REG_ERR_COUNT_FOLLOWS_NOTHING,
- REG_ERR_TRAILING_BACKSLASH,
+ REG_ERR_INVALID_ESCAPE,
REG_ERR_CORRUPTED,
REG_ERR_NULL_CHAR,
+ REG_ERR_UNMATCHED_BRACKET,
REG_ERR_NUM
};
diff --git a/tests/regexp.test b/tests/regexp.test
index e372fbd..c6133e7 100644
--- a/tests/regexp.test
+++ b/tests/regexp.test
@@ -714,14 +714,13 @@ test regexp-22.10 {Various char escapes} {
join $result |
} {12,11|8,9|27}
-test regexp-22.11 {backslash as last char} {
+test regexp-22.11 {backslash as last char} -body {
regexp -all -inline "\[a\\" "ba\\d\[ef"
-} "a\ \\\\"
+} -returnCodes error -result {couldn't compile regular expression pattern: invalid escape \ sequence}
-# Probably should be an error
-test regexp-22.12 {missing closing bracket} {
+test regexp-22.12 {missing closing bracket} -body {
regexp -all -inline {[abc} "abcdefghi"
-} {a b c}
+} -returnCodes error -result {couldn't compile regular expression pattern: brackets [] not balanced}
test regexp-22.13 {empty alternative} {
regexp -all -inline {a(a|b|)c} "aacbacbaa"