]> Cypherpunks repositories - gostls13.git/commitdiff
syscall: fix infinite recursion in itoa
authorRuss Cox <rsc@golang.org>
Thu, 18 Sep 2014 23:40:06 +0000 (19:40 -0400)
committerRuss Cox <rsc@golang.org>
Thu, 18 Sep 2014 23:40:06 +0000 (19:40 -0400)
Fixes #8332.

LGTM=dvyukov
R=golang-codereviews, dvyukov
CC=golang-codereviews
https://golang.org/cl/138650044

src/syscall/export_test.go [new file with mode: 0644]
src/syscall/str.go
src/syscall/syscall_test.go

diff --git a/src/syscall/export_test.go b/src/syscall/export_test.go
new file mode 100644 (file)
index 0000000..c977462
--- /dev/null
@@ -0,0 +1,7 @@
+// Copyright 2014 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package syscall
+
+var Itoa = itoa
index 0fce842e8c1351bdfac6723c51a0a76875037fc8..2ddf04b2275032e5fbbbd7acc060f60fcf7f026f 100644 (file)
@@ -6,8 +6,12 @@ package syscall
 
 func itoa(val int) string { // do it here rather than with fmt to avoid dependency
        if val < 0 {
-               return "-" + itoa(-val)
+               return "-" + uitoa(uint(-val))
        }
+       return uitoa(uint(val))
+}
+
+func uitoa(val uint) string {
        var buf [32]byte // big enough for int64
        i := len(buf) - 1
        for val >= 10 {
index 2a39b54f1b2dffb6f064e1b7cb76e2442fcbadfc..846c4873d28aadda67541932ebebbd93638cdb90 100644 (file)
@@ -5,6 +5,7 @@
 package syscall_test
 
 import (
+       "fmt"
        "syscall"
        "testing"
 )
@@ -28,3 +29,19 @@ func TestEnv(t *testing.T) {
        // make sure TESTENV gets set to "", not deleted
        testSetGetenv(t, "TESTENV", "")
 }
+
+func TestItoa(t *testing.T) {
+       // Make most negative integer: 0x8000...
+       i := 1
+       for i<<1 != 0 {
+               i <<= 1
+       }
+       if i >= 0 {
+               t.Fatal("bad math")
+       }
+       s := syscall.Itoa(i)
+       f := fmt.Sprint(i)
+       if s != f {
+               t.Fatalf("itoa(%d) = %s, want %s", i, s, f)
+       }
+}