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 invalid string input #253

Merged
merged 3 commits into from
Aug 25, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/main/java/org/fusesource/jansi/AnsiRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ public static Appendable render(final String input, Appendable target) throws IO
return target;
}
j += BEGIN_TOKEN_LEN;

// Check for invalid string with END_TOKEN before BEGIN_TOKEN
if (k < j) {
throw new IllegalArgumentException("Invalid input string found.");
}
String spec = input.substring(j, k);

String[] items = spec.split(CODE_TEXT_SEPARATOR, 2);
Expand Down
7 changes: 6 additions & 1 deletion src/test/java/org/fusesource/jansi/AnsiRendererTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
import static org.fusesource.jansi.AnsiRenderer.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Tests for the {@link AnsiRenderer} class.
*
*/
public class AnsiRendererTest {

@BeforeAll
static void setUp() {
Ansi.setEnabled(true);
Expand Down Expand Up @@ -112,6 +112,11 @@ public void testRenderInvalidMissingEnd() {
assertEquals("@|bold foo", str);
}

@Test
public void testRenderInvalidEndBeforeStart() {
assertThrows(IllegalArgumentException.class, () -> render("@|@"));
}

@Test
public void testRenderInvalidMissingText() {
String str = render("@|bold|@");
Expand Down