[ISSUE #10207] Fix potential NPE in HA connections when remote address is null#10209
Open
daguimu wants to merge 1 commit intoapache:developfrom
Open
[ISSUE #10207] Fix potential NPE in HA connections when remote address is null#10209daguimu wants to merge 1 commit intoapache:developfrom
daguimu wants to merge 1 commit intoapache:developfrom
Conversation
…address is null getRemoteSocketAddress() can return null if the socket is not connected. Add null check before calling toString() in DefaultHAConnection and AutoSwitchHAConnection constructors to prevent NullPointerException. Fixes apache#10207
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
In
DefaultHAConnectionandAutoSwitchHAConnection,.toString()is called directly on the return value ofgetRemoteSocketAddress()without a null check:Socket.getRemoteSocketAddress()returnsnullif the socket is not connected. Under network instability (e.g., slave connects and immediately disconnects before the HA connection constructor completes), this throwsNullPointerException.Root Cause
No null check on the return value of
getRemoteSocketAddress()before calling.toString().Fix
Extract the
SocketAddressinto a local variable and check for null before calling.toString():Applied to both:
DefaultHAConnection.java:64AutoSwitchHAConnection.java:109Tests Added
testConstructorWithNullRemoteAddress— Creates aDefaultHAConnectionwith a non-connectedSocketChannel(wheregetRemoteSocketAddress()returns null). Verifies no NPE is thrown andclientAddressis set to empty string.Impact
Fixes #10207