Skip to content

Commit 3f78e99

Browse files
wenshaoqwencoder
andcommitted
2.0.66.android8: port fixes from main
* fix: bound JSONB decimal scale to prevent excessive CPU usage (#7857) Mirror of main's six-commit hardening series (35d225b, 9d43412, 29efe4d, 73bac1f, c27f0e7, 333cb52, merged as PR #7858). A hostile BC_DECIMAL record can carry an enormous scale (CPU burn inside BigDecimal), a nested decimal as its own scale field (unbounded recursion to StackOverflowError), or a non-int32 scale encoding. The port adds JSONFactory.checkDecimalScale(int)/(BigDecimal), routes every BC_DECIMAL read in JSONReaderJSONB through the new readDecimalScale / readDecimalUnscaled / checkDecimalScaleSum guards (8 switch arms plus the readBigDecimal fast paths and skipValue), caps decimal scale/unscaled nesting at MAX_DECIMAL_LEVEL = 128 (small-stack margin), wraps string-to-BigDecimal conversions with checkDecimalScale, and guards the writer side (JSONWriterJSONB.writeDecimal) and JSONBDump the same way. main's isInt32Num/isInt32Byte/isInt32Short helpers are added to JSONB (this branch did not have them). Branch adaptations: - JSONReaderJSONB gains `import static com.alibaba.fastjson2.JSONB.*;` (mirroring main's import shape) in place of the JSONB.typeName single-type import; unqualified calls do not resolve across classes without it. - The readBigDecimal fast paths keep this branch's getInt/UNSAFE.getLong forms (no getIntBE/check3/check7 here); only the guards are added. - readFloatValue/readDoubleValue BC_DECIMAL arms also take main's intValue -> floatValue/doubleValue corrections; this branch had the same latent bug. - FieldReaderBigDecimalField gains main's FieldReaderBigDecimal NullOnError branch (try/catch -> null when the feature is set). Eleven sibling readers on this branch already implement NullOnError; the field-based BigDecimal reader did not, so the ported resync contract (the record is fully consumed before the throw and NullOnError readers resume at the record boundary) had no consumer here, and the three nullOnError* tests in Issue7857 could not pass. (cherry picked from the PR #7858 series, manually applied) * fix: WriteClassName produces illegal JSON for NaN and Infinity (#7783) Mirror of main 6fd9ba1. Non-finite values must not take the D/F suffix: Double.isFinite / Float.isFinite are added to the existing WriteClassName condition, keeping this branch's simpler condition shape (no WriteNonStringKeyAsString / NotWriteNumberClassName checks here). Test adaptation: Issue7783 is ported without test_specialAsString -- JSONWriter.Feature.WriteFloatSpecialAsString does not exist on this branch. (cherry picked from commit 6fd9ba1, manually applied) * fix: treat format "string" as WriteNonStringValueAsString for BigDecimal (#7734) Mirror of main 7da8818. The "string" sentinel (from @jsonformat(shape = STRING) on main, or @JSONField(format = "string") here) must not be compiled into a DecimalFormat pattern. Ports the FieldWriter constructor guard and the BigDecimal / BigDecimal[] writer resolution arms. Branch adaptations: - main's ObjectWriterProvider.getObjectWriter(Type, String, Locale) hunk is not applicable: the method does not exist on this branch. - FieldWriterDoubleField now reads this.features when computing writeNonStringValueAsString. The base FieldWriter constructor ORs the sentinel bit into its own parameter before assigning this.features; the subclass captured the flag from its own (unmodified) parameter, so the sentinel was silently lost for Double fields. main reads the feature at write time and is unaffected. The same capture pattern remains in FieldWriterDoubleMethod / FieldWriterFloatMethod / FieldWriterInt16 / FieldWriterInt8 -- out of the ported suite's scope. - Issue7734 is adapted to drive the sentinel through @JSONField(format = "string"): this branch's BeanUtils does not process @jsonformat, while the FieldWriter sentinel -> WriteNonStringValueAsString mapping (FieldWriter.java:74) makes the @JSONField path exercise the same contract. 4/4 green. (cherry picked from commit 7da8818, manually applied) * fix: validate cached field writer by value type in toJSONObject (#7714) Mirror of main 6c29d27 + 514f3af (merged as PR #7739). Ports FieldWriterObject.isTypeMatch (final form, with the null guard and javadoc), the getObjectWriter refactor to share the predicate, and the ObjectWriterAdapter.toJSONObject re-resolution block including the JSON.toJSON conversion for non-adapter writers. This branch's toJSONObject had the same unguarded reuse (issue #7714: ClassCastException wrapped as "key get error", and subclass values silently dropped). Issue7714 ported verbatim, 4/4 green. (cherry picked from commits 6c29d27 and 514f3af, manually applied) * fix: use own ClassLoader as parent when context ClassLoader loads a foreign fastjson2 (#7869) Not applicable to this branch -- DynamicClassLoader does not exist here (no ASM codegen), so the foreign-parent failure mode of main #7873 cannot occur. * fix: the getter method is discarded when serializing character fields (#7719) Not applicable to this branch -- main's shared FieldWriterCharValue / FieldWriterChar constructors dropped the method argument; here ObjectWriterCreator always passes the getter to FieldWriterCharMethod (ObjectWriterCreator.java:876) and FieldWriterCharMethod invokes it (line 31). The guard test was already ported from main 7c4609f as ObjectWriter14Test; it is extended with the f2 assertions from d5c30ac (primitive char getter returning lowercase while the backing field holds uppercase), adapted to this branch's JSONPath.of().eval() spelling. 3/3 green. Verification: 34 ported tests green (Issue7857 19, Issue7783 4, Issue7734 4, Issue7714 4, ObjectWriter14Test 3); full core suite BUILD SUCCESS (JDK 8). Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
1 parent 8355a57 commit 3f78e99

18 files changed

Lines changed: 1073 additions & 56 deletions

‎core/src/main/java/com/alibaba/fastjson2/JSONB.java‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,6 +1345,36 @@ static byte[] fromJSONBytes(byte[] jsonUtf8Bytes) {
13451345
return JSONB.toBytes(object);
13461346
}
13471347

1348+
/**
1349+
* Checks if the specified type is an int32 number type
1350+
*
1351+
* @param type the type to check
1352+
* @return true if the type is an int32 number type, false otherwise
1353+
*/
1354+
static boolean isInt32Num(int type) {
1355+
return type >= BC_INT32_NUM_MIN && type <= BC_INT32_NUM_MAX;
1356+
}
1357+
1358+
/**
1359+
* Checks if the specified type is an int32 byte type
1360+
*
1361+
* @param type the type to check
1362+
* @return true if the type is an int32 byte type, false otherwise
1363+
*/
1364+
static boolean isInt32Byte(int type) {
1365+
return (type & 0xF0) == 0x30;
1366+
}
1367+
1368+
/**
1369+
* Checks if the specified type is an int32 short type
1370+
*
1371+
* @param type the type to check
1372+
* @return true if the type is an int32 short type, false otherwise
1373+
*/
1374+
static boolean isInt32Short(int type) {
1375+
return (type & 0xF8) == 0x40;
1376+
}
1377+
13481378
static String typeName(byte type) {
13491379
switch (type) {
13501380
case BC_OBJECT:

‎core/src/main/java/com/alibaba/fastjson2/JSONBDump.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import static com.alibaba.fastjson2.JSONB.Constants.*;
1212
import static com.alibaba.fastjson2.JSONB.typeName;
13+
import static com.alibaba.fastjson2.JSONFactory.checkDecimalScale;
1314
import static com.alibaba.fastjson2.util.JDKUtils.*;
1415

1516
final class JSONBDump {
@@ -241,7 +242,7 @@ private void dumpAny() {
241242
return;
242243
}
243244
case BC_DECIMAL: {
244-
int scale = readInt32Value();
245+
int scale = checkDecimalScale(readInt32Value());
245246
BigInteger unscaledValue;
246247
int type = bytes[offset++];
247248
switch (type) {

‎core/src/main/java/com/alibaba/fastjson2/JSONFactory.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@ public final class JSONFactory {
4444

4545
static int defaultDecimalMaxScale = 2048;
4646

47+
static int checkDecimalScale(int scale) {
48+
if (scale < -defaultDecimalMaxScale || scale > defaultDecimalMaxScale) {
49+
throw new JSONException("scale overflow : " + scale);
50+
}
51+
return scale;
52+
}
53+
54+
static BigDecimal checkDecimalScale(BigDecimal decimal) {
55+
if (decimal == null) {
56+
return null;
57+
}
58+
checkDecimalScale(decimal.scale());
59+
return decimal;
60+
}
61+
4762
static final class NameCacheEntry2 {
4863
final String name;
4964
final long value0;

‎core/src/main/java/com/alibaba/fastjson2/JSONReader.java‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2673,9 +2673,7 @@ public final Number getNumber() {
26732673
}
26742674
case JSON_TYPE_BIG_DEC: {
26752675
if (scale > 0) {
2676-
if (scale > defaultDecimalMaxScale) {
2677-
throw new JSONException("scale overflow : " + scale);
2678-
}
2676+
checkDecimalScale(scale);
26792677
return toBigDecimal(stringValue);
26802678
} else {
26812679
return new BigInteger(stringValue);

0 commit comments

Comments
 (0)