From 7ca5fabb0759ff8da8f35a88d4a8ccf1ab8a8a0c Mon Sep 17 00:00:00 2001 From: Fufu Date: Mon, 18 Mar 2024 12:30:08 +0800 Subject: [PATCH] Fix IPv4 boundary checking --- ips.go | 2 +- ips_test.go | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/ips.go b/ips.go index 333054f..499b741 100644 --- a/ips.go +++ b/ips.go @@ -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 } } diff --git a/ips_test.go b/ips_test.go index 7c392eb..020f81f 100644 --- a/ips_test.go +++ b/ips_test.go @@ -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")) @@ -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"))