fix: guard NPE in NetworkUtil and concurrent read in IndexService - #10743
Open
btlqql wants to merge 1 commit into
Open
fix: guard NPE in NetworkUtil and concurrent read in IndexService#10743btlqql wants to merge 1 commit into
btlqql wants to merge 1 commit into
Conversation
- NetworkUtil.socketAddress2String: InetSocketAddress.getAddress() returns null when the hostname is unresolved, so getAddress().getHostAddress() threw NPE; fall back to getHostString() when the address is null - NetworkUtil.string2SocketAddress: an address without ':' made lastIndexOf return -1 and substring(0, -1) threw StringIndexOutOfBoundsException; throw a clear IllegalArgumentException - IndexService.getTotalSize: indexFileList is a plain ArrayList guarded by readWriteLock everywhere else, but getTotalSize read it without the lock, so a concurrent destroy()/deleteExpiredFile() could clear it between the isEmpty() check and get(0), causing IndexOutOfBoundsException; acquire the read lock Compiled and verified on the build server (mvn -pl broker -am compile).
RockteMQ-AI
approved these changes
Aug 1, 2026
RockteMQ-AI
left a comment
Contributor
There was a problem hiding this comment.
Review by github-manager-bot
Summary
Guards against NPE in NetworkUtil.socketAddress2String when hostname is unresolved, adds input validation in string2SocketAddress, and fixes a race condition in IndexService.getTotalSize() by acquiring the read lock.
Findings
- [Info]
NetworkUtil.java:195— The newIllegalArgumentExceptionfor missing ':' is a good defensive check. Consider including the original address string in the error message (already done — 👍). - [Info]
NetworkUtil.java:207— The null-guard ongetAddress()with fallback togetHostString()correctly handles unresolved hostnames. This prevents NPE in broker/namesrv paths that use this utility. - [Info]
IndexService.java:95-103— WrappinggetTotalSize()in the read lock correctly prevents the race betweenisEmpty()check andget(0)call. The existingreadWriteLockis the right choice here.
Assessment
Clean, well-scoped bug fix. All three changes are correct and minimal. No backward compatibility concerns.
Automated review by github-manager-bot
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.
Motivation
Three edge-case bugs found by code review (verified against the current develop branch):
NetworkUtil.socketAddress2StringNPE (common/src/main/java/org/apache/rocketmq/common/utils/NetworkUtil.java)InetSocketAddress.getAddress()returnsnullwhen the hostname could not be resolved, sogetAddress().getHostAddress()threwNullPointerException. This util is used across broker/namesrv/tools. Fall back togetHostString()when the address is null.NetworkUtil.string2SocketAddressStringIndexOutOfBoundsExceptionAn address without
:makeslastIndexOf(":")return-1, andsubstring(0, -1)throwsStringIndexOutOfBoundsException. Throw a clearIllegalArgumentExceptioninstead.IndexService.getTotalSizeconcurrent read (store/src/main/java/org/apache/rocketmq/store/index/IndexService.java)indexFileListis a plainArrayListguarded byreadWriteLockin every other accessor, butgetTotalSizeread it without the lock. A concurrentdestroy()/deleteExpiredFile()could clear the list between theisEmpty()check andget(0), causingIndexOutOfBoundsException. Acquire the read lock.Verification
mvn -pl broker -am compilepasses on the build server.Diff
2 files changed, +15 / -5.