about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJun-ichi Takimoto <takimoto-j@kba.biglobe.ne.jp>2023-09-13 09:59:42 +0900
committerJun-ichi Takimoto <takimoto-j@kba.biglobe.ne.jp>2023-09-13 09:59:42 +0900
commit0eab788437d42afb290637441f88542fc496c307 (patch)
tree1e7b8455db410a3388e7abd59be92f2babbbeeb4
parent96ce0abf6b7607ca2df64759c495d153132d07bd (diff)
downloadzsh-0eab788437d42afb290637441f88542fc496c307.tar.gz
zsh-0eab788437d42afb290637441f88542fc496c307.tar.xz
zsh-0eab788437d42afb290637441f88542fc496c307.zip
52122 + 52129: fix (#) parameter expansion flag
Without the X flag, null string "" is substituted for bad math expression.
-rw-r--r--ChangeLog6
-rw-r--r--Src/subst.c13
-rw-r--r--Test/D04parameter.ztst40
3 files changed, 56 insertions, 3 deletions
diff --git a/ChangeLog b/ChangeLog
index c8bc5e30f..45cb416dc 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2023-09-13  Jun-ichi Takimoto  <takimoto-j@kba.biglobe.ne.jp>
+
+	* 52122 + 52129: Src/subst.c, Test/D04parameter.ztst: fix (#)
+	parameter expansion flag for bad math expressions and out-of-
+	range characters
+
 2023-09-10  Bart Schaefer  <schaefer@zsh.org>
 
 	* 52125: Src/exec.c: getoutput() must not free() gettempname()
diff --git a/Src/subst.c b/Src/subst.c
index 14947ae36..d68159227 100644
--- a/Src/subst.c
+++ b/Src/subst.c
@@ -1489,11 +1489,18 @@ subst_parse_str(char **sp, int single, int err)
 static char *
 substevalchar(char *ptr)
 {
-    zlong ires = mathevali(ptr);
+    zlong ires;
     int len = 0;
+    int saved_errflag = errflag;
 
-    if (errflag)
-	return NULL;
+    errflag = 0;
+    ires = mathevali(ptr);
+
+    if (errflag) {  /* not a valid numerical expression */
+	errflag |= saved_errflag;
+	return noerrs ? dupstring(""): NULL;
+    }
+    errflag |= saved_errflag;
 #ifdef MULTIBYTE_SUPPORT
     if (isset(MULTIBYTE) && ires > 127) {
 	/* '\\' + 'U' + 8 bytes of character + '\0' */
diff --git a/Test/D04parameter.ztst b/Test/D04parameter.ztst
index 0d44558a7..12ae1a446 100644
--- a/Test/D04parameter.ztst
+++ b/Test/D04parameter.ztst
@@ -2785,3 +2785,43 @@ F:behavior, see http://austingroupbugs.net/view.php?id=888
 >string with spaces
 >stringwithspaces
 >stringwithspaces
+
+  : ${(#X):-@}
+1:${(#X)...}: bad math expression
+?(eval):1: bad math expression: illegal character: @
+
+  echo a${(#):-@}z
+0:${(#)...}: bad math expression
+>az
+
+  printf "a%sz\n" ${(#):-@}
+0:${(#)...}: bad math expression, printf
+>az
+
+  a=( '1 +' '@' )
+  : ${(#X)a}
+1:${(#X)...}: array of bad math expressions
+?(eval):2: bad math expression: operand expected at end of string
+
+  printf "a%sz\n" ${(#)a}
+0:${(#)...}: array of bad math expressions, printf
+>az
+
+  : ${(#X):-0x80}
+1:${(#X)...}: out-of-range character
+?(eval):1: character not in range
+
+  [[ ${(#):-0x80} = $'\x80' ]] && echo OK
+0:${(#)...}: out-of-range character
+>OK
+
+  a=( 0x80 0x81 )
+  : ${(#X)a}
+1:${(#X)...}: array of out-of-range characters
+?(eval):2: character not in range
+
+  printf "%s\n" ${(#)a} |
+  while read x; do echo $(( #x )); done
+0:${(#)...}: array of out-of-range characters
+>128
+>129