diff --git a/agrona/src/main/java/org/agrona/AsciiEncoding.java b/agrona/src/main/java/org/agrona/AsciiEncoding.java index 343b2fdc7..9c35c83e9 100644 --- a/agrona/src/main/java/org/agrona/AsciiEncoding.java +++ b/agrona/src/main/java/org/agrona/AsciiEncoding.java @@ -116,6 +116,8 @@ public static int getDigit(final int index, final char value) * @param cs to parse. * @param index at which the number begins. * @param length of the encoded number in characters. + * @throws AsciiNumberFormatException if cs is not an int value + * @throws IndexOutOfBoundsException if cs is empty * @return the parsed value. */ public static int parseIntAscii(final CharSequence cs, final int index, final int length) @@ -124,7 +126,7 @@ public static int parseIntAscii(final CharSequence cs, final int index, final in final int first = cs.charAt(index); int i = index; - if (first == MINUS_SIGN) + if (first == MINUS_SIGN && length > 1) { i++; } @@ -149,6 +151,8 @@ public static int parseIntAscii(final CharSequence cs, final int index, final in * @param cs to parse. * @param index at which the number begins. * @param length of the encoded number in characters. + * @throws AsciiNumberFormatException if cs is not a long value + * @throws IndexOutOfBoundsException if cs is empty * @return the parsed value. */ public static long parseLongAscii(final CharSequence cs, final int index, final int length) @@ -157,7 +161,7 @@ public static long parseLongAscii(final CharSequence cs, final int index, final final int first = cs.charAt(index); int i = index; - if (first == MINUS_SIGN) + if (first == MINUS_SIGN && length > 1) { i++; } diff --git a/agrona/src/test/java/org/agrona/AsciiEncodingTest.java b/agrona/src/test/java/org/agrona/AsciiEncodingTest.java index 69fe0baf6..e5dc00efe 100644 --- a/agrona/src/test/java/org/agrona/AsciiEncodingTest.java +++ b/agrona/src/test/java/org/agrona/AsciiEncodingTest.java @@ -43,14 +43,44 @@ public void shouldParseLong() } @Test(expected = AsciiNumberFormatException.class) - public void shouldThrowExceptionWhenDecodingCharNonAsciiValue() + public void shouldThrowExceptionWhenDecodingCharNonNumericValue() { AsciiEncoding.getDigit(0, 'a'); } @Test(expected = AsciiNumberFormatException.class) - public void shouldThrowExceptionWhenDecodingByteNonAsciiValue() + public void shouldThrowExceptionWhenDecodingByteNonNumericValue() { AsciiEncoding.getDigit(0, (byte)'a'); } + + @Test(expected = AsciiNumberFormatException.class) + public void shouldThrowExceptionWhenParsingLongContainingLoneMinusSign() + { + AsciiEncoding.parseLongAscii("-", 0, 1); + } + + @Test(expected = AsciiNumberFormatException.class) + public void shouldThrowExceptionWhenParsingIntegerContainingLoneMinusSign() + { + AsciiEncoding.parseIntAscii("-", 0, 1); + } + + @Test(expected = AsciiNumberFormatException.class) + public void shouldThrowExceptionWhenParsingLongContainingLonePlusSign() + { + AsciiEncoding.parseLongAscii("+", 0, 1); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void shouldThrowExceptionWhenParsingEmptyLong() + { + AsciiEncoding.parseLongAscii("", 0, 0); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void shouldThrowExceptionWhenParsingEmptyInteger() + { + AsciiEncoding.parseIntAscii("", 0, 0); + } } \ No newline at end of file