Skip to content

Commit

Permalink
Fix invalid string input (#253)
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Chan <arthur.chan@adalogics.com>
Co-authored-by: Guillaume Nodet <gnodet@gmail.com>
  • Loading branch information
arthurscchan and gnodet committed Aug 25, 2023
1 parent 7bc5862 commit 58260c6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
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

0 comments on commit 58260c6

Please sign in to comment.