@@ -243,12 +243,18 @@ public BigInteger asBigInteger() {
243243
244244 @ Override
245245 public BigInteger asBigInteger (BigInteger defaultValue ) {
246+ if (!_numberLengthInRange ()) {
247+ return defaultValue ;
248+ }
246249 BigInteger big = _tryParseAsBigInteger ();
247250 return (big == null ) ? defaultValue : big ;
248251 }
249252
250253 @ Override
251254 public Optional <BigInteger > asBigIntegerOpt () {
255+ if (!_numberLengthInRange ()) {
256+ return Optional .empty ();
257+ }
252258 BigInteger big = _tryParseAsBigInteger ();
253259 return (big == null ) ? Optional .empty () : Optional .of (big );
254260 }
@@ -269,12 +275,18 @@ public float asFloat()
269275 @ Override
270276 public float asFloat (float defaultValue )
271277 {
278+ if (!_numberLengthInRange ()) {
279+ return defaultValue ;
280+ }
272281 Float F = _tryParseAsFloat ();
273282 return (F == null ) ? defaultValue : F ;
274283 }
275284
276285 @ Override
277286 public Optional <Float > asFloatOpt () {
287+ if (!_numberLengthInRange ()) {
288+ return Optional .empty ();
289+ }
278290 Float F = _tryParseAsFloat ();
279291 return (F == null ) ? Optional .empty () : Optional .of (F );
280292 }
@@ -295,12 +307,18 @@ public double asDouble()
295307 @ Override
296308 public double asDouble (double defaultValue )
297309 {
310+ if (!_numberLengthInRange ()) {
311+ return defaultValue ;
312+ }
298313 Double d = _tryParseAsDouble ();
299314 return (d == null ) ? defaultValue : d ;
300315 }
301316
302317 @ Override
303318 public OptionalDouble asDoubleOpt () {
319+ if (!_numberLengthInRange ()) {
320+ return OptionalDouble .empty ();
321+ }
304322 Double d = _tryParseAsDouble ();
305323 return (d == null ) ? OptionalDouble .empty () : OptionalDouble .of (d );
306324 }
@@ -319,12 +337,18 @@ public BigDecimal asDecimal() {
319337
320338 @ Override
321339 public BigDecimal asDecimal (BigDecimal defaultValue ) {
340+ if (!_numberLengthInRange ()) {
341+ return defaultValue ;
342+ }
322343 BigDecimal dec = _tryParseAsBigDecimal ();
323344 return (dec == null ) ? defaultValue : dec ;
324345 }
325346
326347 @ Override
327348 public Optional <BigDecimal > asDecimalOpt () {
349+ if (!_numberLengthInRange ()) {
350+ return Optional .empty ();
351+ }
328352 BigDecimal dec = _tryParseAsBigDecimal ();
329353 return (dec == null ) ? Optional .empty () : Optional .of (dec );
330354 }
@@ -362,6 +386,10 @@ protected Long _tryParseAsLong() {
362386
363387 protected BigInteger _tryParseAsBigInteger () {
364388 if (NumberInput .looksLikeValidNumber (_value )) {
389+ // Enforce number-length limit before the super-linear parse, same as
390+ // deserializers do; no `StreamReadConstraints` available here so use
391+ // `defaults()` (compare `DecimalNode`/`POJONode`, [databind#6214])
392+ StreamReadConstraints .defaults ().validateIntegerLength (_value .length ());
365393 try {
366394 return NumberInput .parseBigInteger (_value , true );
367395 } catch (NumberFormatException e ) {
@@ -373,6 +401,7 @@ protected BigInteger _tryParseAsBigInteger() {
373401
374402 protected Float _tryParseAsFloat () {
375403 if (NumberInput .looksLikeValidNumber (_value )) {
404+ StreamReadConstraints .defaults ().validateFPLength (_value .length ());
376405 try {
377406 return NumberInput .parseFloat (_value , true );
378407 } catch (NumberFormatException e ) {
@@ -384,6 +413,7 @@ protected Float _tryParseAsFloat() {
384413
385414 protected Double _tryParseAsDouble () {
386415 if (NumberInput .looksLikeValidNumber (_value )) {
416+ StreamReadConstraints .defaults ().validateFPLength (_value .length ());
387417 try {
388418 return NumberInput .parseDouble (_value , true );
389419 } catch (NumberFormatException e ) {
@@ -395,6 +425,7 @@ protected Double _tryParseAsDouble() {
395425
396426 protected BigDecimal _tryParseAsBigDecimal () {
397427 if (NumberInput .looksLikeValidNumber (_value )) {
428+ StreamReadConstraints .defaults ().validateFPLength (_value .length ());
398429 try {
399430 return NumberInput .parseBigDecimal (_value , true );
400431 } catch (NumberFormatException e ) {
@@ -403,6 +434,16 @@ protected BigDecimal _tryParseAsBigDecimal() {
403434 }
404435 return null ;
405436 }
437+
438+ // [databind#6214]-style number-length guard for the lenient default/`Optional`
439+ // accessors: they must return default/empty rather than throw, so they check the
440+ // limit up front (the strict accessors instead go via the `_tryParseAs...` helpers
441+ // above, which surface `StreamConstraintsException`). Both `validateIntegerLength`
442+ // and `validateFPLength` reject lengths past `getMaxNumberLength()`, so a single
443+ // check covers integer and floating-point coercion alike.
444+ private boolean _numberLengthInRange () {
445+ return _value .length () <= StreamReadConstraints .defaults ().getMaxNumberLength ();
446+ }
406447
407448 /*
408449 /**********************************************************************
0 commit comments