]> git.ipfire.org Git - thirdparty/bash.git/commitdiff
change overflow behavior in fltexpr; fix for readline event hook; fix for internal... devel
authorChet Ramey <chet.ramey@case.edu>
Fri, 11 Jul 2025 15:48:01 +0000 (11:48 -0400)
committerChet Ramey <chet.ramey@case.edu>
Fri, 11 Jul 2025 15:48:01 +0000 (11:48 -0400)
CWRU/CWRU.chlog
MANIFEST
examples/loadables/fltexpr.c
externs.h
lib/readline/input.c
subst.c
tests/arith.right
tests/arith.tests
tests/arith11.sub [new file with mode: 0644]
tests/quotearray.right

index fc1002cfe5f0c1695edd0f77180416560601d794..ce51a3e0d6b30fe2364ae16261a86e8e55d68f29 100644 (file)
@@ -11344,3 +11344,22 @@ builtins/printf.def
        - printstr: only adjust precision if the decoded precision is >= 0,
          since decodeint returns -1 on overflow/failure
 
+lib/readline/input.c
+       - _rl_gather_tyi: if the input-available hook returns success,
+         indicating that input is available, set chars_avail to 1, assuming
+         there is at minimum a single character of input available
+       - _rl_gather_tyi: if pselect/select return non-zero, indicating that
+         input is available, set result = -1 before going on so subsequent
+         branches will check how many chars are available and set chars_avail
+         Report from Dmitri A. Sergatskov <dasergatskov@gmail.com>
+
+                                  7/10
+                                  ----
+subst.c
+       - ARITH_EXP_CHARS: remove ~ from set because we don't perform tilde
+         expansion when expanding arithmetic expressions
+       - string_quote_removal: if quoted includes Q_ARITH, indicating we are
+         expanding an arithmetic expression for a compound command or array
+         subscript, remove backslashes quoting `[', `]', and `~', since those
+         are quoted by expand_array_subscript but not dequoted anywhere else
+         Fixes quoting bug reported by Isabella Bosia <izaberina@gmail.com>
index 985d1793b652f773ab3f2f94aec53f2f71b1db5b..78143bc872a4de0638adcc331a77fd423b2ba26c 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -970,6 +970,7 @@ tests/arith7.sub    f
 tests/arith8.sub       f
 tests/arith9.sub       f
 tests/arith10.sub      f
+tests/arith11.sub      f
 tests/array.tests      f
 tests/array.right      f
 tests/array1.sub       f
index 7f775f276b1c56f1f50dc1d8f248cf4b70b0d2e5..48c132e15e709d99ecc47e0e32edc5e149828b12 100644 (file)
@@ -148,6 +148,7 @@ typedef double sh_float_t;
 #define SHFLOAT_MANT_DIG       DBL_MANT_DIG
 #define SHFLOAT_LENGTH_MODIFIER        'l';
 #define SHFLOAT_STRTOD         strtod
+#define SHFLOAT_HUGE_VAL       HUGE_VAL
 
 #ifndef M_EGAMMA
 #define M_EGAMMA 0.57721566490153286060651209008240243
@@ -489,9 +490,11 @@ fltexpr_strtod (const char *nptr, char **ep)
 
   errno = 0;
   r = SHFLOAT_STRTOD (nptr, &xp);
-  if (errno == ERANGE)
+  if (errno == ERANGE && (r == SHFLOAT_HUGE_VAL || r == -SHFLOAT_HUGE_VAL))
+    r = (r == SHFLOAT_HUGE_VAL) ? infval : -infval;
+  else if (errno == ERANGE)
     evalerror ("number out of range");
-  else if (r == 0 && *ep == nptr)
+  else if (r == 0 && xp == nptr)
     evalerror ("invalid number");
   if (ep)
     *ep = xp;
index 47b80f6a2d963d9a9212bc3bbc7e37951e3812ef..d73b5a1aea352baa4fa4336c08557ddc038c8f59 100644 (file)
--- a/externs.h
+++ b/externs.h
@@ -27,7 +27,8 @@
 #include "stdc.h"
 
 /* Functions from expr.c. */
-#define EXP_EXPANDED   0x01
+#define EXP_EXPANDED   0x01    /* already expanded */
+#define EXP_QUOTED     0x02    /* expanded, needs internal quote removal, not used yet */
 
 extern intmax_t evalexp (const char *, int, int *);
 
index e6a39e26b64a75aee21138a85d27132e9261502e..3383edb678a99593b935ccc5d2f86b869ef45198 100644 (file)
@@ -261,13 +261,16 @@ rl_gather_tyi (void)
   input = 0;
   tty = fileno (rl_instream);
 
-  /* Move this up here to give it first shot, but it can't set chars_avail */
+  /* Move this up here to give it first shot, but it can't set chars_avail,
+     so we assume a single character is available. */
   /* XXX - need rl_chars_available_hook? */
   if (rl_input_available_hook)
     {
       result = (*rl_input_available_hook) ();
       if (result == 0)
         result = -1;
+      else
+        chars_avail = 1;
     }
 
 #if defined (HAVE_PSELECT) || defined (HAVE_SELECT)
@@ -285,6 +288,7 @@ rl_gather_tyi (void)
 #endif
       if (result <= 0)
        return 0;       /* Nothing to read. */
+      result = -1;     /* there is something, so check how many chars below */
     }
 #endif
 
diff --git a/subst.c b/subst.c
index e9d3e7557484e22254e7536ea8a33857d9630817..a8ae174d5da3854fa1c0cf1f98390f76a638ac5e 100644 (file)
--- a/subst.c
+++ b/subst.c
@@ -3795,9 +3795,9 @@ pos_params (const char *string, int start, int end, int quoted, int pflags)
 #define EXP_CHAR(s) (s == '$' || s == '`' || s == CTLESC || s == '~')
 #endif
 
-/* We don't perform process substitution in arithmetic expressions, so don't
-   bother checking for it. */
-#define ARITH_EXP_CHAR(s) (s == '$' || s == '`' || s == CTLESC || s == '~')
+/* We don't perform process substitution or tilde expansion in arithmetic
+   expressions, so don't bother checking for them. */
+#define ARITH_EXP_CHAR(s) (s == '$' || s == '`' || s == CTLESC)
 
 /* If there are any characters in STRING that require full expansion,
    then call FUNC to expand STRING; otherwise just perform quote
@@ -12215,6 +12215,14 @@ string_quote_removal (const char *string, int quoted)
              *r++ = '\\';
              break;
            }
+#if defined (ARRAY_VARS)
+         /* The only special characters that matter here are []~, since those
+            are backslash-quoted in expand_array_subscript but not dequoted
+            by the statement following this one. */
+         if ((quoted & Q_ARITH) && (c == LBRACK || c == RBRACK || c == '~'))
+           ;           /* placeholder here */
+         else
+#endif
          if (((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || dquote) && (sh_syntaxtab[c] & CBSDQUOTE) == 0)
            *r++ = '\\';
          /* FALLTHROUGH */
index 1e52e6b6641f2a8edf4a6e8c2889b0922afaf97c..e9cb891f165da46942d144a7aa7d9792a8c60daa 100644 (file)
@@ -355,15 +355,24 @@ declare -a a=([0]="0")
 3 1
 ./arith10.sub: line 95: let: 0 - "": arithmetic syntax error: operand expected (error token is """")
 4 1
+0
+declare -a yy=([0]="10")
+
+./arith11.sub: line 46: $(echo xxxx) : arithmetic syntax error: operand expected (error token is "$(echo xxxx) ")
+./arith11.sub: line 47: $(echo xxxx): arithmetic syntax error: operand expected (error token is "$(echo xxxx)")
+./arith11.sub: line 48: $(echo xxxx): arithmetic syntax error: operand expected (error token is "$(echo xxxx)")
+0
+Y
+./arith11.sub: line 65: 'foo' : arithmetic syntax error: operand expected (error token is "'foo' ")
 8 12
-./arith.tests: line 335: ((: x=9 y=41 : arithmetic syntax error in expression (error token is "y=41 ")
-./arith.tests: line 339: a b: arithmetic syntax error in expression (error token is "b")
-./arith.tests: line 340: ((: a b: arithmetic syntax error in expression (error token is "b")
+./arith.tests: line 338: ((: x=9 y=41 : arithmetic syntax error in expression (error token is "y=41 ")
+./arith.tests: line 342: a b: arithmetic syntax error in expression (error token is "b")
+./arith.tests: line 343: ((: a b: arithmetic syntax error in expression (error token is "b")
 42
 42
 42
 42
 42
 42
-./arith.tests: line 355: 'foo' : arithmetic syntax error: operand expected (error token is "'foo' ")
-./arith.tests: line 358: b[c]d: arithmetic syntax error in expression (error token is "d")
+./arith.tests: line 358: 'foo' : arithmetic syntax error: operand expected (error token is "'foo' ")
+./arith.tests: line 361: b[c]d: arithmetic syntax error in expression (error token is "d")
index 4e36b005c35f8a2e3dac1c36cd22ad63591295cd..7947a6acd67b2108b3c2b35edbbdacebe9145184 100644 (file)
@@ -324,6 +324,9 @@ ${THIS_SH} ./arith9.sub
 # empty expressions in various arithmetic evaluation contexts
 ${THIS_SH} ./arith10.sub
 
+# internal quoting for array subscripts
+${THIS_SH} ./arith11.sub
+
 x=4
 y=7
 
diff --git a/tests/arith11.sub b/tests/arith11.sub
new file mode 100644 (file)
index 0000000..9a85d81
--- /dev/null
@@ -0,0 +1,65 @@
+#   This program is free software: you can redistribute it and/or modify
+#   it under the terms of the GNU General Public License as published by
+#   the Free Software Foundation, either version 3 of the License, or
+#   (at your option) any later version.
+#
+#   This program is distributed in the hope that it will be useful,
+#   but WITHOUT ANY WARRANTY; without even the implied warranty of
+#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#   GNU General Public License for more details.
+#
+#   You should have received a copy of the GNU General Public License
+#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+# make sure all internal backslash quoting for array subscripts in arithmetic
+# contexts is removed appropriately before the expression is evaluated
+
+a=(0 1 2)
+b=(0 1 2)
+x=0
+
+((i=$x,a[b[i]]))
+let "i=$x,a[b[i]]"
+
+echo $((i=$x,a[b[i]]))
+for (( i=$x,a[b[i]]; i != 0; i--)); do echo a; done
+
+yy[i=$x,a[b[i]]]=10
+declare -p yy
+unset yy
+
+declare -i yy
+yy=i=$x,a[b[i]]
+unset yy
+
+[[ i=$x,a[b[i]] -gt 0 ]]
+
+echo ${var:i=$x,a[b[i]]:4}
+
+unset i a b
+
+xxxx=4
+foo='$(echo xxxx)'
+
+# errors
+
+echo $(( $foo ))
+echo $(( b[~foo/$xxxx] ))      # still attempts tilde expansion
+echo $(( b[~foo * $xxxx] ))
+
+# but associative arrays work, not arithmetic expressions
+typeset -A b
+xxxx=4
+foo='$(echo comsub)'
+
+echo $(( b[~foo * $xxxx] ))
+
+# original report
+
+x[3]=10 i=3 a[5]=42 p=15
+# comparison throws error
+(( a[p - x[i]] > 10)) && echo Y
+
+# errors
+foo=1
+echo $(( 'foo' ))
index a3cc93a6b2c529fca786c2af99be3abc6c538163..a75ef52a67eeb9c38d5d81ef0e2c6edae25c0c2d 100644 (file)
@@ -44,7 +44,7 @@ declare -A assoc=(["\` echo >&2 foo\`"]="128" [0]="0" ["]"]="12" ["x],b[\$(echo
 foo
 0
 0
-./quotearray1.sub: line 68: 0\],b\[1: arithmetic syntax error: invalid arithmetic operator (error token is "\],b\[1")
+./quotearray1.sub: line 68: 0],b[1: arithmetic syntax error: invalid arithmetic operator (error token is "],b[1")
 declare -a array
 0
 0