From 3ef0e8f8235fe938ad5d1c99859cc63470877ec7 Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Fri, 2 Sep 2016 01:14:57 -0700 Subject: [PATCH] net: remove parsing of negative decimals in IPv4 literal 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 Reviewed-by: Mikio Hara Run-TryBot: Joe Tsai --- src/net/ip_test.go | 10 ++++++++++ src/net/parse.go | 14 +------------- src/net/parse_test.go | 5 ++--- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/net/ip_test.go b/src/net/ip_test.go index 0ef46ee3340..46551633ce2 100644 --- a/src/net/ip_test.go +++ b/src/net/ip_test.go @@ -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: ""}}, } diff --git a/src/net/parse.go b/src/net/parse.go index 363c83f6ce2..d615eb2b567 100644 --- a/src/net/parse.go +++ b/src/net/parse.go @@ -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 } diff --git a/src/net/parse_test.go b/src/net/parse_test.go index 5c1c88cacda..c5f8bfd198c 100644 --- a/src/net/parse_test.go +++ b/src/net/parse_test.go @@ -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 {