Skip to content
Merged
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
2 changes: 1 addition & 1 deletion moshi/src/main/java/com/squareup/moshi/-JsonUtf8Reader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ internal class `-JsonUtf8Reader` : JsonReader {
}

NUMBER_CHAR_DIGIT -> {
if (value == 0L) {
if (fitsInLong && value == 0L) {
return PEEKED_NONE // Leading '0' prefix is not allowed (since it could be octal).
}
val newValue = value * 10 - (c - '0').toLong()
Expand Down
13 changes: 13 additions & 0 deletions moshi/src/test/java/com/squareup/moshi/JsonUtf8ReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,19 @@ public void negativeZeroIsANumber() throws Exception {
assertEquals("-0", reader.nextString());
}

/**
* Regression test for a bug where {@code peekNumber} rejected a valid numeric literal whose
* prefix happened to be a multiple of 2<sup>64</sup>. The {@code long} accumulator wraps to
* exactly zero in that case, which the old "leading zero" guard misread as an octal prefix.
*/
@Test
public void numberLongAccumulatorOverflowsToZero() throws IOException {
String number = "184467440737095516160";
JsonReader reader = newReader(number);
assertThat(reader.peek()).isEqualTo(NUMBER);
assertThat(reader.nextString()).isEqualTo(number);
}

@Test
public void numberToStringCoersion() throws Exception {
JsonReader reader = newReader("[0, 9223372036854775807, 2.5, 3.010, \"a\", \"5\"]");
Expand Down