]> Cypherpunks repositories - gostls13.git/commitdiff
strconv: fix ParseUint return value on range overflow
authorMartin Möhrmann <moehrmann@google.com>
Sat, 12 Aug 2017 18:11:55 +0000 (20:11 +0200)
committerMartin Möhrmann <moehrmann@google.com>
Sun, 13 Aug 2017 09:11:32 +0000 (09:11 +0000)
If the value corresponding to the input string cannot be
represented by an unsigned integer of the given size,
err.Err = ErrRange and the returned value is the maximum
magnitude unsigned integer of the appropriate bitSize.
This is consistent with ParseInt's behavior and the documentation.

Expand tests to test 32 bit test value tables with bitsize 32 set.
These tests fail without the fix in this CL.

Fixes #21278

Change-Id: I8aab39279ec3e31905fcbf582a916cbf6d9b95da
Reviewed-on: https://go-review.googlesource.com/55134
Run-TryBot: Martin Möhrmann <moehrmann@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
src/strconv/atoi.go
src/strconv/atoi_test.go

index 66df149172d8741ebd30d39f6fd79d9097380391..8261627fe34eeba0cb9ddb781bedffea8302dcd6 100644 (file)
@@ -115,7 +115,7 @@ func ParseUint(s string, base int, bitSize int) (uint64, error) {
 
                if n >= cutoff {
                        // n*base overflows
-                       n = maxUint64
+                       n = maxVal
                        err = ErrRange
                        goto Error
                }
@@ -124,7 +124,7 @@ func ParseUint(s string, base int, bitSize int) (uint64, error) {
                n1 := n + uint64(v)
                if n1 < n || n1 > maxVal {
                        // n+v overflows
-                       n = maxUint64
+                       n = maxVal
                        err = ErrRange
                        goto Error
                }
index d608505da216ff3ceaeb51b06b62a82a6dd0e777..9cef025941eceea7876da1f03ea41d1e36180e32 100644 (file)
@@ -235,6 +235,17 @@ func init() {
        }
 }
 
+func TestParseUint32(t *testing.T) {
+       for i := range atoui32tests {
+               test := &atoui32tests[i]
+               out, err := ParseUint(test.in, 10, 32)
+               if uint64(test.out) != out || !reflect.DeepEqual(test.err, err) {
+                       t.Errorf("Atoui32(%q) = %v, %v want %v, %v",
+                               test.in, out, err, test.out, test.err)
+               }
+       }
+}
+
 func TestParseUint64(t *testing.T) {
        for i := range atoui64tests {
                test := &atoui64tests[i]
@@ -257,6 +268,17 @@ func TestParseUint64Base(t *testing.T) {
        }
 }
 
+func TestParseInt32(t *testing.T) {
+       for i := range atoi32tests {
+               test := &atoi32tests[i]
+               out, err := ParseInt(test.in, 10, 32)
+               if int64(test.out) != out || !reflect.DeepEqual(test.err, err) {
+                       t.Errorf("Atoi32(%q) = %v, %v want %v, %v",
+                               test.in, out, err, test.out, test.err)
+               }
+       }
+}
+
 func TestParseInt64(t *testing.T) {
        for i := range atoi64tests {
                test := &atoi64tests[i]