Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 14 additions & 23 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF16.java
Original file line number Diff line number Diff line change
Expand Up @@ -4379,18 +4379,12 @@ public final BigDecimal readBigDecimal() {
}

valueType = JSON_TYPE_INT;
boolean overflow = false;
int digitCount = 0; // Total digit count for overflow detection
long longValue = 0;
while (ch >= '0' && ch <= '9') {
valid = true;
if (!overflow) {
long r = longValue * 10;
if ((longValue | 10) >>> 31 == 0L || (r / 10 == longValue)) {
longValue = r + (ch - '0');
} else {
overflow = true;
}
}
longValue = (longValue << 3) + (longValue << 1) + (ch & 0xf);
digitCount++;

if (offset == end) {
ch = EOI;
Expand All @@ -4400,25 +4394,15 @@ public final BigDecimal readBigDecimal() {
ch = chars[offset++];
}

if (longValue < 0) {
overflow = true;
}

this.scale = 0;
if (ch == '.') {
valueType = JSON_TYPE_DEC;
ch = chars[offset++];
while (ch >= '0' && ch <= '9') {
valid = true;
this.scale++;
if (!overflow) {
long r = longValue * 10;
if ((longValue | 10) >>> 31 == 0L || (r / 10 == longValue)) {
longValue = r + (ch - '0');
} else {
overflow = true;
}
}
longValue = (longValue << 3) + (longValue << 1) + (ch & 0xf);
digitCount++;

if (offset == end) {
ch = EOI;
Expand All @@ -4429,6 +4413,9 @@ public final BigDecimal readBigDecimal() {
}
}

// Check overflow: more than 18 digits, or 19 digits with negative value (sign bit set)
boolean overflow = digitCount > 18 && (digitCount > 19 || longValue < 0);

int expValue = 0;
if (ch == 'e' || ch == 'E') {
boolean negativeExp;
Expand Down Expand Up @@ -4520,8 +4507,12 @@ public final BigDecimal readBigDecimal() {
}
}
if (!value) {
if (expValue == 0 && !overflow && longValue != 0) {
decimal = BigDecimal.valueOf(negative ? -longValue : longValue, scale);
if (!overflow && longValue != 0) {
// Combine scale and exponent: scale - expValue
// For 1.5E3: scale=1, exp=3 => combined=-2 => 1500
// For 1.5E-3: scale=1, exp=-3 => combined=4 => 0.0015
int combinedScale = scale - expValue;
decimal = BigDecimal.valueOf(negative ? -longValue : longValue, combinedScale);
value = true;
}

Expand Down
37 changes: 14 additions & 23 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF8.java
Original file line number Diff line number Diff line change
Expand Up @@ -6822,18 +6822,12 @@ public final BigDecimal readBigDecimal() {
}

valueType = JSON_TYPE_INT;
boolean overflow = false;
int digitCount = 0; // Total digit count for overflow detection
long longValue = 0;
while (ch >= '0' && ch <= '9') {
valid = true;
if (!overflow) {
long r = longValue * 10;
if ((longValue | 10) >>> 31 == 0L || (r / 10 == longValue)) {
longValue = r + (ch - '0');
} else {
overflow = true;
}
}
longValue = (longValue << 3) + (longValue << 1) + (ch & 0xf);
digitCount++;

if (offset == end) {
ch = EOI;
Expand All @@ -6843,25 +6837,15 @@ public final BigDecimal readBigDecimal() {
ch = bytes[offset++];
}

if (longValue < 0) {
overflow = true;
}

this.scale = 0;
if (ch == '.') {
valueType = JSON_TYPE_DEC;
ch = bytes[offset++];
while (ch >= '0' && ch <= '9') {
valid = true;
this.scale++;
if (!overflow) {
long r = longValue * 10;
if ((longValue | 10) >>> 31 == 0L || (r / 10 == longValue)) {
longValue = r + (ch - '0');
} else {
overflow = true;
}
}
longValue = (longValue << 3) + (longValue << 1) + (ch & 0xf);
digitCount++;

if (offset == end) {
ch = EOI;
Expand All @@ -6872,6 +6856,9 @@ public final BigDecimal readBigDecimal() {
}
}

// Check overflow: more than 18 digits, or 19 digits with negative value (sign bit set)
boolean overflow = digitCount > 18 && (digitCount > 19 || longValue < 0);

int expValue = 0;
if (ch == 'e' || ch == 'E') {
boolean negativeExp;
Expand Down Expand Up @@ -6959,8 +6946,12 @@ public final BigDecimal readBigDecimal() {
}
}
if (!value) {
if (expValue == 0 && !overflow && longValue != 0) {
decimal = BigDecimal.valueOf(negative ? -longValue : longValue, scale);
if (!overflow && longValue != 0) {
// Combine scale and exponent: scale - expValue
// For 1.5E3: scale=1, exp=3 => combined=-2 => 1500
// For 1.5E-3: scale=1, exp=-3 => combined=4 => 0.0015
int combinedScale = scale - expValue;
decimal = BigDecimal.valueOf(negative ? -longValue : longValue, combinedScale);
value = true;
}

Expand Down
Loading