apache / apache/rocketmq

[Bug] NullPointerException in AdminBrokerProcessor.updateAndCreateTopicList at line 636

Open
#10,008 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
22.6k
Forks
12k
Avg merge
2d 20h
Merged PRs (30d)
26

Description

### Before Creating the Bug Report

- [x] I found a bug, not just asking a question, which should be created in [GitHub Discussions](https://github.com/apache/rocketmq/discussions).

- [x] I have searched the [GitHub Issues](https://github.com/apache/rocketmq/issues) and [GitHub Discussions](https://github.com/apache/rocketmq/discussions) of this repository and believe that this is not a duplicate.

- [x] I have confirmed that this bug belongs to the current repository, not other repositories of RocketMQ.

### Runtime platform environment

OS name: "linux", version: "5.15.0-138-generic", arch: "amd64", family: "unix"

### RocketMQ version

version: rocketmq-all-5.4.0
link: https://github.com/apache/rocketmq/archive/refs/tags/rocketmq-all-5.4.0.zip

### JDK Version

Java version: 1.8.0_452
Build tool: Apache Maven 3.9.1

### Describe the Bug

A NullPointerException occurs when invoking AdminBrokerProcessor.processRequest with a RemotingCommand created via RemotingCommand.createRequestCommand(...) that has a null body. The issue arises because the method CreateTopicListRequestBody.decode(...) returns null when the input byte array is null, and the subsequent call to requestBody.getTopicConfigList() dereferences this null value.

### Steps to Reproduce

Test Code:

```java
package org.apache.rocketmq.broker.processor;
import org.apache.rocketmq.broker.processor.AdminBrokerProcessor;
import org.junit.Test;
import org.apache.rocketmq.broker.BrokerController;
import io.netty.channel.ChannelHandlerContext;
import org.apache.rocketmq.remoting.protocol.RemotingCommand;
import org.apache.rocketmq.remoting.protocol.RequestCode;
import org.apache.rocketmq.common.BrokerConfig;
import org.apache.rocketmq.common.MixAll;
import org.apache.rocketmq.remoting.netty.NettyClientConfig;
import org.apache.rocketmq.remoting.netty.NettyServerConfig;
import org.apache.rocketmq.store.config.MessageStoreConfig;

public class TestClass {
@Test(timeout=3000)
public void test() throws Throwable {
BrokerConfig brokerConfig = new BrokerConfig();
NettyServerConfig nettyServerConfig = new NettyServerConfig();
NettyClientConfig nettyClientConfig = new NettyClientConfig();
MessageStoreConfig messageStoreConfig = new MessageStoreConfig();
BrokerController brokerController = new BrokerController(brokerConfig, nettyServerConfig, nettyClientConfig, messageStoreConfig);
AdminBrokerProcessor adminBrokerProcessor0 = new AdminBrokerProcessor(brokerController);
RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.UPDATE_AND_CREATE_TOPIC_LIST, null);
adminBrokerProcessor0.processRequest((ChannelHandlerContext) null, request);
}
}
```

Execution Result:

```
JUnit version 4.13.2
.E
Time: 0.869
There was 1 failure:
1) test(org.apache.rocketmq.broker.processor.TestClass)
java.lang.NullPointerException
at org.apache.rocketmq.broker.processor.AdminBrokerProcessor.updateAndCreateTopicList(AdminBrokerProcessor.java:636)
at org.apache.rocketmq.broker.processor.AdminBrokerProcessor.processRequest(AdminBrokerProcessor.java:269)
at org.apache.rocketmq.broker.processor.TestClass.test(TestClass.java:24)

FAILURES!!!
Tests run: 1, Failures: 1
```

### What Did You Expect to See?

The method should either:

- Validate that the request body is non-null before decoding, or
- Handle the case where the decoded CreateTopicListRequestBody is null gracefully (e.g., return an error response).

### What Did You See Instead?

A NPE is thrown:

```
java.lang.NullPointerException
at org.apache.rocketmq.broker.processor.AdminBrokerProcessor.updateAndCreateTopicList(AdminBrokerProcessor.java:636)
at org.apache.rocketmq.broker.processor.AdminBrokerProcessor.processRequest(AdminBrokerProcessor.java:269)
at org.apache.rocketmq.broker.processor.TestClass.test(TestClass.java:24)
```

### Additional Context

Root Cause Analysis

1. RemotingCommand.createRequestCommand(...) does not initialize the body field.

2. In updateAndCreateTopicList, the code calls:

```java
final CreateTopicListRequestBody requestBody = CreateTopicListRequestBody.decode(request.getBody(), CreateTopicListRequestBody.class);
```

Since request.getBody() returns null, decode(...) returns null.

3. The next line unconditionally accesses:

```java
List topicConfigList = requestBody.getTopicConfigList(); // NPE here
```

Contributor guide

Open the contributing guide

Research direction

Start in AdminBrokerProcessor.updateAndCreateTopicList at line 636 and inspect CreateTopicListRequestBody.decode with a null request body. Run the supplied JUnit reproduction through AdminBrokerProcessor.processRequest. Done means a request created without a body no longer throws a NullPointerException and is handled gracefully, such as by returning an error response.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.