Skip to content

Commit c7db9c0

Browse files
SDE if dfdlx:bitAnd, dfdlx:bitOr, or dfdlx:bitXor arguments do not match signedness
These functions are not well defined if the arguments do not match in signedness. This modifies these functions to create a schema definition error if the functions do not match in signedness, which matches the extension documentation. DAFFODIL-3049
1 parent 67053f9 commit c7db9c0

File tree

5 files changed

+157
-9
lines changed

5 files changed

+157
-9
lines changed

daffodil-core/src/main/scala/org/apache/daffodil/core/dpath/Expression.scala

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2467,14 +2467,21 @@ case class DFDLXBitBinaryExpr(
24672467
override lazy val inherentType = {
24682468
val arg0Type = args(0).inherentType
24692469
val arg1Type = args(1).inherentType
2470+
2471+
(arg0Type, arg1Type) match {
2472+
case (_: NodeInfo.Long.Kind, _: NodeInfo.Long.Kind) => // noop
2473+
case (_: NodeInfo.UnsignedLong.Kind, _: NodeInfo.UnsignedLong.Kind) => // noop
2474+
case _ => {
2475+
SDE(
2476+
"Both arguments for %s must be either xs:unsignedLong or xs:long or a subtype of those, and must match in signedness, but was %s and %s",
2477+
nameAsParsed,
2478+
arg0Type.globalQName,
2479+
arg1Type.globalQName
2480+
)
2481+
}
2482+
}
2483+
24702484
val argInherentType = if (arg1Type.isSubtypeOf(arg0Type)) arg0Type else arg1Type
2471-
schemaDefinitionUnless(
2472-
argInherentType.isSubtypeOf(NodeInfo.PrimType.UnsignedLong) || argInherentType
2473-
.isSubtypeOf(NodeInfo.PrimType.Long),
2474-
"Both arguments for %s must be either xs:unsignedLong or xs:long or a subtype of those, but was %s.",
2475-
nameAsParsed,
2476-
argInherentType.globalQName
2477-
)
24782485
argInherentType
24792486
}
24802487
override def targetTypeForSubexpression(subexp: Expression): NodeInfo.Kind = inherentType

daffodil-test/src/test/resources/org/apache/daffodil/section23/dfdl_functions/BitFunctionsAnd.tdml

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,18 @@
164164
</xs:complexType>
165165
</xs:element>
166166

167+
<xs:element name="testIntegerAnd" type="xs:integer"
168+
dfdl:inputValueCalc="{ dfdlx:bitAnd(xs:integer(1), xs:long(1)) }" />
169+
170+
<xs:element name="testNonNegativeIntegerAnd" type="xs:nonNegativeInteger"
171+
dfdl:inputValueCalc="{ dfdlx:bitAnd(xs:nonNegativeInteger(1), xs:unsignedLong(1)) }" />
172+
173+
<xs:element name="testDifferentSignednessAnd" type="xs:long"
174+
dfdl:inputValueCalc="{ dfdlx:bitAnd(xs:long(1), xs:unsignedLong(1)) }" />
175+
176+
<xs:element name="testPromotionAnd" type="xs:long"
177+
dfdl:inputValueCalc="{ dfdlx:bitAnd(xs:byte(1), xs:short(257)) }" />
178+
167179
</tdml:defineSchema>
168180

169181
<tdml:parserTestCase name="testIntAnd" root="testIntAnd" model="BitFunctions">
@@ -326,4 +338,35 @@
326338
</tdml:dfdlInfoset>
327339
</tdml:infoset>
328340
</tdml:parserTestCase>
329-
</tdml:testSuite>
341+
342+
<tdml:parserTestCase name="testIntegerAnd" root="testIntegerAnd" model="BitFunctions">
343+
<tdml:document></tdml:document>
344+
<tdml:errors>
345+
<tdml:error>xs:unsignedLong or xs:long or a subtype of those</tdml:error>
346+
</tdml:errors>
347+
</tdml:parserTestCase>
348+
349+
<tdml:parserTestCase name="testNonNegativeIntegerAnd" root="testNonNegativeIntegerAnd" model="BitFunctions">
350+
<tdml:document></tdml:document>
351+
<tdml:errors>
352+
<tdml:error>xs:unsignedLong or xs:long or a subtype of those</tdml:error>
353+
</tdml:errors>
354+
</tdml:parserTestCase>
355+
356+
<tdml:parserTestCase name="testDifferentSignednessAnd" root="testDifferentSignednessAnd" model="BitFunctions">
357+
<tdml:document></tdml:document>
358+
<tdml:errors>
359+
<tdml:error>must match in signedness</tdml:error>
360+
</tdml:errors>
361+
</tdml:parserTestCase>
362+
363+
<tdml:parserTestCase name="testPromotionAnd" root="testPromotionAnd" model="BitFunctions">
364+
<tdml:document></tdml:document>
365+
<tdml:infoset>
366+
<tdml:dfdlInfoset>
367+
<ex:testPromotionAnd>1</ex:testPromotionAnd>
368+
</tdml:dfdlInfoset>
369+
</tdml:infoset>
370+
</tdml:parserTestCase>
371+
372+
</tdml:testSuite>

daffodil-test/src/test/resources/org/apache/daffodil/section23/dfdl_functions/BitFunctionsOr.tdml

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,18 @@
164164
</xs:complexType>
165165
</xs:element>
166166

167+
<xs:element name="testIntegerOr" type="xs:integer"
168+
dfdl:inputValueCalc="{ dfdlx:bitOr(xs:integer(1), xs:long(1)) }" />
169+
170+
<xs:element name="testNonNegativeIntegerOr" type="xs:nonNegativeInteger"
171+
dfdl:inputValueCalc="{ dfdlx:bitOr(xs:nonNegativeInteger(1), xs:unsignedLong(1)) }" />
172+
173+
<xs:element name="testDifferentSignednessOr" type="xs:long"
174+
dfdl:inputValueCalc="{ dfdlx:bitOr(xs:long(1), xs:unsignedLong(1)) }" />
175+
176+
<xs:element name="testPromotionOr" type="xs:long"
177+
dfdl:inputValueCalc="{ dfdlx:bitOr(xs:byte(1), xs:short(256)) }" />
178+
167179
</tdml:defineSchema>
168180

169181
<tdml:parserTestCase name="testIntOr" root="testIntOr" model="BitFunctions">
@@ -326,4 +338,35 @@
326338
</tdml:dfdlInfoset>
327339
</tdml:infoset>
328340
</tdml:parserTestCase>
329-
</tdml:testSuite>
341+
342+
<tdml:parserTestCase name="testIntegerOr" root="testIntegerOr" model="BitFunctions">
343+
<tdml:document></tdml:document>
344+
<tdml:errors>
345+
<tdml:error>xs:unsignedLong or xs:long or a subtype of those</tdml:error>
346+
</tdml:errors>
347+
</tdml:parserTestCase>
348+
349+
<tdml:parserTestCase name="testNonNegativeIntegerOr" root="testNonNegativeIntegerOr" model="BitFunctions">
350+
<tdml:document></tdml:document>
351+
<tdml:errors>
352+
<tdml:error>xs:unsignedLong or xs:long or a subtype of those</tdml:error>
353+
</tdml:errors>
354+
</tdml:parserTestCase>
355+
356+
<tdml:parserTestCase name="testDifferentSignednessOr" root="testDifferentSignednessOr" model="BitFunctions">
357+
<tdml:document></tdml:document>
358+
<tdml:errors>
359+
<tdml:error>must match in signedness</tdml:error>
360+
</tdml:errors>
361+
</tdml:parserTestCase>
362+
363+
<tdml:parserTestCase name="testPromotionOr" root="testPromotionOr" model="BitFunctions">
364+
<tdml:document></tdml:document>
365+
<tdml:infoset>
366+
<tdml:dfdlInfoset>
367+
<ex:testPromotionOr>257</ex:testPromotionOr>
368+
</tdml:dfdlInfoset>
369+
</tdml:infoset>
370+
</tdml:parserTestCase>
371+
372+
</tdml:testSuite>

daffodil-test/src/test/resources/org/apache/daffodil/section23/dfdl_functions/BitFunctionsXor.tdml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,18 @@
166166
</xs:complexType>
167167
</xs:element>
168168

169+
<xs:element name="testIntegerXor" type="xs:integer"
170+
dfdl:inputValueCalc="{ dfdlx:bitXor(xs:integer(1), xs:long(1)) }" />
171+
172+
<xs:element name="testNonNegativeIntegerXor" type="xs:nonNegativeInteger"
173+
dfdl:inputValueCalc="{ dfdlx:bitXor(xs:nonNegativeInteger(1), xs:unsignedLong(1)) }" />
174+
175+
<xs:element name="testDifferentSignednessXor" type="xs:long"
176+
dfdl:inputValueCalc="{ dfdlx:bitXor(xs:long(1), xs:unsignedLong(1)) }" />
177+
178+
<xs:element name="testPromotionXor" type="xs:long"
179+
dfdl:inputValueCalc="{ dfdlx:bitXor(xs:byte(3), xs:short(257)) }" />
180+
169181
</tdml:defineSchema>
170182

171183
<tdml:parserTestCase name="testIntXor" root="testIntXor" model="BitFunctions">
@@ -328,4 +340,35 @@
328340
</tdml:dfdlInfoset>
329341
</tdml:infoset>
330342
</tdml:parserTestCase>
343+
344+
<tdml:parserTestCase name="testIntegerXor" root="testIntegerXor" model="BitFunctions">
345+
<tdml:document></tdml:document>
346+
<tdml:errors>
347+
<tdml:error>xs:unsignedLong or xs:long or a subtype of those</tdml:error>
348+
</tdml:errors>
349+
</tdml:parserTestCase>
350+
351+
<tdml:parserTestCase name="testNonNegativeIntegerXor" root="testNonNegativeIntegerXor" model="BitFunctions">
352+
<tdml:document></tdml:document>
353+
<tdml:errors>
354+
<tdml:error>xs:unsignedLong or xs:long or a subtype of those</tdml:error>
355+
</tdml:errors>
356+
</tdml:parserTestCase>
357+
358+
<tdml:parserTestCase name="testDifferentSignednessXor" root="testDifferentSignednessXor" model="BitFunctions">
359+
<tdml:document></tdml:document>
360+
<tdml:errors>
361+
<tdml:error>must match in signedness</tdml:error>
362+
</tdml:errors>
363+
</tdml:parserTestCase>
364+
365+
<tdml:parserTestCase name="testPromotionXor" root="testPromotionXor" model="BitFunctions">
366+
<tdml:document></tdml:document>
367+
<tdml:infoset>
368+
<tdml:dfdlInfoset>
369+
<ex:testPromotionXor>258</ex:testPromotionXor>
370+
</tdml:dfdlInfoset>
371+
</tdml:infoset>
372+
</tdml:parserTestCase>
373+
331374
</tdml:testSuite>

daffodil-test/src/test/scala/org/apache/daffodil/section23/dfdl_expressions/TestBitFunctions.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ class TestBitFunctionsXor extends TdmlTests {
8484
@Test def testUnsignedLongXor = test
8585
@Test def testUnsignedShortXor = test
8686
@Test def testUnsignedByteXor = test
87+
@Test def testIntegerXor = test
88+
@Test def testNonNegativeIntegerXor = test
89+
@Test def testDifferentSignednessXor = test
90+
@Test def testPromotionXor = test
8791
}
8892

8993
class TestBitFunctionsOr extends TdmlTests {
@@ -97,6 +101,10 @@ class TestBitFunctionsOr extends TdmlTests {
97101
@Test def testUnsignedLongOr = test
98102
@Test def testUnsignedShortOr = test
99103
@Test def testUnsignedByteOr = test
104+
@Test def testIntegerOr = test
105+
@Test def testNonNegativeIntegerOr = test
106+
@Test def testDifferentSignednessOr = test
107+
@Test def testPromotionOr = test
100108
}
101109

102110
class TestBitFunctionsAnd extends TdmlTests {
@@ -110,6 +118,10 @@ class TestBitFunctionsAnd extends TdmlTests {
110118
@Test def testUnsignedLongAnd = test
111119
@Test def testUnsignedShortAnd = test
112120
@Test def testUnsignedByteAnd = test
121+
@Test def testIntegerAnd = test
122+
@Test def testNonNegativeIntegerAnd = test
123+
@Test def testDifferentSignednessAnd = test
124+
@Test def testPromotionAnd = test
113125
}
114126

115127
class TestBitFunctionsNot extends TdmlTests {

0 commit comments

Comments
 (0)