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 infinite loop due to integer overflow when reading large strings #1350

Closed
wants to merge 3 commits into from
Closed
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 @@ -2544,7 +2544,14 @@ private final void _finishString2(char[] outBuf, int outPtr)
outBuf = _textBuffer.finishCurrentSegment();
outPtr = 0;
}
final int max = Math.min(_inputEnd, (ptr + (outBuf.length - outPtr)));

int max;
try {
max = Math.min(_inputEnd, Math.addExact(ptr, outBuf.length - outPtr));
} catch (ArithmeticException ex) {
max = Integer.MAX_VALUE;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way that this could be set to something like the StreamReadConstraints max number size? For people who might set a bigger max number size but that don't go for the absolute max.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eg would _inputEnd work?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, either _inputEnd or _streamReadConstraints.getMaxStringLength() will fix the issue. Do you have a preference?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

min of both?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, looking at the code Integer.MAX_VALUE looks like a good fallback

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I think Integer.MAX_VALUE would be fine since we're just trying to prevent an overflow here. max would generally be larger than the max string length or _inputEnd. Verification that we don't go beyond the max string length happens when data is read.

}

while (ptr < max) {
c = inputBuffer[ptr++] & 0xFF;
if (codes[c] != 0) {
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/com/fasterxml/jackson/core/json/TestLargeString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.fasterxml.jackson.core.json;

import com.fasterxml.jackson.core.*;
import net.bytebuddy.utility.RandomString;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;

import static org.junit.jupiter.api.Assertions.assertEquals;

class TestLargeString extends JUnit5TestBase {

@Test
void testLargeStringDeserialization() throws Exception
{
String largeString = RandomString.make(Integer.MAX_VALUE - 1024);
JsonFactory f = JsonFactory.builder()
.streamReadConstraints(StreamReadConstraints.builder()
.maxStringLength(Integer.MAX_VALUE)
.build())
.build();
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
JsonGenerator g = f.createGenerator(bytes);
g.writeString(largeString);
g.close();

JsonParser parser = f.createParser(bytes.toByteArray());
String parsedString = parser.nextTextValue();

assertEquals(largeString, parsedString);
}
}
Loading