From 32d596da36758446331c4202dad9494329545ec2 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Wed, 2 Feb 2022 13:53:00 +0100 Subject: lib: fix infinite loop on empty regexp matches in uc_split() The regular expression `/()/` will match the empty string, causing the match loop to never advance. Add extra logic to deal with this case, similar to the empty separator string logic. Signed-off-by: Jo-Philipp Wich --- lib.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'lib.c') diff --git a/lib.c b/lib.c index e204ded..6235470 100644 --- a/lib.c +++ b/lib.c @@ -927,9 +927,18 @@ uc_split(uc_vm_t *vm, size_t nargs) if (res == REG_NOMATCH) break; - ucv_array_push(arr, ucv_string_new_length(splitstr, pmatch.rm_so)); + if (pmatch.rm_so != pmatch.rm_eo) { + ucv_array_push(arr, ucv_string_new_length(splitstr, pmatch.rm_so)); + splitstr += pmatch.rm_eo; + } + else if (*splitstr) { + ucv_array_push(arr, ucv_string_new_length(splitstr, 1)); + splitstr++; + } + else { + goto out; + } - splitstr += pmatch.rm_eo; eflags |= REG_NOTBOL; } @@ -956,6 +965,7 @@ uc_split(uc_vm_t *vm, size_t nargs) return NULL; } +out: return arr; } -- cgit v1.2.3