]> Cypherpunks repositories - gostls13.git/commitdiff
Use unsigned int in itoa to avoid relying on the behaviour of
authorIan Lance Taylor <iant@golang.org>
Mon, 29 Sep 2008 20:40:23 +0000 (13:40 -0700)
committerIan Lance Taylor <iant@golang.org>
Mon, 29 Sep 2008 20:40:23 +0000 (13:40 -0700)
signed integer overflow when negating the most negative
integer.

R=rsc
DELTA=11  (0 added, 7 deleted, 4 changed)
OCL=16105
CL=16120

src/lib/strings.go

index a4e81b3544f7d94abc3d95d630598bd3a45dd52d..4a2184341f74584a59d3d188b6fd4041e0f62d0e 100644 (file)
@@ -168,30 +168,23 @@ export func itoa(i int) string {
        }
        
        neg := false;   // negative
-       bigneg := false;        // largest negative number
+       u := uint(i);
        if i < 0 {
                neg = true;
-               i = -i;
-               if i < 0 {
-                       bigneg = true;  // is largest negative int
-                       i--     // now positive
-               }
+               u = -u;
        }
 
        // Assemble decimal in reverse order.
        var b [32]byte;
        bp := len(b);
-       for ; i > 0; i /= 10 {
+       for ; u > 0; u /= 10 {
                bp--;
-               b[bp] = byte(i%10) + '0'
+               b[bp] = byte(u%10) + '0'
        }
        if neg {        // add sign
                bp--;
                b[bp] = '-'
        }
-       if bigneg {     // account for i-- above
-               b[len(b)-1]++
-       }
        
        // BUG return string(b[bp:len(b)])
        return string((&b)[bp:len(b)])