Skip to content

[8.16] Added test to verify the int overflow happen (backport #17353) #17357

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

Merged
merged 1 commit into from
Mar 19, 2025
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: 2 additions & 0 deletions logstash-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ tasks.register("javaTests", Test) {
exclude '/org/logstash/plugins/factory/PluginFactoryExtTest.class'
exclude '/org/logstash/execution/ObservedExecutionTest.class'

maxHeapSize = "12g"

jacoco {
enabled = true
destinationFile = layout.buildDirectory.file('jacoco/test.exec').get().asFile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class BufferedTokenizerExt extends RubyObject {
private RubyString delimiter = NEW_LINE;
private int sizeLimit;
private boolean hasSizeLimit;
private int inputSize;
private long inputSize;
private boolean bufferFullErrorNotified = false;
private String encodingName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,19 @@
@SuppressWarnings("unchecked")
public final class BufferedTokenizerExtWithSizeLimitTest extends RubyTestBase {

public static final int GB = 1024 * 1024 * 1024;
private BufferedTokenizerExt sut;
private ThreadContext context;

@Before
public void setUp() {
initSUTWithSizeLimit(10);
}

private void initSUTWithSizeLimit(int sizeLimit) {
sut = new BufferedTokenizerExt(RubyUtil.RUBY, RubyUtil.BUFFERED_TOKENIZER);
context = RUBY.getCurrentContext();
IRubyObject[] args = {RubyUtil.RUBY.newString("\n"), RubyUtil.RUBY.newFixnum(10)};
IRubyObject[] args = {RubyUtil.RUBY.newString("\n"), RubyUtil.RUBY.newFixnum(sizeLimit)};
sut.init(context, args);
}

Expand Down Expand Up @@ -108,4 +113,29 @@ public void giveMultipleSegmentsThatGeneratesMultipleBufferFullErrorsThenIsAbleT
RubyArray<RubyString> tokens = (RubyArray<RubyString>) sut.extract(context, RubyUtil.RUBY.newString("ccc\nddd\n"));
assertEquals(List.of("ccccc", "ddd"), tokens);
}

@Test
public void givenMaliciousInputExtractDoesntOverflow() {
assertEquals("Xmx must equals to what's defined in the Gradle's javaTests task",
12L * GB, Runtime.getRuntime().maxMemory());

// re-init the tokenizer with big sizeLimit
initSUTWithSizeLimit((int) (2L * GB) - 3);
// Integer.MAX_VALUE is 2 * GB
String bigFirstPiece = generateString("a", Integer.MAX_VALUE - 1024);
sut.extract(context, RubyUtil.RUBY.newString(bigFirstPiece));

// add another small fragment to trigger int overflow
// sizeLimit is (2^32-1)-3 first segment length is (2^32-1) - 1024 second is 1024 +2
// so the combined length of first and second is > sizeLimit and should throw an expection
// but because of overflow it's negative and happens to be < sizeLimit
Exception thrownException = assertThrows(IllegalStateException.class, () -> {
sut.extract(context, RubyUtil.RUBY.newString(generateString("a", 1024 + 2)));
});
assertThat(thrownException.getMessage(), containsString("input buffer full"));
}

private String generateString(String fill, int size) {
return fill.repeat(size);
}
}