[hackers] [sbase][PATCH 2/2] expr: don't evaluate matched substr as a number

From: Randy Palamar <randy_AT_rnpnr.xyz>
Date: Sun, 7 Jan 2024 11:02:18 -0700

POSIX specifies that if the pattern contains a subexpression then
the first matched subexpression should be returned if it exists.

This fixes things like the following:

./expr 00003 : '\(.*\)'
Before: 3
After: 00003
---
 expr.c | 13 ++-----------
 1 file changed, 2 insertions(+), 11 deletions(-)
diff --git a/expr.c b/expr.c
index ae32b9f..3afb94b 100644
--- a/expr.c
+++ b/expr.c
_AT_@ -59,11 +59,9 @@ match(struct val *vstr, struct val *vregx, struct val *ret)
 {
 	regex_t re;
 	regmatch_t matches[2];
-	long long d;
 	size_t anchlen;
 	char *s, *p, *anchreg;
 	char *str = vstr->str, *regx = vregx->str;
-	const char *errstr;
 
 	/* anchored regex */
 	anchlen = strlen(regx) + 1 + 1;
_AT_@ -83,15 +81,8 @@ match(struct val *vstr, struct val *vregx, struct val *ret)
 		s = str + matches[1].rm_so;
 		p = str + matches[1].rm_eo;
 		*p = '\0';
-
-		d = strtonum(s, LLONG_MIN, LLONG_MAX, &errstr);
-		if (!errstr) {
-			ret->num = d;
-			return;
-		} else {
-			ret->str = enstrdup(3, s);
-			return;
-		}
+		ret->str = enstrdup(3, s);
+		return;
 	} else {
 		regfree(&re);
 		str += matches[0].rm_so;
-- 
2.41.0
Received on Sun Jan 07 2024 - 19:02:18 CET

This archive was generated by hypermail 2.3.0 : Sun Jan 07 2024 - 19:12:34 CET