@@ -84,6 +84,21 @@ public class JsonFormat {
8484 private static final String EXPECTED_STRING = "Expected string." ;
8585 private static final String MISSING_END_QUOTE = "String missing ending quote." ;
8686
87+ // Bounds all work and error messages performed by parseInteger after tokenization.
88+ static final int MAX_INTEGER_TOKEN_LENGTH = 256 ;
89+
90+ // Enum identifiers are schema-defined and small. This is an input-contract and performance
91+ // limit, not the error-response bound; abbreviateIdentifier() bounds errors independently.
92+ // Keep it separate from generic identifiers, strings and bytes so large values remain valid.
93+ static final int MAX_ENUM_TOKEN_LENGTH = 256 ;
94+
95+ private static final int MAX_IDENTIFIER_ECHO_PREFIX_LENGTH = 64 ;
96+
97+ // Caps only the JDK NumberFormatException detail retained for integer syntax errors. The detail
98+ // may contain an input prefix. Supported JDKs emit shorter messages in tested cases, but their
99+ // message format is not a stable contract, so keep this limit as a defensive boundary.
100+ private static final int MAX_INTEGER_ERROR_DETAIL_PREFIX_LENGTH = 256 ;
101+
87102 public static final boolean ALWAYS_OUTPUT_DEFAULT_VALUE_FIELDS = true ;
88103 public static final Set <Class <? extends Message >> MESSAGES = ImmutableSet .of (
89104 BalanceContract .AccountBalanceResponse .class ,
@@ -768,6 +783,13 @@ private static Object handlePrimitive(Tokenizer tokenizer, FieldDescriptor field
768783 + number + "." );
769784 }
770785 } else {
786+ String enumToken = tokenizer .currentToken ();
787+ // Reject before case normalization and descriptor lookup. Error echo is bounded
788+ // independently by abbreviateIdentifier().
789+ if (enumToken .length () > MAX_ENUM_TOKEN_LENGTH ) {
790+ throw tokenizer .parseException ("Enum token is too long: length "
791+ + enumToken .length () + ", max " + MAX_ENUM_TOKEN_LENGTH );
792+ }
771793 String id = tokenizer .consumeIdentifier ();
772794 if (StringUtils .isAllLowerCase (id )) {
773795 char b = id .charAt (0 );
@@ -780,7 +802,7 @@ private static Object handlePrimitive(Tokenizer tokenizer, FieldDescriptor field
780802 throw tokenizer .parseExceptionPreviousToken ("Enum type \" "
781803 + enumType .getFullName ()
782804 + "\" has no value named \" "
783- + id + "\" ." );
805+ + abbreviateIdentifier ( id ) + "\" ." );
784806 }
785807 }
786808
@@ -1140,14 +1162,50 @@ static long parseUInt64(String text) throws NumberFormatException {
11401162 return parseInteger (text , false , true );
11411163 }
11421164
1165+ private static String truncate (String text , int maxPrefixLength ) {
1166+ if (text == null ) {
1167+ return "null" ;
1168+ }
1169+ if (text .length () <= maxPrefixLength ) {
1170+ return text ;
1171+ }
1172+ int prefixEnd = maxPrefixLength ;
1173+ if (prefixEnd > 0
1174+ && Character .isHighSurrogate (text .charAt (prefixEnd - 1 ))
1175+ && Character .isLowSurrogate (text .charAt (prefixEnd ))) {
1176+ --prefixEnd ;
1177+ }
1178+ return text .substring (0 , prefixEnd ) + "...(truncated)" ;
1179+ }
1180+
1181+ private static String integerOutOfRangeMessage (boolean isSigned , boolean isLong ) {
1182+ return "Number out of range for " + (isLong ? "64" : "32" ) + "-bit "
1183+ + (isSigned ? "signed" : "unsigned" ) + " integer." ;
1184+ }
1185+
1186+ private static String abbreviateIdentifier (String text ) {
1187+ return truncate (text , MAX_IDENTIFIER_ECHO_PREFIX_LENGTH );
1188+ }
1189+
1190+ private static String abbreviateIntegerError (String text ) {
1191+ return truncate (text , MAX_INTEGER_ERROR_DETAIL_PREFIX_LENGTH );
1192+ }
1193+
11431194 private static long parseInteger (String text , boolean isSigned , boolean isLong )
11441195 throws NumberFormatException {
1196+ // This must remain the first operation: no substring, large error message or BigInteger
1197+ // construction may happen before the raw token is bounded.
1198+ if (text .length () > MAX_INTEGER_TOKEN_LENGTH ) {
1199+ throw new NumberFormatException ("Integer token is too long: length " + text .length ()
1200+ + ", max " + MAX_INTEGER_TOKEN_LENGTH );
1201+ }
1202+
11451203 int pos = 0 ;
11461204
11471205 boolean negative = false ;
11481206 if (text .startsWith ("-" , pos )) {
11491207 if (!isSigned ) {
1150- throw new NumberFormatException ("Number must be positive: " + text );
1208+ throw new NumberFormatException ("Number must be positive." );
11511209 }
11521210 ++pos ;
11531211 negative = true ;
@@ -1177,17 +1235,17 @@ private static long parseInteger(String text, boolean isSigned, boolean isLong)
11771235 if (!isLong ) {
11781236 if (isSigned ) {
11791237 if ((result > Integer .MAX_VALUE ) || (result < Integer .MIN_VALUE )) {
1180- throw new NumberFormatException ("Number out of range for 32-bit signed integer: "
1181- + text );
1238+ throw new NumberFormatException (integerOutOfRangeMessage (isSigned , isLong ));
11821239 }
11831240 } else {
11841241 if ((result >= (1L << 32 )) || (result < 0 )) {
1185- throw new NumberFormatException ("Number out of range for 32-bit unsigned integer: "
1186- + text );
1242+ throw new NumberFormatException (integerOutOfRangeMessage (isSigned , isLong ));
11871243 }
11881244 }
11891245 }
11901246 } else {
1247+ // Preserve existing syntax behavior by passing numberText to BigInteger without additional
1248+ // normalization.
11911249 BigInteger bigValue = new BigInteger (numberText , radix );
11921250 if (negative ) {
11931251 bigValue = bigValue .negate ();
@@ -1197,25 +1255,21 @@ private static long parseInteger(String text, boolean isSigned, boolean isLong)
11971255 if (!isLong ) {
11981256 if (isSigned ) {
11991257 if (bigValue .bitLength () > 31 ) {
1200- throw new NumberFormatException ("Number out of range for 32-bit signed integer: "
1201- + text );
1258+ throw new NumberFormatException (integerOutOfRangeMessage (isSigned , isLong ));
12021259 }
12031260 } else {
12041261 if (bigValue .bitLength () > 32 ) {
1205- throw new NumberFormatException ("Number out of range for 32-bit unsigned integer: "
1206- + text );
1262+ throw new NumberFormatException (integerOutOfRangeMessage (isSigned , isLong ));
12071263 }
12081264 }
12091265 } else {
12101266 if (isSigned ) {
12111267 if (bigValue .bitLength () > 63 ) {
1212- throw new NumberFormatException ("Number out of range for 64-bit signed integer: "
1213- + text );
1268+ throw new NumberFormatException (integerOutOfRangeMessage (isSigned , isLong ));
12141269 }
12151270 } else {
12161271 if (bigValue .bitLength () > 64 ) {
1217- throw new NumberFormatException ("Number out of range for 64-bit unsigned integer: "
1218- + text );
1272+ throw new NumberFormatException (integerOutOfRangeMessage (isSigned , isLong ));
12191273 }
12201274 }
12211275 }
@@ -1606,8 +1660,8 @@ public double consumeDouble() throws ParseException {
16061660 double result = Double .parseDouble (currentToken );
16071661 nextToken ();
16081662 return result ;
1609- } catch (NumberFormatException e ) {
1610- throw floatParseException (e );
1663+ } catch (NumberFormatException ignored ) {
1664+ throw floatParseException ();
16111665 }
16121666 }
16131667
@@ -1631,8 +1685,8 @@ public float consumeFloat() throws ParseException {
16311685 float result = Float .parseFloat (currentToken );
16321686 nextToken ();
16331687 return result ;
1634- } catch (NumberFormatException e ) {
1635- throw floatParseException (e );
1688+ } catch (NumberFormatException ignored ) {
1689+ throw floatParseException ();
16361690 }
16371691 }
16381692
@@ -1752,15 +1806,12 @@ public ParseException parseExceptionPreviousToken(String description) {
17521806 * when trying to parse an integer.
17531807 */
17541808 private ParseException integerParseException (NumberFormatException e ) {
1755- return parseException ("Couldn't parse integer: " + e .getMessage ());
1809+ return parseException ("Couldn't parse integer: " + abbreviateIntegerError ( e .getMessage () ));
17561810 }
17571811
1758- /**
1759- * Constructs an appropriate {@link ParseException} for the given {@code NumberFormatException}
1760- * when trying to parse a float or double.
1761- */
1762- private ParseException floatParseException (NumberFormatException e ) {
1763- return parseException ("Couldn't parse number: " + e .getMessage ());
1812+ /** Constructs a bounded {@link ParseException} without forwarding the JDK parse error text. */
1813+ private ParseException floatParseException () {
1814+ return parseException ("Couldn't parse number: token length " + currentToken .length ());
17641815 }
17651816 }
17661817
0 commit comments