extmod/modure: Make sure that errors in regexps are caught early.
This commit is contained in:
parent
7cce2f664c
commit
aee704ebe1
|
@ -185,6 +185,9 @@ STATIC const mp_obj_type_t re_type = {
|
|||
STATIC mp_obj_t mod_re_compile(uint n_args, const mp_obj_t *args) {
|
||||
const char *re_str = mp_obj_str_get_str(args[0]);
|
||||
int size = re1_5_sizecode(re_str);
|
||||
if (size == -1) {
|
||||
goto error;
|
||||
}
|
||||
mp_obj_re_t *o = m_new_obj_var(mp_obj_re_t, char, size);
|
||||
o->base.type = &re_type;
|
||||
int flags = 0;
|
||||
|
@ -193,6 +196,7 @@ STATIC mp_obj_t mod_re_compile(uint n_args, const mp_obj_t *args) {
|
|||
}
|
||||
int error = re1_5_compilecode(&o->re, re_str);
|
||||
if (error != 0) {
|
||||
error:
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Error in regex"));
|
||||
}
|
||||
if (flags & FLAG_DEBUG) {
|
||||
|
|
|
@ -62,3 +62,8 @@ m = re.match('a*?', 'ab'); print(m.group(0))
|
|||
m = re.match('^ab$', 'ab'); print(m.group(0))
|
||||
m = re.match('a|b', 'b'); print(m.group(0))
|
||||
m = re.match('a|b|c', 'c'); print(m.group(0))
|
||||
|
||||
try:
|
||||
re.compile("*")
|
||||
except:
|
||||
print("Caught invalid regex")
|
||||
|
|
Loading…
Reference in New Issue