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

Re: arithmetic base accepted on output but not input



On Wed, 14 May 2008 11:22:53 +0100
Stephane Chazelas <Stephane_Chazelas@xxxxxxxx> wrote:
> Hiya,
> 
> there's a bit of a consistency issue here:
> 
> ~$ echo $(([#50]49))
> 50#h
> ~$ echo $((50#h))
> zsh: invalid base: 50
> ~$ integer -i 50 a
> ~$ a=123
> ~$ echo $a
> 50#2N

Right, and it looks like we don't check bases below two at all.  (You can
sort of do base 1 just by holding up the appropriate number of fingers, but
I don't feel like implementing it.  Actually, that's not quite true: it
would be fun to implement but awful to handle all the bug reports saying "I
accidentally set the base to 1 and the system hung for a week when I tried
to print out 0xffffffff".  I don't see a use for base zero.)

Index: Doc/Zsh/builtins.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/builtins.yo,v
retrieving revision 1.105
diff -u -r1.105 builtins.yo
--- Doc/Zsh/builtins.yo	8 May 2008 12:07:06 -0000	1.105
+++ Doc/Zsh/builtins.yo	14 May 2008 10:35:29 -0000
@@ -1551,7 +1551,7 @@
 item(tt(-i))(
 Use an internal integer representation.  If var(n) is nonzero it
 defines the output arithmetic base, otherwise it is determined by the
-first assignment.
+first assignment.  Bases from 2 to 36 inclusive are allowed.
 )
 item(tt(-E))(
 Use an internal double-precision floating point representation.  On output
Index: Src/builtin.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/builtin.c,v
retrieving revision 1.193
diff -u -r1.193 builtin.c
--- Src/builtin.c	12 May 2008 13:50:42 -0000	1.193
+++ Src/builtin.c	14 May 2008 10:35:34 -0000
@@ -1744,6 +1744,10 @@
 		zwarnnam(name, "bad precision value: %s", arg);
 	    return 1;
 	}
+	if (pm->base < 2 || pm->base > 36) {
+	    zwarnnam(name, "invalid base: %d", pm->base);
+	    return 1;
+	}
     } else if (always)
 	pm->base = 0;
 
Index: Src/math.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/math.c,v
retrieving revision 1.30
diff -u -r1.30 math.c
--- Src/math.c	15 Jun 2007 10:03:58 -0000	1.30
+++ Src/math.c	14 May 2008 10:35:35 -0000
@@ -460,6 +460,10 @@
 		}
 		if(*ptr != ']')
 			goto bofs;
+		if (outputradix < 2 || outputradix > 36) {
+		    zerr("invalid base: %d", outputradix);
+		    return EOI;
+		}
 		ptr++;
 		break;
 	    }
Index: Src/utils.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/utils.c,v
retrieving revision 1.189
diff -u -r1.189 utils.c
--- Src/utils.c	12 May 2008 13:50:42 -0000	1.189
+++ Src/utils.c	14 May 2008 10:35:39 -0000
@@ -1834,7 +1834,7 @@
 	    base = 8;
     }
     inp = s;
-    if (base > 36) {
+    if (base < 2 || base > 36) {
 	zerr("invalid base: %d", base);
 	return (zlong)0;
     } else if (base <= 10)



-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070



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