Skip to content

Commit

Permalink
netutil: fix ip parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Oct 3, 2024
1 parent d912957 commit 0749626
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
15 changes: 5 additions & 10 deletions netutil/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ func CloneIPs(ips []net.IP) (clone []net.IP) {
func IPAndPortFromAddr(addr net.Addr) (ip net.IP, port uint16) {
switch addr := addr.(type) {
case *net.TCPAddr:
// #nosec G115 -- Assume that ports always fit in uin16.
// #nosec G115 -- Assume that ports always fit in uint16.
return addr.IP, uint16(addr.Port)
case *net.UDPAddr:
// #nosec G115 -- Assume that ports always fit in uin16.
// #nosec G115 -- Assume that ports always fit in uint16.
return addr.IP, uint16(addr.Port)
}

Expand Down Expand Up @@ -288,17 +288,12 @@ func trimValidIPv6Field(s string, gotFields int, hasEllipsis bool) (withoutField
// address. It returns 0 if the field is invalid, due to an assumption that the
// field is not shorter than 1 rune.
func countIPv6FieldRunes(s string) (n int) {
value := uint32(0)
for n = range s {
b := fromHexByte(s[n])
if b == 0xff {
if fromHexByte(s[n]) == 0xff {
// Not a hex digit, return.
return n
}

value = value<<4 | uint32(b)
if value > math.MaxUint16 {
// Overflow.
} else if n > 3 {
// IPv6 label can't contain more than 4 hex digits.
return 0
}
}
Expand Down
23 changes: 12 additions & 11 deletions netutil/ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,24 +296,25 @@ func BenchmarkIsValidIPString(b *testing.B) {
})
}

// Most recent results:
//
// goos: darwin
// goarch: amd64
// pkg: github.com/AdguardTeam/golibs/netutil
// cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
// BenchmarkIsValidIPString/good_ipv4-12 35469972 33.93 ns/op 0 B/op 0 allocs/op
// BenchmarkIsValidIPString/good_ipv4_long-12 23231380 51.57 ns/op 0 B/op 0 allocs/op
// BenchmarkIsValidIPString/good_ipv6-12 24500007 46.62 ns/op 0 B/op 0 allocs/op
// BenchmarkIsValidIPString/good_ipv6_long-12 9725222 126.7 ns/op 0 B/op 0 allocs/op
// BenchmarkIsValidIPString/not_ip-12 170261018 7.078 ns/op 0 B/op 0 allocs/op
// BenchmarkIsValidIPString/zeroes-12 6189428 189.4 ns/op 0 B/op 0 allocs/op
// BenchmarkIsValidIPString/bad_ipv4-12 44662938 26.39 ns/op 0 B/op 0 allocs/op
// BenchmarkIsValidIPString/bad_ipv4_long-12 21964128 49.89 ns/op 0 B/op 0 allocs/op
// BenchmarkIsValidIPString/bad_ipv6-12 22520074 53.08 ns/op 0 B/op 0 allocs/op
// BenchmarkIsValidIPString/bad_ipv6_long-12 8275022 147.3 ns/op 0 B/op 0 allocs/op
// BenchmarkIsValidIPString/good_ipv4-12 30707500 35.37 ns/op 0 B/op 0 allocs/op
// BenchmarkIsValidIPString/good_ipv4_long-12 24959916 49.38 ns/op 0 B/op 0 allocs/op
// BenchmarkIsValidIPString/good_ipv6-12 25886067 46.94 ns/op 0 B/op 0 allocs/op
// BenchmarkIsValidIPString/good_ipv6_long-12 8827570 137.2 ns/op 0 B/op 0 allocs/op
// BenchmarkIsValidIPString/not_ip-12 196169263 6.420 ns/op 0 B/op 0 allocs/op
// BenchmarkIsValidIPString/zeroes-12 163102160 6.250 ns/op 0 B/op 0 allocs/op
// BenchmarkIsValidIPString/bad_ipv4-12 45634869 25.53 ns/op 0 B/op 0 allocs/op
// BenchmarkIsValidIPString/bad_ipv4_long-12 25619106 49.08 ns/op 0 B/op 0 allocs/op
// BenchmarkIsValidIPString/bad_ipv6-12 22189162 50.77 ns/op 0 B/op 0 allocs/op
// BenchmarkIsValidIPString/bad_ipv6_long-12 7907097 150.1 ns/op 0 B/op 0 allocs/op
}

func FuzzIsValidIPString(f *testing.F) {
// TODO(e.burkov): !! Fix!
for _, seed := range []string{
"",
" ",
Expand Down

0 comments on commit 0749626

Please sign in to comment.