@@ -1789,6 +1789,8 @@ public void writeNumber(String encodedValue) throws IOException
1789
1789
protected void _writeIntegralNumber (String enc , boolean neg ) throws IOException
1790
1790
{
1791
1791
int len = enc .length ();
1792
+ // 16-Dec-2023, tatu: Guard against too-big numbers
1793
+ _streamReadConstraints ().validateIntegerLength (len );
1792
1794
if (neg ) {
1793
1795
--len ;
1794
1796
}
@@ -1806,18 +1808,23 @@ protected void _writeIntegralNumber(String enc, boolean neg) throws IOException
1806
1808
}
1807
1809
return ;
1808
1810
} catch (NumberFormatException e ) { }
1809
- throw new JsonGenerationException ("Invalid String representation for Number ('" +enc
1810
- +"'); can not write using Smile format" , this );
1811
+ _reportError ("Invalid String representation for Number ('" +enc
1812
+ +"'); can not write using Smile format" );
1811
1813
}
1812
1814
1813
1815
protected void _writeDecimalNumber (String enc ) throws IOException
1814
1816
{
1815
- try {
1816
- writeNumber (NumberInput .parseBigDecimal (enc , false ));
1817
- } catch (NumberFormatException e ) {
1818
- throw new JsonGenerationException ("Invalid String representation for Number ('" +enc
1819
- +"'); can not write using Smile format" , this );
1817
+ // 16-Dec-2023, tatu: Guard against too-big numbers
1818
+ _streamReadConstraints ().validateFPLength (enc .length ());
1819
+ // ... and check basic validity too
1820
+ if (NumberInput .looksLikeValidNumber (enc )) {
1821
+ try {
1822
+ writeNumber (NumberInput .parseBigDecimal (enc , false ));
1823
+ return ;
1824
+ } catch (NumberFormatException e ) { }
1820
1825
}
1826
+ _reportError ("Invalid String representation for Number ('" +enc
1827
+ +"'); can not write using Smile format" );
1821
1828
}
1822
1829
1823
1830
/*
@@ -2763,4 +2770,21 @@ protected long outputOffset() {
2763
2770
protected UnsupportedOperationException _notSupported () {
2764
2771
return new UnsupportedOperationException ();
2765
2772
}
2773
+
2774
+ /*
2775
+ /**********************************************************
2776
+ /* Internal methods, misc other
2777
+ /**********************************************************
2778
+ */
2779
+
2780
+ /**
2781
+ * We need access to some reader-side constraints for safety-check within
2782
+ * number decoding for {@linl #writeNumber(String)}: for now we need to
2783
+ * rely on global defaults; should be ok for basic safeguarding.
2784
+ *
2785
+ * @since 2.17
2786
+ */
2787
+ protected StreamReadConstraints _streamReadConstraints () {
2788
+ return StreamReadConstraints .defaults ();
2789
+ }
2766
2790
}
0 commit comments