Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: sh emulation POSIX non-conformances ("inf"/"Inf" in arithmetic expressions)



On Fri, Apr 23, 2021 at 9:46 AM Vincent Lefevre <vincent@xxxxxxxxxx> wrote:
>
> On 2021-04-22 11:55:25 -0700, Bart Schaefer wrote:
> > On Thu, Apr 22, 2021 at 8:31 AM Vincent Lefevre <vincent@xxxxxxxxxx> wrote:
> > >
> > > IMHO, zsh should also output a warning when such variables are used.
> >
> > Exactly how would that work?
>
> When inf or nan is used in a math context and the corresponding
> variable exists.

So, it's the "expend effort on a check that is nearly always going to
fail" option.

Not advocating for the below patch, just providing for reference.
gmail may have munged tabs causing indentation to look funny.

diff --git a/Src/math.c b/Src/math.c
index 1d0d86639..50c34416d 100644
--- a/Src/math.c
+++ b/Src/math.c
@@ -865,12 +865,22 @@ zzlex(void)
         ptr = ie;
         if (ie - p == 3) {
             if (strncasecmp(p, "NaN", 3) == 0) {
+                char iec = *ie; *ie = 0;
+            if (issetvar(p)) {
+                zwarn("%s: using constant NaN", p);
+            }
+            *ie = iec;
             yyval.type = MN_FLOAT;
             yyval.u.d = 0.0;
             yyval.u.d /= yyval.u.d;
             return NUM;
             }
             else if (strncasecmp(p, "Inf", 3) == 0) {
+                char iec = *ie; *ie = 0;
+            if (issetvar(p)) {
+                zwarn("%s: using constant Inf", p);
+            }
+            *ie = iec;
             yyval.type = MN_FLOAT;
             yyval.u.d = 0.0;
             yyval.u.d = 1.0 / yyval.u.d;
diff --git a/Test/C01arith.ztst b/Test/C01arith.ztst
index d0092fefa..e6333890c 100644
--- a/Test/C01arith.ztst
+++ b/Test/C01arith.ztst
@@ -306,17 +306,22 @@
   in=1 info=2 Infinity=3 Inf=4
   print $(( in )) $(( info )) $(( Infinity )) $(( $Inf )) $(( inf ))
$(( INF )) $(( Inf )) $(( iNF ))
 0:Infinity parsing
+?(eval):2: Inf: using constant Inf
 >1 2 3 4 Inf Inf Inf Inf

   integer Inf
   print $(( Inf[0] ))
 1:Refer to Inf with an array subscript
+?(eval):2: Inf: using constant Inf
 ?(eval):2: bad base syntax

+  integer NaN
   (( NaN = 1 ))
 2:Assign to NaN
-?(eval):1: bad math expression: lvalue required
+?(eval):2: NaN: using constant NaN
+?(eval):2: bad math expression: lvalue required

+  unset Inf
   a='Inf'
   (( b = 1e500 ))
   print $((1e500)) $(($((1e500)))) $(( a )) $b $(( b )) $(( 3.0 / 0 ))




Messages sorted by: Reverse Date, Date, Thread, Author