Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TOML parse failure when number token hits buffer edge #356

Merged
merged 2 commits into from
Nov 22, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.ValueNode;

import java.io.IOException;
import java.io.Reader;
Expand Down Expand Up @@ -242,7 +243,12 @@ private JsonNode parseInt(int nextState) throws IOException {
}
}

ValueNode node = parseIntFromBuffer(buffer, start, length);
pollExpected(TomlToken.INTEGER, nextState);
return node;
}

private ValueNode parseIntFromBuffer(char[] buffer, int start, int length) throws TomlStreamReadException {
if (length > 2) {
char baseChar = buffer[start + 1];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void testBigIntegerDecoding50033() throws Exception
JsonNode n = TOML_MAPPER.readTree(INPUT);
Assert.fail("Should not pass, got: "+n);
} catch (StreamReadException e) {
verifyException(e, "Invalid number");
verifyException(e, "Premature end of file");
// NOTE: decoding of token for error message seems wrong, cannot
// quite verify it for the last line
}
Expand Down Expand Up @@ -73,8 +73,7 @@ public void testNumberParsingFail50395() throws Exception
TOML_MAPPER.readTree(INPUT);
Assert.fail("Should not pass");
} catch (StreamReadException e) {
verifyException(e, "Invalid number representation");
verifyException(e, "Illegal leading minus sign");
verifyException(e, "Premature end of file");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.core.io.ContentReference;
import com.fasterxml.jackson.core.io.IOContext;
import com.fasterxml.jackson.core.json.JsonReadFeature;
import com.fasterxml.jackson.core.util.BufferRecycler;
import com.fasterxml.jackson.core.util.BufferRecyclers;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -23,6 +24,7 @@
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.util.Arrays;

@SuppressWarnings("OctalInteger")
public class ParserTest {
Expand Down Expand Up @@ -1027,4 +1029,23 @@ public void unknownEscape() throws IOException {
expectedException.expectMessage("Unknown escape sequence");
toml("foo = \"\\k\"");
}

@Test
public void chunkEdge() throws IOException {
BufferRecycler br = BufferRecyclers.getBufferRecycler();
char[] chars = br.allocCharBuffer(0);
br.releaseCharBuffer(0, chars);
int bufferLength = chars.length;

ObjectNode node = toml("foo = \"" + repeat('a', bufferLength - 19) + "\"\n" +
"bar = 123\n" +
"baz = \"" + repeat('a', bufferLength) + "\"");
Assert.assertEquals(123, node.get("bar").intValue());
}

private static String repeat(char c, int n) {
char[] chars = new char[n];
Arrays.fill(chars, c);
return new String(chars);
}
}