[ISSUE #10591] Fix IPv6 NPE in NetworkUtil.string2SocketAddress by stripping brackets#10592
[ISSUE #10591] Fix IPv6 NPE in NetworkUtil.string2SocketAddress by stripping brackets#10592itxaiohanglover wants to merge 3 commits into
Conversation
…g brackets When a gRPC client connects via IPv6, the remote address is formatted as [240e:...:2]:44880. The string2SocketAddress method splits on the last colon, leaving the host part with surrounding brackets. Java's InetSocketAddress constructor fails to resolve the bracketed host, causing getAddress() to return null, which triggers a NullPointerException in CommitLog.asyncPutMessage. Strip the surrounding [] before constructing InetSocketAddress.
|
Thanks for the fix. Could you also check |
As noted in PR review, SimpleChannel#parseSocketAddress used
address.split(":") which fails for IPv6 addresses like
[240e:...:2]:44880 — split produces more than 2 segments so the
method returns null, causing bornSocketAddress to be null and
triggering the NPE in CommitLog.
Use lastIndexOf(":") to split host:port and strip brackets from
IPv6 host, consistent with the NetworkUtil.string2SocketAddress fix.
|
Thanks for pointing that out! You're right — Fixed in the latest push — switched to |
yx9o
left a comment
There was a problem hiding this comment.
Thanks for the update. It would be better to add a unit test for the SimpleChannel change.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## develop #10592 +/- ##
=============================================
- Coverage 48.26% 48.17% -0.09%
+ Complexity 13433 13412 -21
=============================================
Files 1378 1378
Lines 100817 100823 +6
Branches 13040 13042 +2
=============================================
- Hits 48660 48573 -87
- Misses 46211 46293 +82
- Partials 5946 5957 +11 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Cover IPv4, IPv6 with brackets, IPv6 loopback, null/empty input, and missing port cases to improve patch coverage.
RockteMQ-AI
left a comment
There was a problem hiding this comment.
Review by github-manager-bot
Summary
Fixes NPE when parsing IPv6 addresses in NetworkUtil.string2SocketAddress() and SimpleChannel.parseSocketAddress(). The root cause was using split(":") which breaks for IPv6 addresses containing multiple colons. The fix uses lastIndexOf(":") to correctly split host and port, and strips square brackets from IPv6 addresses.
Findings
-
[Info] Good fix — switching from
split(":")tolastIndexOf(":")correctly handles full IPv6 addresses like[240e:341:6246:c700:c4ad:e645:459e:2]:44880. The bracket stripping logic is also correct. -
[Info] The
SimpleChannelTestaddresses the unit test request from @yx9o's earlier review. Tests cover IPv4, IPv6 with brackets, IPv6 without brackets, and no-port edge cases. -
[Warning]
NetworkUtil.java:198andSimpleChannel.java:98—Integer.parseInt(port)will throwNumberFormatExceptionon malformed input (e.g.,"[::1]:abc"). Both methods currently let this propagate uncaught. Consider wrapping in a try-catch and returningnull(or logging a warning) for robustness, since these are utility methods that may receive untrusted input. -
[Info]
NetworkUtilTest.java— ThetestString2SocketAddressIPv6WithoutBracketstest uses"240e:341:6246:c700:c4ad:e645:459e:2:44880"which is ambiguous (the last colon could be part of the IPv6 address or the port separator). ThelastIndexOf(":")approach will treat44880as port and the rest as host, which is correct for this format. However, this is inherently fragile — consider documenting that IPv6 addresses without brackets are not reliably parseable.
Suggestions
- Add input validation or try-catch around
Integer.parseInt(port)in both methods. - Consider adding a Javadoc note on
string2SocketAddress()specifying the expected format:host:portfor IPv4,[host]:portfor IPv6.
Automated review by github-manager-bot
Which Issue(s) This PR Fixes
Fixes #10591
Brief Description
When a gRPC client connects to the Proxy via IPv6, the remote address is formatted as
[240e:...:2]:44880(with brackets). TheNetworkUtil.string2SocketAddressmethod splits on the last:to separate host and port, but the host part retains the surrounding[]brackets.When
new InetSocketAddress("[240e:...:2]", port)is constructed with a bracketed host, Java'sInetAddress.getByName()fails to resolve it, causinggetAddress()to returnnull. Thisnullpropagates toCommitLog.asyncPutMessage(line 994) wherebornSocketAddress.getAddress()triggers aNullPointerException.Root cause:
string2SocketAddressdoes not strip IPv6 brackets before constructingInetSocketAddress.Fix: Strip surrounding
[]from the host part before callingnew InetSocketAddress(host, port).How Did You Test This Change?
Added
NetworkUtilTestwith 5 test cases:testString2SocketAddressIPv4— existing IPv4 behavior unchangedtestString2SocketAddressIPv6WithBrackets— reproduces the issue:[240e:341:6246:c700:c4ad:e645:459e:2]:44880now resolves correctly with non-nullgetAddress()testString2SocketAddressIPv6Loopback—[::1]:8080resolves correctlytestSocketAddress2StringIPv4— round-trip conversion unchangedtestNormalizeAndDenormalizeHostAddress— bracket normalize/denormalize helpers