Skip to content

Commit 25eebb6

Browse files
authored
fix(bigquery-jdbc): bound BigDecimal scale in epoch-decimal timestamp parsing (#15218)
Bug: b/584839496 ### The Issue `BigQueryTemporalUtility.parseEpochDecimalToInstant` and `BigQueryTemporalUtility.formatTimestampStringFromEpochDecimal` called `new BigDecimal(epochDecimal).setScale(..., RoundingMode.DOWN)` without bounding input length or exponent magnitude. Coercing a `STRING` or `JSON` column containing a large-exponent scientific-notation value (such as `"1e100000000"`, `"1e2147483648"`, or `"12345678901234567890e-100000000"`) via `ResultSet.getTimestamp` or `ResultSet.getObject`, or formatting out-of-range epoch-decimal strings in `TIMESTAMP(12)` / `RANGE<TIMESTAMP>`, triggered unbounded `BigInteger.bigTenToThe` expansion, causing uninterruptible CPU stalls and uncaught `OutOfMemoryError` (or unchecked `DateTimeException` on 17-digit out-of-range epoch seconds). ### The Fix - Added `BigQueryTemporalUtility.parseScaledEpochDecimal` to reject strings longer than 128 characters, short-circuit zero (`signum() == 0`) and sub-unit magnitudes (`integerDigits <= -scale`) to `BigDecimal.ZERO.setScale(scale)`, and reject integer magnitudes exceeding 17 digits (`Instant.MIN..Instant.MAX` width, computed in `long` arithmetic to prevent 32-bit `int` overflow) before calling `BigDecimal.setScale`. - Wrapped `LocalDateTime.ofEpochSecond` in `formatTimestampStringFromEpochDecimal` to catch `DateTimeException` and rethrow `BigQueryJdbcException`. - Added unit regression tests in `BigQueryTemporalUtilityTest` covering extreme positive exponents, extreme sub-unit/zero exponents, and overlong inputs.
1 parent cadc868 commit 25eebb6

2 files changed

Lines changed: 99 additions & 3 deletions

File tree

‎java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryTemporalUtility.java‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ static Instant parseEpochDecimalToInstant(String epochDecimal) {
234234
return null;
235235
}
236236

237-
BigDecimal bd = new BigDecimal(epochDecimal).setScale(9, RoundingMode.DOWN);
237+
BigDecimal bd = parseScaledEpochDecimal(epochDecimal, 9);
238238
BigDecimal secondsBd = bd.setScale(0, RoundingMode.FLOOR);
239239
BigDecimal fractionBd = bd.subtract(secondsBd);
240240
int nanos = fractionBd.multiply(NANOS_PER_SECOND).intValue();
@@ -260,7 +260,7 @@ static String formatTimestampStringFromEpochDecimal(
260260
int scale = enableTimestampPicos ? 12 : 6;
261261
BigDecimal bd;
262262
try {
263-
bd = new BigDecimal(epochDecimal).setScale(scale, RoundingMode.DOWN);
263+
bd = parseScaledEpochDecimal(epochDecimal, scale);
264264
} catch (NumberFormatException e) {
265265
try {
266266
return formatTimestampStringFromIso(epochDecimal, enableTimestampPicos);
@@ -275,12 +275,34 @@ static String formatTimestampStringFromEpochDecimal(
275275
BigDecimal multiplier = scale == 12 ? PICOS_PER_SECOND : MICROS_PER_SECOND;
276276
long fractionVal = fractionBd.multiply(multiplier).setScale(0, RoundingMode.DOWN).longValue();
277277

278-
LocalDateTime dt = LocalDateTime.ofEpochSecond(epochSeconds, 0, ZoneOffset.UTC);
278+
LocalDateTime dt;
279+
try {
280+
dt = LocalDateTime.ofEpochSecond(epochSeconds, 0, ZoneOffset.UTC);
281+
} catch (DateTimeException e) {
282+
throw new BigQueryJdbcException("Invalid timestamp value: " + epochDecimal, e);
283+
}
279284
StringBuilder sb = formatDateTimeBase(dt, scale);
280285
appendPadded(sb, fractionVal, scale);
281286
return sb.toString();
282287
}
283288

289+
private static BigDecimal parseScaledEpochDecimal(String epochDecimal, int scale) {
290+
if (epochDecimal.length() > 128) {
291+
throw new NumberFormatException("Epoch timestamp string too long");
292+
}
293+
BigDecimal raw = new BigDecimal(epochDecimal);
294+
if (raw.signum() == 0) {
295+
return BigDecimal.ZERO.setScale(scale);
296+
}
297+
long integerDigits = (long) raw.precision() - (long) raw.scale();
298+
if (integerDigits > 17) {
299+
throw new NumberFormatException("Epoch timestamp out of range");
300+
}
301+
return integerDigits <= -scale
302+
? BigDecimal.ZERO.setScale(scale)
303+
: raw.setScale(scale, RoundingMode.DOWN);
304+
}
305+
284306
/**
285307
* Normalizes an ISO-8601 or civil timestamp string into a standard UTC JDBC timestamp string
286308
* formatted with 6 or 12 fractional digits according to {@code enableTimestampPicos}.

‎java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryTemporalUtilityTest.java‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static com.google.cloud.bigquery.jdbc.utils.TestUtilities.rangeField;
2323
import static com.google.common.truth.Truth.assertThat;
2424
import static org.junit.jupiter.api.Assertions.assertThrows;
25+
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
2526

2627
import com.google.cloud.bigquery.Field;
2728
import com.google.cloud.bigquery.FieldValue;
@@ -31,12 +32,14 @@
3132
import java.sql.Date;
3233
import java.sql.Time;
3334
import java.sql.Timestamp;
35+
import java.time.Duration;
3436
import java.time.Instant;
3537
import java.time.LocalDate;
3638
import java.time.LocalDateTime;
3739
import java.time.LocalTime;
3840
import java.time.ZoneId;
3941
import java.util.Calendar;
42+
import java.util.Collections;
4043
import java.util.TimeZone;
4144
import org.junit.jupiter.api.Test;
4245
import org.junit.jupiter.params.ParameterizedTest;
@@ -388,6 +391,77 @@ public void testParseEpochDecimalToInstantInvalid() {
388391
() -> BigQueryTemporalUtility.parseEpochDecimalToInstant("invalid_epoch"));
389392
}
390393

394+
@ParameterizedTest
395+
@ValueSource(
396+
strings = {
397+
"1e100000000",
398+
"-1e100000000",
399+
"1e2147483647",
400+
"-1e2147483647",
401+
"1e2147483648",
402+
"-1e2147483648",
403+
"1e18",
404+
"-1e18",
405+
"5e16",
406+
"-5e16"
407+
})
408+
public void testExtremePositiveExponentEpochDecimalsFailFast(String input) {
409+
assertTimeoutPreemptively(
410+
Duration.ofSeconds(1),
411+
() -> {
412+
assertThrows(
413+
BigQueryJdbcException.class,
414+
() ->
415+
BigQueryTypeRegistry.convert(
416+
input, StandardSQLTypeName.STRING, Timestamp.class, null));
417+
assertThrows(
418+
BigQueryJdbcException.class,
419+
() -> BigQueryTemporalUtility.formatTimestampStringFromEpochDecimal(input, false));
420+
assertThrows(
421+
BigQueryJdbcException.class,
422+
() -> BigQueryTemporalUtility.formatTimestampStringFromEpochDecimal(input, true));
423+
});
424+
}
425+
426+
@ParameterizedTest
427+
@ValueSource(
428+
strings = {
429+
"0e100000000",
430+
"-0e100000000",
431+
"1e-100000000",
432+
"-1e-100000000",
433+
"12345678901234567890e-100000000",
434+
"-12345678901234567890e-100000000"
435+
})
436+
public void testExtremeSubUnitAndZeroExponentsTruncateInConstantTime(String input)
437+
throws Exception {
438+
assertTimeoutPreemptively(
439+
Duration.ofSeconds(1),
440+
() -> {
441+
assertThat(BigQueryTemporalUtility.parseEpochDecimalToInstant(input))
442+
.isEqualTo(Instant.EPOCH);
443+
assertThat(BigQueryTemporalUtility.formatTimestampStringFromEpochDecimal(input, false))
444+
.isEqualTo("1970-01-01 00:00:00.000000");
445+
assertThat(BigQueryTemporalUtility.formatTimestampStringFromEpochDecimal(input, true))
446+
.isEqualTo("1970-01-01 00:00:00.000000000000");
447+
});
448+
}
449+
450+
@Test
451+
public void testOverlongEpochDecimalStringRejectedFast() {
452+
String overlong = String.join("", Collections.nCopies(200, "0")) + ".5";
453+
assertTimeoutPreemptively(
454+
Duration.ofSeconds(1),
455+
() -> {
456+
assertThrows(
457+
NumberFormatException.class,
458+
() -> BigQueryTemporalUtility.parseEpochDecimalToInstant(overlong));
459+
assertThrows(
460+
BigQueryJdbcException.class,
461+
() -> BigQueryTemporalUtility.formatTimestampStringFromEpochDecimal(overlong, false));
462+
});
463+
}
464+
391465
@Test
392466
public void testFormatTimestampStringFromEpochDecimalWithPicosEnabled()
393467
throws BigQueryJdbcException {

0 commit comments

Comments
 (0)