Skip to content

Commit

Permalink
fixup! fixup! fixup! fixup! Parse Error on Out of Range Binary Integers
Browse files Browse the repository at this point in the history
- added more decimal tests
- updated PE to be dynamic for signed and unsigned packed integers
- added PE for packed decimal with length 0

DAFFODIL-2297
  • Loading branch information
olabusayoT committed Oct 29, 2024
1 parent fd8e9a9 commit 0c3e934
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@ abstract class PackedBinaryDecimalBaseParser(

def parse(start: PState): Unit = {
val nBits = getBitLength(start)
if (nBits == 0) return // zero length is used for outputValueCalc often.
if (nBits == 0) {
PE(

Check warning on line 56 in daffodil-runtime1/src/main/scala/org/apache/daffodil/runtime1/processors/PackedBinaryTraits.scala

View check run for this annotation

Codecov / codecov/patch

daffodil-runtime1/src/main/scala/org/apache/daffodil/runtime1/processors/PackedBinaryTraits.scala#L56

Added line #L56 was not covered by tests
start,
"Number of bits %d out of range for a binary decimal. " +
"An unsigned decimal with length 1 bit could be used instead.",
nBits
)
return

Check warning on line 62 in daffodil-runtime1/src/main/scala/org/apache/daffodil/runtime1/processors/PackedBinaryTraits.scala

View check run for this annotation

Codecov / codecov/patch

daffodil-runtime1/src/main/scala/org/apache/daffodil/runtime1/processors/PackedBinaryTraits.scala#L62

Added line #L62 was not covered by tests
}
val dis = start.dataInputStream

if (!dis.isDefinedForLength(nBits)) {
Expand Down Expand Up @@ -87,10 +95,14 @@ abstract class PackedBinaryIntegerBaseParser(
def parse(start: PState): Unit = {
val nBits = getBitLength(start)
if (nBits == 0) {
val signedStr = if (signed) "a signed" else "an unsigned"
val minWidth = if (signed) 2 else 1
PE(
start,
"Minimum length for an unsigned binary integer is 1 bit(s), number of bits %d out of range. " +
"Minimum length for %s binary integer is %d bit(s), number of bits %d out of range. " +
"An unsigned integer with length 1 bit could be used instead.",
signedStr,
minWidth,
nBits
)
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1453,7 +1453,7 @@
<xs:element name="packed1">
<xs:complexType>
<xs:sequence>
<xs:element name="len" type="xs:unsignedByte" dfdl:lengthUnits="bytes" dfdl:inputValueCalc="{ 2 }"/>
<xs:element name="len" type="xs:unsignedByte" dfdl:lengthUnits="bytes"/>
<xs:element name="e1" dfdl:binaryNumberRep="packed" type="xs:int"
dfdl:lengthKind="explicit" dfdl:lengthUnits="bytes" dfdl:length="{ ../ex:len }"
dfdl:binaryPackedSignCodes="C D F C"/>
Expand All @@ -1464,12 +1464,32 @@
<xs:element name="packed2">
<xs:complexType>
<xs:sequence>
<xs:element name="len" type="xs:unsignedByte" dfdl:lengthUnits="bytes" dfdl:inputValueCalc="{ 2 }"/>
<xs:element name="len" type="xs:unsignedByte" dfdl:lengthUnits="bytes"/>
<xs:element name="e1" dfdl:binaryNumberRep="ibm4690Packed" type="xs:int"
dfdl:lengthKind="explicit" dfdl:lengthUnits="bytes" dfdl:length="{ ../ex:len }"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="packed3">
<xs:complexType>
<xs:sequence>
<xs:element name="len" type="xs:unsignedByte" dfdl:lengthUnits="bytes"/>
<xs:element name="e1" dfdl:binaryNumberRep="packed" type="xs:unsignedInt"
dfdl:lengthKind="explicit" dfdl:lengthUnits="bytes" dfdl:length="{ ../ex:len }"
dfdl:binaryPackedSignCodes="C D F C"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="packed4">
<xs:complexType>
<xs:sequence>
<xs:element name="len" type="xs:unsignedByte" dfdl:lengthUnits="bytes"/>
<xs:element name="e1" dfdl:binaryNumberRep="ibm4690Packed" type="xs:unsignedInt"
dfdl:lengthKind="explicit" dfdl:lengthUnits="bytes" dfdl:length="{ ../ex:len }"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</tdml:defineSchema>

<tdml:defineConfig name="disallowSignedIntegerLength1Bit">
Expand Down Expand Up @@ -1641,6 +1661,62 @@
</tdml:warnings>
</tdml:parserTestCase>

<tdml:parserTestCase name="outOfRangeLengthBinaryInteger10" root="packed1" model="BinaryIntegers"
description="packed signed explicit binary integer"
ignoreUnexpectedWarnings="false">
<tdml:document>
<tdml:documentPart type="byte">00123C</tdml:documentPart>
</tdml:document>
<tdml:errors>
<tdml:error>Parse Error</tdml:error>
<tdml:error>signed binary integer</tdml:error>
<tdml:error>2 bit(s)</tdml:error>
<tdml:error>0 out of range</tdml:error>
</tdml:errors>
</tdml:parserTestCase>

<tdml:parserTestCase name="outOfRangeLengthBinaryInteger11" root="packed2" model="BinaryIntegers"
description="ibm4690 packed signed explicit binary integer"
ignoreUnexpectedWarnings="false">
<tdml:document>
<tdml:documentPart type="byte">001088</tdml:documentPart>
</tdml:document>
<tdml:errors>
<tdml:error>Parse Error</tdml:error>
<tdml:error>signed binary integer</tdml:error>
<tdml:error>2 bit(s)</tdml:error>
<tdml:error>0 out of range</tdml:error>
</tdml:errors>
</tdml:parserTestCase>

<tdml:parserTestCase name="outOfRangeLengthBinaryInteger12" root="packed3" model="BinaryIntegers"
description="packed unsigned explicit binary integer"
ignoreUnexpectedWarnings="false">
<tdml:document>
<tdml:documentPart type="byte">00123C</tdml:documentPart>
</tdml:document>
<tdml:errors>
<tdml:error>Parse Error</tdml:error>
<tdml:error>unsigned binary integer</tdml:error>
<tdml:error>1 bit(s)</tdml:error>
<tdml:error>0 out of range</tdml:error>
</tdml:errors>
</tdml:parserTestCase>

<tdml:parserTestCase name="outOfRangeLengthBinaryInteger13" root="packed4" model="BinaryIntegers"
description="ibm4690 packed signed explicit binary integer"
ignoreUnexpectedWarnings="false">
<tdml:document>
<tdml:documentPart type="byte">001088</tdml:documentPart>
</tdml:document>
<tdml:errors>
<tdml:error>Parse Error</tdml:error>
<tdml:error>unsigned binary integer</tdml:error>
<tdml:error>1 bit(s)</tdml:error>
<tdml:error>0 out of range</tdml:error>
</tdml:errors>
</tdml:parserTestCase>

<tdml:parserTestCase name="inRangeLengthBinaryInteger1" root="spare2" model="BinaryIntegers"
description="No Parse Error when attempting to use in range unsigned binary integer">
<tdml:document>
Expand Down Expand Up @@ -1669,36 +1745,32 @@
</tdml:infoset>
</tdml:parserTestCase>

<tdml:unparserTestCase name="inRangeLengthBinaryInteger3" root="spare7" model="BinaryIntegers"
description="No Unparse Error when attempting to use in range signed binary integer">
<tdml:unparserTestCase name="inRangeLengthBinaryInteger3" root="spare4" model="BinaryIntegers"
description="No Unparse Error when attempting to use in range signed binary integer">
<tdml:document>
<tdml:documentPart type="byte">02</tdml:documentPart>
<tdml:documentPart type="bits">11</tdml:documentPart>
<tdml:documentPart type="bits">
11
</tdml:documentPart>
</tdml:document>
<tdml:infoset>
<tdml:dfdlInfoset>
<ex:spare7>
<ex:len>2</ex:len>
<ex:e1>-1</ex:e1>
</ex:spare7>
<ex:spare4>-1</ex:spare4>
</tdml:dfdlInfoset>
</tdml:infoset>
</tdml:unparserTestCase>

<tdml:unparserTestCase name="inRangeLengthBinaryInteger4" root="spare6" model="BinaryIntegers"
description="No Unparse Error when attempting to use in range unsigned binary integer">
<tdml:unparserTestCase name="inRangeLengthBinaryInteger4" root="spare2" model="BinaryIntegers"
description="No Unparse Error when attempting to use in range unsigned binary integer">
<tdml:document>
<tdml:documentPart type="bits">
1
</tdml:documentPart>
</tdml:document>
<tdml:infoset>
<tdml:dfdlInfoset>
<ex:spare6>
<ex:len>1</ex:len>
<ex:e1>1</ex:e1>
</ex:spare6>
<ex:spare2>1</ex:spare2>
</tdml:dfdlInfoset>
</tdml:infoset>
<tdml:document>
<tdml:documentPart type="byte">01</tdml:documentPart>
<tdml:documentPart type="bits">1</tdml:documentPart>
</tdml:document>
</tdml:unparserTestCase>

<tdml:unparserTestCase name="inRangeLengthBinaryInteger5" root="spare7" model="BinaryIntegers"
Expand All @@ -1722,7 +1794,7 @@
description="packed explicit binary integer"
ignoreUnexpectedWarnings="false">
<tdml:document>
<tdml:documentPart type="byte">123C</tdml:documentPart>
<tdml:documentPart type="byte">02123C</tdml:documentPart>
</tdml:document>
<tdml:infoset>
<tdml:dfdlInfoset>
Expand All @@ -1738,7 +1810,7 @@
description="ibm4690 packed explicit binary integer"
ignoreUnexpectedWarnings="false">
<tdml:document>
<tdml:documentPart type="byte">1088</tdml:documentPart>
<tdml:documentPart type="byte">021088</tdml:documentPart>
</tdml:document>
<tdml:infoset>
<tdml:dfdlInfoset>
Expand All @@ -1750,6 +1822,38 @@
</tdml:infoset>
</tdml:parserTestCase>

<tdml:unparserTestCase name="inRangeLengthBinaryInteger8" root="spare7" model="BinaryIntegers"
description="No Unparse Error when attempting to use in range signed binary integer">
<tdml:document>
<tdml:documentPart type="byte">02</tdml:documentPart>
<tdml:documentPart type="bits">11</tdml:documentPart>
</tdml:document>
<tdml:infoset>
<tdml:dfdlInfoset>
<ex:spare7>
<ex:len>2</ex:len>
<ex:e1>-1</ex:e1>
</ex:spare7>
</tdml:dfdlInfoset>
</tdml:infoset>
</tdml:unparserTestCase>

<tdml:unparserTestCase name="inRangeLengthBinaryInteger9" root="spare6" model="BinaryIntegers"
description="No Unparse Error when attempting to use in range unsigned binary integer">
<tdml:infoset>
<tdml:dfdlInfoset>
<ex:spare6>
<ex:len>1</ex:len>
<ex:e1>1</ex:e1>
</ex:spare6>
</tdml:dfdlInfoset>
</tdml:infoset>
<tdml:document>
<tdml:documentPart type="byte">01</tdml:documentPart>
<tdml:documentPart type="bits">1</tdml:documentPart>
</tdml:document>
</tdml:unparserTestCase>

<tdml:defineSchema name="BinaryDecimals">
<xs:include schemaLocation="/org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>
<dfdl:format ref="ex:GeneralFormat" binaryDecimalVirtualPoint="0" lengthKind="explicit" representation="binary" lengthUnits="bits"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,18 @@ class TestLengthKindExplicit {
@Test def test_outOfRangeLengthBinaryInteger9(): Unit = {
runner.runOneTest("outOfRangeLengthBinaryInteger9")
}
@Test def test_outOfRangeLengthBinaryInteger10(): Unit = {
runner.runOneTest("outOfRangeLengthBinaryInteger10")
}
@Test def test_outOfRangeLengthBinaryInteger11(): Unit = {
runner.runOneTest("outOfRangeLengthBinaryInteger11")
}
@Test def test_outOfRangeLengthBinaryInteger12(): Unit = {
runner.runOneTest("outOfRangeLengthBinaryInteger12")
}
@Test def test_outOfRangeLengthBinaryInteger13(): Unit = {
runner.runOneTest("outOfRangeLengthBinaryInteger13")
}
@Test def test_inRangeLengthBinaryInteger1(): Unit = {
runner.runOneTest("inRangeLengthBinaryInteger1")
}
Expand All @@ -214,6 +226,12 @@ class TestLengthKindExplicit {
@Test def test_inRangeLengthBinaryInteger7(): Unit = {
runner.runOneTest("inRangeLengthBinaryInteger7")
}
@Test def test_inRangeLengthBinaryInteger8(): Unit = {
runner.runOneTest("inRangeLengthBinaryInteger8")
}
@Test def test_inRangeLengthBinaryInteger9(): Unit = {
runner.runOneTest("inRangeLengthBinaryInteger9")
}
@Test def test_outOfRangeLengthBinaryDecimal1(): Unit = {
runner.runOneTest("outOfRangeLengthBinaryDecimal1")
}
Expand Down

0 comments on commit 0c3e934

Please sign in to comment.