diff --git a/sbe-tool/src/main/java/uk/co/real_logic/sbe/xml/SetType.java b/sbe-tool/src/main/java/uk/co/real_logic/sbe/xml/SetType.java index 467ac93b80..56ce301fe8 100644 --- a/sbe-tool/src/main/java/uk/co/real_logic/sbe/xml/SetType.java +++ b/sbe-tool/src/main/java/uk/co/real_logic/sbe/xml/SetType.java @@ -223,7 +223,7 @@ public Choice(final Node node, final PrimitiveType encodingType) deprecated = Integer.parseInt(getAttributeValue(node, "deprecated", "0")); // choice values are bit positions (0, 1, 2, 3, 4, etc.) from LSB to MSB - if (value.longValue() >= (encodingType.size() * 8L)) + if (value.longValue() < 0 || value.longValue() >= (encodingType.size() * 8L)) { throw new IllegalArgumentException("Choice value out of bounds: " + value.longValue()); } diff --git a/sbe-tool/src/test/java/uk/co/real_logic/sbe/xml/SetTypeTest.java b/sbe-tool/src/test/java/uk/co/real_logic/sbe/xml/SetTypeTest.java index 60d09daa15..fcaf2a020b 100644 --- a/sbe-tool/src/test/java/uk/co/real_logic/sbe/xml/SetTypeTest.java +++ b/sbe-tool/src/test/java/uk/co/real_logic/sbe/xml/SetTypeTest.java @@ -175,6 +175,21 @@ void shouldThrowExceptionWhenValueOutOfBoundsSpecified() parseTestXmlWithMap("/types/set", testXmlString)); } + @Test + void shouldThrowExceptionWhenNegativeValueSpecified() + { + final String testXmlString = + "" + + "" + + " 0" + + " -1" + + "" + + ""; + + assertThrows(IllegalArgumentException.class, () -> + parseTestXmlWithMap("/types/set", testXmlString)); + } + @Test void shouldHandleEncodingTypesWithNamedTypes() throws Exception {