aboutsummaryrefslogtreecommitdiff
path: root/extmod/modure.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2015-03-04 13:51:32 +0000
committerDamien George <damien.p.george@gmail.com>2015-03-04 13:51:32 +0000
commit6cb6947b99e57ababc70fcc0fccd59038682c885 (patch)
treeaded483c33512e2a805f91e2f78a9c7f43391acd /extmod/modure.c
parent2a68c2c21b2bb47016a4794b6fd7fcd7a02d0526 (diff)
extmod/ure: Correctly return None when a group has no match.
See issue #1122.
Diffstat (limited to 'extmod/modure.c')
-rw-r--r--extmod/modure.c6
1 files changed, 6 insertions, 0 deletions
diff --git a/extmod/modure.c b/extmod/modure.c
index 62b72bd87..3f0256ad6 100644
--- a/extmod/modure.c
+++ b/extmod/modure.c
@@ -65,6 +65,10 @@ STATIC mp_obj_t match_group(mp_obj_t self_in, mp_obj_t no_in) {
}
const char *start = self->caps[no * 2];
+ if (start == NULL) {
+ // no match for this group
+ return mp_const_none;
+ }
return mp_obj_new_str(start, self->caps[no * 2 + 1] - start, false);
}
MP_DEFINE_CONST_FUN_OBJ_2(match_group_obj, match_group);
@@ -97,6 +101,7 @@ STATIC mp_obj_t re_exec(bool is_anchored, uint n_args, const mp_obj_t *args) {
subj.end = subj.begin + len;
int caps_num = (self->re.sub + 1) * 2;
mp_obj_match_t *match = m_new_obj_var(mp_obj_match_t, char*, caps_num);
+ memset(match->caps, 0, caps_num * sizeof(char*));
int res = re1_5_recursiveloopprog(&self->re, &subj, match->caps, caps_num, is_anchored);
if (res == 0) {
m_del_var(mp_obj_match_t, char*, caps_num, match);
@@ -135,6 +140,7 @@ STATIC mp_obj_t re_split(uint n_args, const mp_obj_t *args) {
mp_obj_t retval = mp_obj_new_list(0, NULL);
const char **caps = alloca(caps_num * sizeof(char*));
while (true) {
+ memset(caps, 0, caps_num * sizeof(char*));
int res = re1_5_recursiveloopprog(&self->re, &subj, caps, caps_num, false);
// if we didn't have a match, or had an empty match, it's time to stop