This is an automated email from the ASF dual-hosted git repository.
ShannonDing pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/rocketmq.git
The following commit(s) were added to refs/heads/develop by this push:
new 8764dd7b6a fix(remoting): match every address for a /0 CIDR in
ipInCIDR (#11578)
8764dd7b6a is described below
commit 8764dd7b6a469eb7783401d749c60240e6814b70
Author: zhzh12312312 <[email protected]>
AuthorDate: Mon Nov 23 21:22:23 2026 +0800
fix(remoting): match every address for a /0 CIDR in ipInCIDR (#11578)
A /0 prefix (e.g. 0.0.0.0/0) is meant to match every IPv4 address, but
RemotingHelper.ipInCIDR computed the mask as 0xFFFFFFFF << (32 - netId).
Java reduces an int shift count modulo 32, so for netId == 0 the shift
became << 0 and produced a mask of -1, causing 0.0.0.0/0 to match only
0.0.0.0. Handle /0 explicitly so the mask is 0, consistent with
IPAddressUtils.isIPInRange.
Add RemotingHelperTest covering /0, /32, /24, /8 and host-bit masking.
Co-authored-by: zhzh12312312 <[email protected]>
Co-authored-by: Claude Code <[email protected]>
Co-authored-by: dinglei <[email protected]>
---
.../rocketmq/remoting/common/RemotingHelper.java | 6 +++-
.../remoting/common/RemotingHelperTest.java | 32 ++++++++++++++++++++--
2 files changed, 35 insertions(+), 3 deletions(-)
diff --git
a/remoting/src/main/java/org/apache/rocketmq/remoting/common/RemotingHelper.java
b/remoting/src/main/java/org/apache/rocketmq/remoting/common/RemotingHelper.java
index aa0e97bb58..eb51fc6f41 100644
---
a/remoting/src/main/java/org/apache/rocketmq/remoting/common/RemotingHelper.java
+++
b/remoting/src/main/java/org/apache/rocketmq/remoting/common/RemotingHelper.java
@@ -302,7 +302,11 @@ public class RemotingHelper {
int ipAddr = ipToInt(ip);
String[] cidrArr = cidr.split("/");
int netId = Integer.parseInt(cidrArr[1]);
- int mask = 0xFFFFFFFF << (32 - netId);
+ // A /0 prefix (e.g. 0.0.0.0/0) is meant to match every address. Java
takes an int
+ // shift count modulo 32, so "0xFFFFFFFF << 32" is evaluated as
"0xFFFFFFFF << 0"
+ // == -1 (compare all 32 bits) and would never match. Handle /0
explicitly so the
+ // mask is 0 and every address matches.
+ int mask = netId == 0 ? 0 : (0xFFFFFFFF << (32 - netId));
int cidrIpAddr = ipToInt(cidrArr[0]);
return (ipAddr & mask) == (cidrIpAddr & mask);
diff --git
a/remoting/src/test/java/org/apache/rocketmq/remoting/common/RemotingHelperTest.java
b/remoting/src/test/java/org/apache/rocketmq/remoting/common/RemotingHelperTest.java
index 640e4100ef..579fb0e672 100644
---
a/remoting/src/test/java/org/apache/rocketmq/remoting/common/RemotingHelperTest.java
+++
b/remoting/src/test/java/org/apache/rocketmq/remoting/common/RemotingHelperTest.java
@@ -14,7 +14,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
package org.apache.rocketmq.remoting.common;
import java.net.InetSocketAddress;
@@ -27,7 +26,6 @@ import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class RemotingHelperTest {
-
private static final int SMALL_BUF = 16 * 1024;
private static final int LARGE_BUF = 256 * 1024;
@@ -76,4 +74,34 @@ public class RemotingHelperTest {
NettySystemConfig.socketRcvbufSize = originalRcv;
}
}
+ @Test
+ public void testIpInCIDRWithZeroPrefixMatchesEveryAddress() {
+ // A /0 prefix is the catch-all CIDR and must match every IPv4 address,
+ // mirroring IPAddressUtils.isIPInRange(ip, "0.0.0.0/0").
+ assertThat(RemotingHelper.ipInCIDR("0.0.0.0", "0.0.0.0/0")).isTrue();
+ assertThat(RemotingHelper.ipInCIDR("10.0.0.1", "0.0.0.0/0")).isTrue();
+ assertThat(RemotingHelper.ipInCIDR("192.168.1.1",
"0.0.0.0/0")).isTrue();
+ assertThat(RemotingHelper.ipInCIDR("255.255.255.255",
"0.0.0.0/0")).isTrue();
+ }
+
+ @Test
+ public void testIpInCIDRWithFullPrefixRequiresExactAddress() {
+ assertThat(RemotingHelper.ipInCIDR("192.168.1.1",
"192.168.1.1/32")).isTrue();
+ assertThat(RemotingHelper.ipInCIDR("192.168.1.2",
"192.168.1.1/32")).isFalse();
+ }
+
+ @Test
+ public void testIpInCIDRWithNormalPrefix() {
+ assertThat(RemotingHelper.ipInCIDR("192.168.1.100",
"192.168.1.0/24")).isTrue();
+ assertThat(RemotingHelper.ipInCIDR("192.168.2.1",
"192.168.1.0/24")).isFalse();
+ assertThat(RemotingHelper.ipInCIDR("10.1.2.3", "10.0.0.0/8")).isTrue();
+ assertThat(RemotingHelper.ipInCIDR("11.1.2.3",
"10.0.0.0/8")).isFalse();
+ }
+
+ @Test
+ public void testIpInCIDRMasksHostBitsOfCidr() {
+ // Host bits of the CIDR must be ignored: 192.168.1.5/24 behaves like
192.168.1.0/24.
+ assertThat(RemotingHelper.ipInCIDR("192.168.1.200",
"192.168.1.5/24")).isTrue();
+ assertThat(RemotingHelper.ipInCIDR("192.168.2.200",
"192.168.1.5/24")).isFalse();
+ }
}