Skip to content

Commit 097fdb8

Browse files
12wrigjacopybara-github
authored andcommitted
Improve error messaging when detecting and erroring out on integer overflow of byte count limit variables.
PiperOrigin-RevId: 728362523
1 parent a717e24 commit 097fdb8

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

java/core/src/main/java/com/google/protobuf/CodedInputStream.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -1184,7 +1184,8 @@ public int pushLimit(int byteLimit) throws InvalidProtocolBufferException {
11841184
}
11851185
byteLimit += getTotalBytesRead();
11861186
if (byteLimit < 0) {
1187-
throw InvalidProtocolBufferException.parseFailure();
1187+
// Check for for integer overflow in byteLimit
1188+
throw InvalidProtocolBufferException.sizeLimitExceeded();
11881189
}
11891190
final int oldLimit = currentLimit;
11901191
if (byteLimit > oldLimit) {
@@ -2679,7 +2680,8 @@ public int pushLimit(int byteLimit) throws InvalidProtocolBufferException {
26792680
}
26802681
byteLimit += totalBytesRetired + pos;
26812682
if (byteLimit < 0) {
2682-
throw InvalidProtocolBufferException.parseFailure();
2683+
// Check for for integer overflow in byteLimit
2684+
throw InvalidProtocolBufferException.sizeLimitExceeded();
26832685
}
26842686
final int oldLimit = currentLimit;
26852687
if (byteLimit > oldLimit) {

java/core/src/main/java/com/google/protobuf/InvalidProtocolBufferException.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,9 @@ static InvalidProtocolBufferException recursionLimitExceeded() {
137137

138138
static InvalidProtocolBufferException sizeLimitExceeded() {
139139
return new InvalidProtocolBufferException(
140-
"Protocol message was too large. May be malicious. "
141-
+ "Use CodedInputStream.setSizeLimit() to increase the size limit.");
140+
"Protocol message was too large. May be malicious. Use CodedInputStream.setSizeLimit() to"
141+
+ " increase the size limit. If reading multiple messages, consider resetting the"
142+
+ " counter between each message using CodedInputStream.resetSizeCounter().");
142143
}
143144

144145
static InvalidProtocolBufferException parseFailure() {

0 commit comments

Comments
 (0)