Skip to content

Commit db12a65

Browse files
committed
Further checking for SmileGenerator.writeNumber(String) to avoid problems
1 parent 1d1ccb0 commit db12a65

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

smile/src/main/java/com/fasterxml/jackson/dataformat/smile/SmileGenerator.java

+31-7
Original file line numberDiff line numberDiff line change
@@ -1789,6 +1789,8 @@ public void writeNumber(String encodedValue) throws IOException
17891789
protected void _writeIntegralNumber(String enc, boolean neg) throws IOException
17901790
{
17911791
int len = enc.length();
1792+
// 16-Dec-2023, tatu: Guard against too-big numbers
1793+
_streamReadConstraints().validateIntegerLength(len);
17921794
if (neg) {
17931795
--len;
17941796
}
@@ -1806,18 +1808,23 @@ protected void _writeIntegralNumber(String enc, boolean neg) throws IOException
18061808
}
18071809
return;
18081810
} 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");
18111813
}
18121814

18131815
protected void _writeDecimalNumber(String enc) throws IOException
18141816
{
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) { }
18201825
}
1826+
_reportError("Invalid String representation for Number ('"+enc
1827+
+"'); can not write using Smile format");
18211828
}
18221829

18231830
/*
@@ -2763,4 +2770,21 @@ protected long outputOffset() {
27632770
protected UnsupportedOperationException _notSupported() {
27642771
return new UnsupportedOperationException();
27652772
}
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+
}
27662790
}

0 commit comments

Comments
 (0)