Repository navigation
Conversation
util.IPMatch panicked when either argument was not a valid IP address or CIDR. Enforce() recovered that panic, but callers received an error containing a full debug.Stack() dump, and direct callers of the public util.IPMatch helper crashed the process. This violated the convention documented on RegexMatch: an invalid pattern matches nothing and the Func wrapper reports it as an error (GlobMatch/GlobMatchFunc behave the same way). The logic now lives in an internal ipMatch() that returns an error; IPMatch keeps its bool signature and returns false on invalid input, while IPMatchFunc reports the error, matching RegexMatchFunc. Signed-off-by: NUAAwqy <162230229@nuaa.edu.cn>
util.IPMatch panicked when either argument was not a valid IP address or CIDR. Enforce() recovered that panic, but callers received an error containing a full debug.Stack() dump, and direct callers of the public util.IPMatch helper crashed the process. This violated the convention documented on RegexMatch: an invalid pattern matches nothing and the Func wrapper reports it as an error (GlobMatch/GlobMatchFunc behave the same way). The logic now lives in an internal ipMatch() that returns an error; IPMatch keeps its bool signature and returns false on invalid input, while IPMatchFunc reports the error, matching RegexMatchFunc. Signed-off-by: NUAAwqy <162230229@nuaa.edu.cn>
staticcheck ST1005 flags error strings ending with punctuation. Update the matching assertions in builtin_operators_test.go. Signed-off-by: NUAAwqy <162230229@nuaa.edu.cn>
Member
|
Thanks for reporting this and for the clear write-up. The same fix has been landed directly on master in c92cc6a: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
util.IPMatchpanics when either argument is not a valid IP address / CIDR:Two consequences:
m = ipMatch(r.ip, p.ip)), a user-supplied request value such as192.168.1.abcmakesEnforcereturnpanic: invalid argument: ...with a fulldebug.Stack()dump instead of a clean error.util.IPMatchhelper crash the process.Every sibling operator already follows the convention documented on
RegexMatch:GlobMatchFunclikewise propagates the glob error.IPMatchis the only operator in the package that panics (verified: the twopanic()calls inutilare both inIPMatch).Fix
ipMatch(ip1, ip2) (bool, error).IPMatchkeeps itsboolsignature, no longer panics, and returnsfalseon invalid input — same semantics asRegexMatchreturningfalsefor an invalid pattern.IPMatchFuncreturnsipMatch: <detail>as an error, matchingRegexMatchFunc/GlobMatchFunc.Tests
Added
TestIPMatchInvalidInput:IPMatch("192.168.1.abc", "192.168.1.0/24")→false(was a panic)IPMatch("192.168.2.123", "not-an-ip")→false(was a panic)IPMatchFuncon both inputs returnsipMatch: invalid argument: ...Full
go build ./...andgo test ./util/ .pass.Before:
Enforceerror waspanic: invalid argument: ip1 in IPMatch() function is not an IP address. <stack trace>After:
Enforceerror isipMatch: invalid argument: ip1 in IPMatch() function is not an IP address.