olabusayoT commented on code in PR #1652:
URL: https://github.com/apache/daffodil/pull/1652#discussion_r3406921734


##########
daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/parsers/ParserTraits.scala:
##########
@@ -170,18 +171,82 @@ trait PrefixedLengthParserMixin {
  * Some parsers do not calculate their own length, but instead expect another 
parser
  * to set the bit limit, and then they use that bit limit as the length.
  * An example of this is prefix length parsers. This trait can be used by those
- * parsers to do determine the length based on the bitLimit and position.
+ * parsers to determine the length based on the bitLimit and position.
+ *
+ * For dfdl:lengthKind='endOfParent' parsers that need to scan to end-of-stream
+ * when no bit limit is set, mix in [[EndOfParentBitLengthMixin]] instead.
  */
 trait BitLengthFromBitLimitMixin {
 
-  def getBitLength(s: ParseOrUnparseState): Int = {
-    val pState = s.asInstanceOf[PState]
-    val len = getLengthInBits(pState)
-    len.toInt
+  def getBitLength(s: ParseOrUnparseState): Int = 
getBitLengthAsInt(s.asInstanceOf[PState])
+
+  /**
+   * getLengthInBits converted to Int with a parse error on overflow.
+   *
+   * The default maxCacheSizeInBytes (256 MiB) yields a maximum bit length of
+   * exactly Int.MaxValue + 1, so a bare .toInt without this guard can overflow
+   * by one bit on a full-cache EOP element.
+   */
+  def getBitLengthAsInt(pstate: PState): Int = {
+    val len = getLengthInBits(pstate)
+    if (pstate.processorStatus ne Success) return 0
+    if (len > Int.MaxValue) {
+      pstate.setFailed(
+        new ParseError(

Review Comment:
   It looks like a mix of processing errors and SDEs, but an SDE in this 
instance makes sense!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to