Skip to content

Commit fb858e4

Browse files
committed
javatime.kt: fix generation of out-of-bounds nanoseconds values if the number of proper digits is 1
1 parent d9baf9e commit fb858e4

File tree

1 file changed

+23
-3
lines changed
  • firebase-dataconnect/testutil/src/main/kotlin/com/google/firebase/dataconnect/testutil/property/arbitrary

1 file changed

+23
-3
lines changed

firebase-dataconnect/testutil/src/main/kotlin/com/google/firebase/dataconnect/testutil/property/arbitrary/javatime.kt

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import com.google.firebase.dataconnect.testutil.property.arbitrary.JavaTimeEdgeC
2424
import com.google.firebase.dataconnect.testutil.property.arbitrary.JavaTimeEdgeCases.MIN_NANO
2525
import com.google.firebase.dataconnect.testutil.property.arbitrary.JavaTimeEdgeCases.MIN_YEAR
2626
import com.google.firebase.dataconnect.testutil.toTimestamp
27+
import io.kotest.common.mapError
2728
import io.kotest.property.Arb
2829
import io.kotest.property.arbitrary.arbitrary
2930
import io.kotest.property.arbitrary.choice
@@ -316,8 +317,12 @@ object JavaTimeArbs {
316317
repeat(digitCounts.leadingZeroes) { append('0') }
317318
if (digitCounts.proper > 0) {
318319
append(nonZeroDigits.bind())
319-
repeat(digitCounts.proper - 2) { append(digits.bind()) }
320-
append(nonZeroDigits.bind())
320+
if (digitCounts.proper > 1) {
321+
if (digitCounts.proper > 2) {
322+
repeat(digitCounts.proper - 2) { append(digits.bind()) }
323+
}
324+
append(nonZeroDigits.bind())
325+
}
321326
}
322327
repeat(digitCounts.trailingZeroes) { append('0') }
323328
}
@@ -327,9 +332,24 @@ object JavaTimeArbs {
327332
if (nanosecondsStringTrimmed.isEmpty()) {
328333
0
329334
} else {
330-
nanosecondsStringTrimmed.toInt()
335+
val toIntResult = nanosecondsStringTrimmed.runCatching { toInt() }
336+
toIntResult.mapError { exception ->
337+
Exception(
338+
"internal error qbdgapmye2: " +
339+
"failed to parse nanosecondsStringTrimmed as an int: " +
340+
"\"$nanosecondsStringTrimmed\" (digitCounts=$digitCounts)",
341+
exception
342+
)
343+
}
344+
toIntResult.getOrThrow()
331345
}
332346

347+
check(nanosecondsInt in 0..999_999_999) {
348+
"internal error c7j2myw6bd: " +
349+
"nanosecondsStringTrimmed parsed to a value outside the valid range: " +
350+
"$nanosecondsInt (digitCounts=$digitCounts)"
351+
}
352+
333353
Nanoseconds(nanosecondsInt, nanosecondsString)
334354
}
335355
}

0 commit comments

Comments
 (0)