]> Cypherpunks repositories - gostls13.git/commitdiff
net: remove parsing of negative decimals in IPv4 literal
authorJoe Tsai <joetsai@digital-static.net>
Fri, 2 Sep 2016 08:14:57 +0000 (01:14 -0700)
committerJoe Tsai <thebrokentoaster@gmail.com>
Wed, 7 Sep 2016 17:48:45 +0000 (17:48 +0000)
https://golang.org/cl/27206 fixed the dtoi function such that
it now properly parses negative number. Ironically, this causes
several other functions that depended on dtoi to now (incorrectly)
parse negative numbers.

For example, ParseCIDR("-1.0.0.0/32") used to be rejected prior to the
above CL, but is now accepted even though it is an invalid CIDR notation.
This CL fixes that regression.

We fix this by removing the signed parsing logic entirely from dtoi.
It was introduced relatively recently in https://golang.org/cl/12447
to fix a bug where an invalid port was improperly being parsed as OK.
It seems to me that the fix in that CL to the port handling logic was
sufficient such that a change to dtoi was unnecessary.

Updates #16350

Change-Id: I414bb1aa27d0a226ebd4b05a09cb40d784691b43
Reviewed-on: https://go-review.googlesource.com/28414
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Mikio Hara <mikioh.mikioh@gmail.com>
Run-TryBot: Joe Tsai <thebrokentoaster@gmail.com>

src/net/ip_test.go
src/net/parse.go
src/net/parse_test.go

index 0ef46ee33403a7eab87604aca8473f1a579fffe2..46551633ce2369b43600079a289764f1578a34ad 100644 (file)
@@ -28,6 +28,10 @@ var parseIPTests = []struct {
        {"2001:4860:0:2001::68", IP{0x20, 0x01, 0x48, 0x60, 0, 0, 0x20, 0x01, 0, 0, 0, 0, 0, 0, 0x00, 0x68}},
        {"2001:4860:0000:2001:0000:0000:0000:0068", IP{0x20, 0x01, 0x48, 0x60, 0, 0, 0x20, 0x01, 0, 0, 0, 0, 0, 0, 0x00, 0x68}},
 
+       {"-0.0.0.0", nil},
+       {"0.-1.0.0", nil},
+       {"0.0.-2.0", nil},
+       {"0.0.0.-3", nil},
        {"127.0.0.256", nil},
        {"abc", nil},
        {"123:", nil},
@@ -332,6 +336,12 @@ var parseCIDRTests = []struct {
        {"192.168.1.1/255.255.255.0", nil, nil, &ParseError{Type: "CIDR address", Text: "192.168.1.1/255.255.255.0"}},
        {"192.168.1.1/35", nil, nil, &ParseError{Type: "CIDR address", Text: "192.168.1.1/35"}},
        {"2001:db8::1/-1", nil, nil, &ParseError{Type: "CIDR address", Text: "2001:db8::1/-1"}},
+       {"2001:db8::1/-0", nil, nil, &ParseError{Type: "CIDR address", Text: "2001:db8::1/-0"}},
+       {"-0.0.0.0/32", nil, nil, &ParseError{Type: "CIDR address", Text: "-0.0.0.0/32"}},
+       {"0.-1.0.0/32", nil, nil, &ParseError{Type: "CIDR address", Text: "0.-1.0.0/32"}},
+       {"0.0.-2.0/32", nil, nil, &ParseError{Type: "CIDR address", Text: "0.0.-2.0/32"}},
+       {"0.0.0.-3/32", nil, nil, &ParseError{Type: "CIDR address", Text: "0.0.0.-3/32"}},
+       {"0.0.0.0/-0", nil, nil, &ParseError{Type: "CIDR address", Text: "0.0.0.0/-0"}},
        {"", nil, nil, &ParseError{Type: "CIDR address", Text: ""}},
 }
 
index 363c83f6ce28781e8483c7c8be08d375ae87e676..d615eb2b567418e567411996528b2824dbda757d 100644 (file)
@@ -127,26 +127,14 @@ const big = 0xFFFFFF
 // Returns number, characters consumed, success.
 func dtoi(s string) (n int, i int, ok bool) {
        n = 0
-       neg := false
-       if len(s) > 0 && s[0] == '-' {
-               neg = true
-               s = s[1:]
-       }
        for i = 0; i < len(s) && '0' <= s[i] && s[i] <= '9'; i++ {
                n = n*10 + int(s[i]-'0')
                if n >= big {
-                       if neg {
-                               return -big, i + 1, false
-                       }
                        return big, i, false
                }
        }
        if i == 0 {
-               return 0, i, false
-       }
-       if neg {
-               n = -n
-               i++
+               return 0, 0, false
        }
        return n, i, true
 }
index 5c1c88cacda4f381129d763a42bb2bfa71a87927..c5f8bfd198ce4f519beae58f25407ffe747b44cb 100644 (file)
@@ -86,12 +86,11 @@ func TestDtoi(t *testing.T) {
                ok  bool
        }{
                {"", 0, 0, false},
-
-               {"-123456789", -big, 9, false},
-               {"-1", -1, 2, true},
                {"0", 0, 1, true},
                {"65536", 65536, 5, true},
                {"123456789", big, 8, false},
+               {"-0", 0, 0, false},
+               {"-1234", 0, 0, false},
        } {
                n, i, ok := dtoi(tt.in)
                if n != tt.out || i != tt.off || ok != tt.ok {