Skip to content

Commit aee704e

Browse files
committed
extmod/modure: Make sure that errors in regexps are caught early.
1 parent 7cce2f6 commit aee704e

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

extmod/modure.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ STATIC const mp_obj_type_t re_type = {
185185
STATIC mp_obj_t mod_re_compile(uint n_args, const mp_obj_t *args) {
186186
const char *re_str = mp_obj_str_get_str(args[0]);
187187
int size = re1_5_sizecode(re_str);
188+
if (size == -1) {
189+
goto error;
190+
}
188191
mp_obj_re_t *o = m_new_obj_var(mp_obj_re_t, char, size);
189192
o->base.type = &re_type;
190193
int flags = 0;
@@ -193,6 +196,7 @@ STATIC mp_obj_t mod_re_compile(uint n_args, const mp_obj_t *args) {
193196
}
194197
int error = re1_5_compilecode(&o->re, re_str);
195198
if (error != 0) {
199+
error:
196200
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Error in regex"));
197201
}
198202
if (flags & FLAG_DEBUG) {

tests/extmod/ure1.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,8 @@
6262
m = re.match('^ab$', 'ab'); print(m.group(0))
6363
m = re.match('a|b', 'b'); print(m.group(0))
6464
m = re.match('a|b|c', 'c'); print(m.group(0))
65+
66+
try:
67+
re.compile("*")
68+
except:
69+
print("Caught invalid regex")

0 commit comments

Comments
 (0)