You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@daffodil.apache.org by sl...@apache.org on 2021/06/14 16:41:19 UTC
[daffodil] branch master updated: Fix unparsing large xs:hexBinary
This is an automated email from the ASF dual-hosted git repository.
slawrence pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/daffodil.git
The following commit(s) were added to refs/heads/master by this push:
new e08b42f Fix unparsing large xs:hexBinary
e08b42f is described below
commit e08b42fe7c27a6032eb3b1076087869f4dfd28e5
Author: Steve Lawrence <sl...@apache.org>
AuthorDate: Fri Jun 11 08:30:19 2021 -0400
Fix unparsing large xs:hexBinary
In some places, we use an Int for variables referring to the length in
bits of xs:hexBinary data. This means we can only unparse MaxInt bits of
hexBinary data, which is only 256MB. This modifies hex binary unparsing
to use Long's in these cases to ensure we can support large hex binary
blobs. The maxLenghHexBinaryInBits tunable is also checked during
unparsing, which ensures we won't try to unparse more than 2GB of
hexBinary data, which is a limitation of Java byte arrays.
DAFFODIL-2532
---
.../org/apache/daffodil/io/DataOutputStream.scala | 2 +-
.../daffodil/io/DataOutputStreamImplMixin.scala | 20 ++++++++-----
.../unparsers/HexBinaryLengthUnparser.scala | 34 ++++++++++++++++------
.../daffodil/section00/general/tunables.tdml | 21 +++++++++++++
.../section05/simple_types/SimpleTypesUnparse.tdml | 7 +++--
.../daffodil/section00/general/TestGeneral.scala | 1 +
6 files changed, 65 insertions(+), 20 deletions(-)
diff --git a/daffodil-io/src/main/scala/org/apache/daffodil/io/DataOutputStream.scala b/daffodil-io/src/main/scala/org/apache/daffodil/io/DataOutputStream.scala
index 56b29dc..b14efc5 100644
--- a/daffodil-io/src/main/scala/org/apache/daffodil/io/DataOutputStream.scala
+++ b/daffodil-io/src/main/scala/org/apache/daffodil/io/DataOutputStream.scala
@@ -196,7 +196,7 @@ trait DataOutputStream extends DataStreamCommon
* It is a usage error if bitLengthFrom1 is not greater than or equal to 1.
*
*/
- def putByteArray(ba: Array[Byte], bitLengthFrom1: Int, finfo: FormatInfo, ignoreByteOrder: Boolean = false): Boolean
+ def putByteArray(ba: Array[Byte], bitLengthFrom1: Long, finfo: FormatInfo, ignoreByteOrder: Boolean = false): Boolean
/**
* Float and Double
diff --git a/daffodil-io/src/main/scala/org/apache/daffodil/io/DataOutputStreamImplMixin.scala b/daffodil-io/src/main/scala/org/apache/daffodil/io/DataOutputStreamImplMixin.scala
index 2ae1298..6192e1d 100644
--- a/daffodil-io/src/main/scala/org/apache/daffodil/io/DataOutputStreamImplMixin.scala
+++ b/daffodil-io/src/main/scala/org/apache/daffodil/io/DataOutputStreamImplMixin.scala
@@ -474,13 +474,15 @@ trait DataOutputStreamImplMixin extends DataStreamCommonState
final override def putByteArray(
array: Array[Byte],
- bitLengthFrom1: Int,
+ bitLengthFrom1: Long,
finfo: FormatInfo,
ignoreByteOrder: Boolean = false): Boolean = {
// this is to be used for an array generated by getByteArray. Thus, this
// array is expected by to BigEndian MSBF. It must be transformed into an
- // array that the other putBytes/Bits/etc functions can accept
- Assert.usage(bitLengthFrom1 >= 1)
+ // array that the other putBytes/Bits/etc functions can accept. The length
+ // in bits must be positive and must be <= the max size of an array in
+ // bits, which is IntMax * 8
+ Assert.usage(bitLengthFrom1 >= 1 && bitLengthFrom1 <= Int.MaxValue.toLong * 8)
Assert.usage(isWritable)
val res =
@@ -488,8 +490,9 @@ trait DataOutputStreamImplMixin extends DataStreamCommonState
false
} else {
// Cannot rely on array.length since it is possible the array is bigger
- // than the number of bits we should put
- val bytesToPutFromArray = (bitLengthFrom1 + 7) / 8
+ // than the number of bits we should put. This value is guaranteed to
+ // fit in an integer due to the assertion at the top, so toInt is safe
+ val bytesToPutFromArray = ((bitLengthFrom1 + 7) / 8).toInt
// First thing we need to do is reverse the array if it's littleEndian,
// but only bother checking if the bilength is more than a byte--single
@@ -520,13 +523,14 @@ trait DataOutputStreamImplMixin extends DataStreamCommonState
// Determine the length of the new fragment after everything is added
// from the array taking into account the current fragment limit, and
// define a place to store its eventual value
- val newFragmentByteLimit = (fragmentLastByteLimit + bitLengthFrom1) % 8
+ val newFragmentByteLimit = ((fragmentLastByteLimit + bitLengthFrom1) % 8).toInt
var newFragmentByte: Int = 0
if (fragmentLastByteLimit == 0) {
// There is no current fragment byte, so we can just write all the
- // full bytes
- val fullBytes = bitLengthFrom1 / 8
+ // full bytes. Due to the assertion at the top, we know
+ // bitLengthFrom1 / 8 must be <= MaxInt, so toInt is safe
+ val fullBytes = (bitLengthFrom1 / 8).toInt
realStream.write(bytes, 0, fullBytes)
// There still maybe be a fragment byte. If so it will become our new
diff --git a/daffodil-runtime1-unparser/src/main/scala/org/apache/daffodil/processors/unparsers/HexBinaryLengthUnparser.scala b/daffodil-runtime1-unparser/src/main/scala/org/apache/daffodil/processors/unparsers/HexBinaryLengthUnparser.scala
index 286a902..1a2b0eb 100644
--- a/daffodil-runtime1-unparser/src/main/scala/org/apache/daffodil/processors/unparsers/HexBinaryLengthUnparser.scala
+++ b/daffodil-runtime1-unparser/src/main/scala/org/apache/daffodil/processors/unparsers/HexBinaryLengthUnparser.scala
@@ -38,29 +38,45 @@ abstract class HexBinaryUnparserBase(override val context: ElementRuntimeData)
val node = state.currentInfosetNode.asSimple
val value = node.dataValue.getByteArray
- val lengthInBits = getLengthInBits(state)
+ val valueLengthInBytes = value.length.toLong
+ if (valueLengthInBytes > state.tunable.maxHexBinaryLengthInBytes) {
+ UnparseError(
+ One(context.schemaFileLocation),
+ One(state.currentLocation),
+ "Length of xs:hexBinary exceeds maximum of %s bytes: %s",
+ state.tunable.maxHexBinaryLengthInBytes,
+ valueLengthInBytes)
+ }
+
+ val lengthInBits = getLengthInBits(state)
val lengthInBytes = (lengthInBits + 7) / 8
- if (value.length > lengthInBytes) {
- UnparseError(One(context.schemaFileLocation), One(state.currentLocation), "Data length %d bits exceeds explicit length value: %d bits", value.length * 8, lengthInBits)
+
+ if (valueLengthInBytes > lengthInBytes) {
+ UnparseError(
+ One(context.schemaFileLocation),
+ One(state.currentLocation),
+ "Length of xs:hexBinary exceeds calculated length of %s bits: %s",
+ valueLengthInBytes * 8,
+ lengthInBits)
}
val bitsFromValueToPut =
- if (lengthInBytes > value.size) {
+ if (lengthInBytes > valueLengthInBytes) {
// the length to put is larger than the number of available bytes in the
// array. So put the whole array and we'll add fill bytes later
- value.size * 8
+ valueLengthInBytes * 8
} else {
// the length to put is either everything or some fragment of the last
// byte, so put the length in bits. putByte will deal with the fragment byte
- Assert.invariant(lengthInBytes == value.size)
+ Assert.invariant(lengthInBytes == valueLengthInBytes)
lengthInBits
}
val dos = state.dataOutputStream
if (bitsFromValueToPut > 0) {
- val ret = dos.putByteArray(value, bitsFromValueToPut.toInt, state)
+ val ret = dos.putByteArray(value, bitsFromValueToPut, state)
if (!ret) {
UnparseError(One(context.schemaFileLocation), One(state.currentLocation), "Failed to write %d hexBinary bits", bitsFromValueToPut)
}
@@ -81,7 +97,7 @@ class HexBinaryMinLengthInBytesUnparser(minLengthInBytes: Long, erd: ElementRunt
extends HexBinaryUnparserBase(erd) {
override def getLengthInBits(state: UState): Long = {
- val len = state.currentNode.get.asSimple.dataValue.getByteArray.length * 8
+ val len = state.currentNode.get.asSimple.dataValue.getByteArray.length.toLong * 8
val min = minLengthInBytes * 8
scala.math.max(len, min)
}
@@ -96,7 +112,7 @@ final class HexBinarySpecifiedLengthUnparser(erd: ElementRuntimeData, val length
} catch {
case e: RetryableException => {
val bytes = state.currentInfosetNode.asSimple.dataValue.getByteArray
- val len = bytes.length * 8
+ val len = bytes.length.toLong * 8
len
}
}
diff --git a/daffodil-test/src/test/resources/org/apache/daffodil/section00/general/tunables.tdml b/daffodil-test/src/test/resources/org/apache/daffodil/section00/general/tunables.tdml
index 1febe91..fb5adc2 100644
--- a/daffodil-test/src/test/resources/org/apache/daffodil/section00/general/tunables.tdml
+++ b/daffodil-test/src/test/resources/org/apache/daffodil/section00/general/tunables.tdml
@@ -596,4 +596,25 @@
</tdml:parserTestCase>
+ <tdml:unparserTestCase
+ name="maxHexBinaryUnparseError" root="root"
+ model="smallMaxHexBinary"
+ config="cfg_smallMaxHexBinary">
+
+ <tdml:infoset>
+ <tdml:dfdlInfoset>
+ <ex:root>00112233445566778899AA</ex:root>
+ </tdml:dfdlInfoset>
+ </tdml:infoset>
+
+ <tdml:errors>
+ <tdml:error>Unparse Error</tdml:error>
+ <tdml:error>xs:hexBinary</tdml:error>
+ <tdml:error>maximum</tdml:error>
+ <tdml:error>10</tdml:error>
+ <tdml:error>11</tdml:error>
+ </tdml:errors>
+
+ </tdml:unparserTestCase>
+
</tdml:testSuite>
diff --git a/daffodil-test/src/test/resources/org/apache/daffodil/section05/simple_types/SimpleTypesUnparse.tdml b/daffodil-test/src/test/resources/org/apache/daffodil/section05/simple_types/SimpleTypesUnparse.tdml
index d6c476c..054a178 100644
--- a/daffodil-test/src/test/resources/org/apache/daffodil/section05/simple_types/SimpleTypesUnparse.tdml
+++ b/daffodil-test/src/test/resources/org/apache/daffodil/section05/simple_types/SimpleTypesUnparse.tdml
@@ -154,8 +154,11 @@
</tdml:infoset>
<tdml:errors>
<tdml:error>Unparse Error</tdml:error>
- <tdml:error>Data length 40 bits</tdml:error>
- </tdml:errors>
+ <tdml:error>xs:hexBinary</tdml:error>
+ <tdml:error>calculated length</tdml:error>
+ <tdml:error>40 bits</tdml:error>
+ <tdml:error>32</tdml:error>
+ </tdml:errors>
</tdml:unparserTestCase>
<!--
diff --git a/daffodil-test/src/test/scala/org/apache/daffodil/section00/general/TestGeneral.scala b/daffodil-test/src/test/scala/org/apache/daffodil/section00/general/TestGeneral.scala
index 3164041..8c19941 100644
--- a/daffodil-test/src/test/scala/org/apache/daffodil/section00/general/TestGeneral.scala
+++ b/daffodil-test/src/test/scala/org/apache/daffodil/section00/general/TestGeneral.scala
@@ -108,5 +108,6 @@ class TestGeneral {
@Test def test_requireEncodingErrorPolicyTrue(): Unit = { tunables_runner.runOneTest("requireEncodingErrorPolicyTrue") }
@Test def test_requireEncodingErrorPolicyFalse(): Unit = { tunables_runner.runOneTest("requireEncodingErrorPolicyFalse") }
@Test def test_maxHexBinaryError(): Unit = { tunables_runner.runOneTest("maxHexBinaryError") }
+ @Test def test_maxHexBinaryUnparseError(): Unit = { tunables_runner.runOneTest("maxHexBinaryUnparseError") }
}