Skip to content

fix: guard NPE in NetworkUtil and concurrent read in IndexService - #10743

Open
btlqql wants to merge 1 commit into
apache:developfrom
btlqql:fix/networkutil-and-indexservice-guards
Open

fix: guard NPE in NetworkUtil and concurrent read in IndexService#10743
btlqql wants to merge 1 commit into
apache:developfrom
btlqql:fix/networkutil-and-indexservice-guards

Conversation

@btlqql

@btlqql btlqql commented Aug 1, 2026

Copy link
Copy Markdown

Motivation

Three edge-case bugs found by code review (verified against the current develop branch):

  1. NetworkUtil.socketAddress2String NPE (common/src/main/java/org/apache/rocketmq/common/utils/NetworkUtil.java)
    InetSocketAddress.getAddress() returns null when the hostname could not be resolved, so getAddress().getHostAddress() threw NullPointerException. This util is used across broker/namesrv/tools. Fall back to getHostString() when the address is null.

  2. NetworkUtil.string2SocketAddress StringIndexOutOfBoundsException
    An address without : makes lastIndexOf(":") return -1, and substring(0, -1) throws StringIndexOutOfBoundsException. Throw a clear IllegalArgumentException instead.

  3. IndexService.getTotalSize concurrent read (store/src/main/java/org/apache/rocketmq/store/index/IndexService.java)
    indexFileList is a plain ArrayList guarded by readWriteLock in every other accessor, but getTotalSize read it without the lock. A concurrent destroy()/deleteExpiredFile() could clear the list between the isEmpty() check and get(0), causing IndexOutOfBoundsException. Acquire the read lock.

Verification

mvn -pl broker -am compile passes on the build server.

Diff

2 files changed, +15 / -5.

- 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 RockteMQ-AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 new IllegalArgumentException for 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 on getAddress() with fallback to getHostString() correctly handles unresolved hostnames. This prevents NPE in broker/namesrv paths that use this utility.
  • [Info] IndexService.java:95-103 — Wrapping getTotalSize() in the read lock correctly prevents the race between isEmpty() check and get(0) call. The existing readWriteLock is 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants