Hi,

This bug was reported [1] on debian against the OpenBSD implementation
of csh.

[1] https://bugs.debian.org/862221

The current code in exp.c:exp4() and exp.c:exp5() makes sequances of
ADDOP (resp. MULOP) evaluated from the right to the left. This is wrong,
especially when '-' (resp. '/') is the first operator of the sequence:
 $ /bin/bsd-csh -c "@ var = 10 - 2 + 4; echo \$var # should be 12"
 4 # should be 12
 $ /bin/bsd-csh -c "@ var = 100 / 10 / 5; echo \$var # should be 2"
 50 # should be 2

The TENEX implementation (tcsh) gives the correct results:
 $ /bin/tcsh -c "@ var = 10 - 2 + 4; echo \$var # should be 12"
 12 # should be 12
 $ /bin/tcsh -c "@ var = 100 / 10 / 5; echo \$var # should be 2"
 2 # should be 2

Please find attached a very simple fix.

Thanks,

_g.
Description: Fix arithmetic precedence for consecutive ADDOP or MULOP
Author: Gilles Filippini <[email protected]>
Bug-Debian: https://bugs.debian.org/862221
Index: csh-20110502/exp.c
===================================================================
--- csh-20110502.orig/exp.c
+++ csh-20110502/exp.c
@@ -317,10 +317,10 @@ exp4(Char ***vp, bool ignore)
 #ifdef EDEBUG
     etracc("exp4 p1", p1, vp);
 #endif
-    if (isa(**vp, ADDOP)) {
+    while (isa(**vp, ADDOP)) {
 	Char *op = *(*vp)++;
 
-	p2 = exp4(vp, ignore);
+	p2 = exp5(vp, ignore);
 #ifdef EDEBUG
 	etracc("exp4 p2", p2, vp);
 #endif
@@ -337,7 +337,7 @@ exp4(Char ***vp, bool ignore)
 	    }
 	xfree((ptr_t) p1);
 	xfree((ptr_t) p2);
-	return (putn(i));
+	p1 = putn(i);
     }
     return (p1);
 }
@@ -352,10 +352,10 @@ exp5(Char ***vp, bool ignore)
 #ifdef EDEBUG
     etracc("exp5 p1", p1, vp);
 #endif
-    if (isa(**vp, MULOP)) {
+    while (isa(**vp, MULOP)) {
 	Char *op = *(*vp)++;
 
-	p2 = exp5(vp, ignore);
+	p2 = exp6(vp, ignore);
 #ifdef EDEBUG
 	etracc("exp5 p2", p2, vp);
 #endif
@@ -382,7 +382,7 @@ exp5(Char ***vp, bool ignore)
 	    }
 	xfree((ptr_t) p1);
 	xfree((ptr_t) p2);
-	return (putn(i));
+	p1 = putn(i);
     }
     return (p1);
 }

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to