Commit 489b5001 authored by Ian Lance Taylor's avatar Ian Lance Taylor

Use unsigned int in itoa to avoid relying on the behaviour of

signed integer overflow when negating the most negative
integer.

R=rsc
DELTA=11  (0 added, 7 deleted, 4 changed)
OCL=16105
CL=16120
parent e4a61c65
......@@ -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)])
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment