Skip to content

Commit a93ec15

Browse files
committed
check if maxMessageSize is negative
1 parent 970b47a commit a93ec15

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

common/src/main/java/org/tron/core/config/args/NodeConfig.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.tron.core.config.args;
22

33
import static org.tron.core.config.Parameter.ChainConstant.MAX_ACTIVE_WITNESS_NUM;
4+
import static org.tron.core.exception.TronError.ErrCode.PARAMETER_INIT;
45

56
import com.typesafe.config.Config;
67
import com.typesafe.config.ConfigBeanFactory;
@@ -10,6 +11,7 @@
1011
import lombok.Getter;
1112
import lombok.Setter;
1213
import lombok.extern.slf4j.Slf4j;
14+
import org.tron.core.exception.TronError;
1315

1416
// Node configuration bean for the "node" section of config.conf.
1517
// ConfigBeanFactory auto-binds all fields including sub-beans, dot-notation keys,
@@ -450,6 +452,20 @@ private void postProcess() {
450452
if (maxTrxCacheSize < 2000) {
451453
maxTrxCacheSize = 2000;
452454
}
455+
456+
// maxMessageSize: reject negative values
457+
if (rpc.maxMessageSize < 0) {
458+
throw new TronError("node.rpc.maxMessageSize must be non-negative, got: "
459+
+ rpc.maxMessageSize, PARAMETER_INIT);
460+
}
461+
if (http.maxMessageSize < 0) {
462+
throw new TronError("node.http.maxMessageSize must be non-negative, got: "
463+
+ http.maxMessageSize, PARAMETER_INIT);
464+
}
465+
if (jsonrpc.maxMessageSize < 0) {
466+
throw new TronError("node.jsonrpc.maxMessageSize must be non-negative, got: "
467+
+ jsonrpc.maxMessageSize, PARAMETER_INIT);
468+
}
453469
}
454470

455471
// ===========================================================================

framework/src/test/java/org/tron/core/config/args/ArgsTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,46 @@ public void testMaxMessageSizePureNumber() {
540540
Args.clearParam();
541541
}
542542

543+
@Test
544+
public void testMaxMessageSizeNegativeValueRejected() {
545+
// Negative maxMessageSize must be rejected at startup which threw TronError(PARAMETER_INIT)
546+
// for negative values).
547+
for (String key : new String[]{
548+
"node.rpc.maxMessageSize", "node.http.maxMessageSize", "node.jsonrpc.maxMessageSize"}) {
549+
Map<String, String> configMap = new HashMap<>();
550+
configMap.put("storage.db.directory", "database");
551+
configMap.put(key, "-1");
552+
Config config = ConfigFactory.defaultOverrides()
553+
.withFallback(ConfigFactory.parseMap(configMap))
554+
.withFallback(ConfigFactory.defaultReference());
555+
try {
556+
Args.applyConfigParams(config);
557+
Assert.fail("Expected TronError for negative " + key);
558+
} catch (TronError e) {
559+
Assert.assertEquals(TronError.ErrCode.PARAMETER_INIT, e.getErrCode());
560+
}
561+
Args.clearParam();
562+
}
563+
}
564+
565+
@Test
566+
public void testRpcMaxMessageSizeExceedsIntMax() {
567+
// HOCON's Config.getInt() throws when a numeric value exceeds int range.
568+
// This documents the failure mode for node.rpc.maxMessageSize (int field).
569+
Map<String, Object> configMap = new HashMap<>();
570+
configMap.put("storage.db.directory", "database");
571+
configMap.put("node.rpc.maxMessageSize", (long) Integer.MAX_VALUE + 1);
572+
Config config = ConfigFactory.defaultOverrides()
573+
.withFallback(ConfigFactory.parseMap(configMap))
574+
.withFallback(ConfigFactory.defaultReference());
575+
try {
576+
Args.applyConfigParams(config);
577+
Assert.fail("Expected RuntimeException for maxMessageSize > Integer.MAX_VALUE");
578+
} catch (RuntimeException e) {
579+
// ConfigBeanFactory/HOCON throws when binding a long out of int range
580+
}
581+
}
582+
543583
// ===== checkBackupMembers() tests =====
544584

545585
@Test

0 commit comments

Comments
 (0)