Skip to content

Commit

Permalink
Fix duration parse issue.
Browse files Browse the repository at this point in the history
Duration strings that consist of a single "-" or "+" character were
throwing an IndexOutOfBoundsException. An IllegalArgumentException
should be thrown instead because it's not a valid duration string.

#127
  • Loading branch information
mangstadt committed Dec 16, 2023
1 parent c2bb74a commit d55af93
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/biweekly/util/Duration.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static Duration parse(String value) {
index++;
}

if (value.charAt(index) != 'P') {
if (index >= value.length() || value.charAt(index) != 'P') {
throw parseError(value);
}

Expand Down
10 changes: 10 additions & 0 deletions src/test/java/biweekly/util/DurationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ public void parse_unknown_character_after_9() {
Duration.parse("P5Z");
}

@Test(expected = IllegalArgumentException.class)
public void parse_single_hyphen() {
Duration.parse("-");
}

@Test(expected = IllegalArgumentException.class)
public void parse_single_plus() {
Duration.parse("+");
}

@Test(expected = IllegalArgumentException.class)
public void parse_empty_string() {
Duration.parse("");
Expand Down

0 comments on commit d55af93

Please sign in to comment.