Skip to content

Commit

Permalink
Fix Bug with valueLength being overwritten after Trim
Browse files Browse the repository at this point in the history
- currently after trimming the value of the element, we set the valueLength, and then overwrite it after returning from the parse that does the trimming. This results in the wrong value for value length. This fixes it by checking if the valueLength has already been set, and only setting it in SpecifiedLengthParserBase.parse if it hasn't
- add tests

DAFFODIL-2658
  • Loading branch information
olabusayoT committed Oct 15, 2024
1 parent 11fcc71 commit 7742ef5
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ sealed abstract class SpecifiedLengthParserBase(eParser: Parser, erd: RuntimeDat
if (pState.processorStatus ne Success) return
val finalEndPos0b = startingBitPos0b + nBits

captureValueLength(pState, ULong(startingBitPos0b), ULong(dis.bitPos0b))
// if we haven't already set the value length, set it now
if (pState.infoset.valueLength.isEndUndef)
captureValueLength(pState, ULong(startingBitPos0b), ULong(dis.bitPos0b))

Assert.invariant(dis eq pState.dataInputStream)
val bitsToSkip = finalEndPos0b - dis.bitPos0b
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
-->

<tdml:testSuite suiteName="PrefixedTests"
description="Section 12 - lengthKind=prefixed" xmlns:tdml="http://www.ibm.com/xmlns/dfdl/testData"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dfdl="http://www.ogf.org/dfdl/dfdl-1.0/"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:ex="http://example.com"
xmlns:tns="http://example.com"
xmlns:dfdlx="http://www.ogf.org/dfdl/dfdl-1.0/extensions"
defaultRoundTrip="onePass">
description="Section 12 - lengthKind=prefixed" xmlns:tdml="http://www.ibm.com/xmlns/dfdl/testData"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dfdl="http://www.ogf.org/dfdl/dfdl-1.0/"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:ex="http://example.com"
xmlns:tns="http://example.com"
xmlns:dfdlx="http://www.ogf.org/dfdl/dfdl-1.0/extensions"
defaultRoundTrip="onePass" xmlns:x="http://www.ogf.org/dfdl/dfdl-1.0/">

<tdml:defineSchema name="lengthKindPrefixed-text.dfdl.xsd">

Expand Down Expand Up @@ -2314,10 +2314,31 @@
<xs:element name="root7">
<xs:complexType>
<xs:sequence>
<xs:element name="s" type="xs:string" dfdl:lengthKind="prefixed"
<xs:element name="s" dfdl:lengthKind="prefixed"
dfdl:prefixLengthType="ex:prefixType2"
dfdl:prefixIncludesPrefixLength="yes"
dfdl:textTrimKind="padChar"
dfdl:textPadKind="padChar"
dfdl:textStringPadCharacter="%SP;"
dfdl:textStringJustification="center">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="7"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="e" type="xs:int" dfdl:inputValueCalc='{ dfdl:valueLength(../s, "bytes") }'>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="root8">
<xs:complexType>
<xs:sequence>
<xs:element name="s" type="xs:string" dfdl:lengthKind="explicit"
dfdl:length="7"
dfdl:textTrimKind="padChar"
dfdl:textPadKind="none"
dfdl:textPadKind="padChar"
dfdl:textStringPadCharacter="%SP;"
dfdl:textStringJustification="center"/>
<xs:element name="e" type="xs:int" dfdl:inputValueCalc='{ dfdl:valueLength(../s, "bytes") }'>
Expand Down Expand Up @@ -2423,8 +2444,10 @@
</tdml:infoset>
</tdml:parserTestCase>

<!-- This will not roundtrip due to DAFFODIL-2943, which incorrectly pads around the prefix length-->
<tdml:parserTestCase name="pl_simpleValueLengthBytes_1"
model="lengthKindPrefixed-2">
model="lengthKindPrefixed-2"
roundTrip="none">
<tdml:document>
<tdml:documentPart type="byte">00 00 00 0B</tdml:documentPart>
<tdml:documentPart type="text"> ABC </tdml:documentPart>
Expand All @@ -2439,6 +2462,21 @@
</tdml:infoset>
</tdml:parserTestCase>

<tdml:parserTestCase name="pl_simpleValueLengthBytes_2"
model="lengthKindPrefixed-2" roundTrip="onePass">
<tdml:document>
<tdml:documentPart type="text"> ABC </tdml:documentPart>
</tdml:document>
<tdml:infoset>
<tdml:dfdlInfoset>
<ex:root8>
<s>ABC</s>
<e>3</e>
</ex:root8>
</tdml:dfdlInfoset>
</tdml:infoset>
</tdml:parserTestCase>

<tdml:defineSchema name="lengthKindPrefixed-3"
useDefaultNamespace="false"
elementFormDefault="unqualified">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,12 @@ class TestLengthKindPrefixed {
}

// DAFFODIL-2658
// @Test def test_pl_simpleValueLengthBytes_1() = { runner.runOneTest("pl_simpleValueLengthBytes_1")}
@Test def test_pl_simpleValueLengthBytes_1() = {
runner.runOneTest("pl_simpleValueLengthBytes_1")
}
@Test def test_pl_simpleValueLengthBytes_2() = {
runner.runOneTest("pl_simpleValueLengthBytes_2")
}

@Test def test_pl_simpleContentLengthCharacters_1() = {
runner.runOneTest("pl_simpleContentLengthCharacters_1")
Expand Down

0 comments on commit 7742ef5

Please sign in to comment.