Skip to content

Commit

Permalink
Fix IPv4 boundary checking
Browse files Browse the repository at this point in the history
  • Loading branch information
fufuok committed Mar 18, 2024
1 parent dbfbf0b commit 7ca5fab
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ips.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func IsIPv4(s string) bool {

for ci = 0; ci < len(s) && '0' <= s[ci] && s[ci] <= '9'; ci++ {
n = n*10 + int(s[ci]-'0')
if n >= 0xFF {
if n > 0xFF {
return false
}
}
Expand Down
2 changes: 2 additions & 0 deletions ips_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
func Test_IsIPv4(t *testing.T) {
t.Parallel()

require.Equal(t, true, IsIPv4("255.255.255.255"))
require.Equal(t, true, IsIPv4("174.23.33.100"))
require.Equal(t, true, IsIPv4("127.0.0.1"))
require.Equal(t, true, IsIPv4("0.0.0.0"))
Expand All @@ -30,6 +31,7 @@ func Test_IsIPv4(t *testing.T) {
require.Equal(t, false, IsIPv4("189.12.34.260"))
require.Equal(t, false, IsIPv4("189.12.260.260"))
require.Equal(t, false, IsIPv4("189.260.260.260"))
require.Equal(t, false, IsIPv4("255.255.255.256"))
require.Equal(t, false, IsIPv4("999.999.999.999"))
require.Equal(t, false, IsIPv4("9999.9999.9999.9999"))

Expand Down

0 comments on commit 7ca5fab

Please # to comment.