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 bug #252

Merged
merged 5 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/main/java/org/fusesource/jansi/Ansi.java
Original file line number Diff line number Diff line change
Expand Up @@ -706,10 +706,16 @@ public Ansi eraseLine(final Erase kind) {
}

public Ansi scrollUp(final int rows) {
if (rows == Integer.MIN_VALUE) {
return scrollDown(Integer.MAX_VALUE);
}
return rows > 0 ? appendEscapeSequence('S', rows) : rows < 0 ? scrollDown(-rows) : this;
}

public Ansi scrollDown(final int rows) {
if (rows == Integer.MIN_VALUE) {
return scrollUp(Integer.MAX_VALUE);
}
return rows > 0 ? appendEscapeSequence('T', rows) : rows < 0 ? scrollUp(-rows) : this;
}

Expand Down
29 changes: 28 additions & 1 deletion src/test/java/org/fusesource/jansi/AnsiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.junit.jupiter.params.provider.CsvSource;

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

/**
* Tests for the {@link Ansi} class.
Expand Down Expand Up @@ -64,6 +65,32 @@ public void apply(Ansi ansi) {
}).toString());
}

@ParameterizedTest
@CsvSource({
"-2147483648,ESC[2147483647T", "2147483647,ESC[2147483647S",
"-100000,ESC[100000T", "100000,ESC[100000S"
})
public void testScrollUp(int x, String expected) {
try {
arthurscchan marked this conversation as resolved.
Show resolved Hide resolved
assertAnsi(expected, Ansi.ansi().scrollUp(x));
} catch (StackOverflowError e) {
fail("Infinite loop detected.");
}
}

@ParameterizedTest
@CsvSource({
"-2147483648,ESC[2147483647S", "2147483647,ESC[2147483647T",
"-100000,ESC[100000S", "100000,ESC[100000T"
})
public void testScrollDown(int x, String expected) {
try {
assertAnsi(expected, Ansi.ansi().scrollDown(x));
arthurscchan marked this conversation as resolved.
Show resolved Hide resolved
} catch (StackOverflowError e) {
fail("Infinite loop detected.");
}
}

@ParameterizedTest
@CsvSource({
"-1,-1,ESC[1;1H", "-1,0,ESC[1;1H", "-1,1,ESC[1;1H", "-1,2,ESC[1;2H",
Expand Down Expand Up @@ -152,4 +179,4 @@ public void testColorDisabled() {
private static void assertAnsi(String expected, Ansi actual) {
assertEquals(expected.replace("ESC", "\033"), actual.toString());
}
}
}