Repository navigation
Fix Int overflow in binary number lengths - #1760
Open
stevedlawrence wants to merge 1 commit into
Open
stevedlawrence wants to merge 1 commit into
stevedlawrence wants to merge 1 commit into
Conversation
getBitLength currently returns an Int, with implementations often using Long.toInt. This can lead to silent overflow/truncation (e.g. with dfdl:length runtime expressions). To resolve this, getBitLength is modified to return a Long so that no truncation needs to happen. Ultimately these lengths need to be converted to integers for the various IO functions, so additional functions are added to ensure we also check the lengths prior to casting to an int. For types that have unlimited lengths (e.g. xs:integer, xs:decimal, packed decimals) a new tunable is added to restrict binary number lengths to reasonable values. This tunable has an xs:int base so if the checks pass the cast from long to int should never overflow. Also replaces the various packed DelimiterUnparsers with the packed MinimumLengthUnparsers. The DelimiterUnparsers have getBitLength return 0 which fails the checks. The MimumLengthUnparers calculate the actual length of the packed fields, which can then be checked against the tunable. DAFFODIL-3088
stevedlawrence
force-pushed
the
daffodil-3088-binary-number-int-overflow
branch
from
October 19, 2026 16:35
314072d to
40096aa
Compare
jadams-tresys
approved these changes
Oct 20, 2026
jadams-tresys
left a comment
Contributor
There was a problem hiding this comment.
+1
Just a couple places where we are converting back to .toInt that I want to make sure aren't a problem
|
|
||
| try { | ||
| val int = toPrimType(context, dis.getByteArray(nBits, start)) | ||
| val int = toPrimType(context, dis.getByteArray(nBits.toInt, start)) |
Contributor
There was a problem hiding this comment.
Don't know if it matters or not, but there is still potential for an overflow/truncation here. For dis.getUnsignedLong it looks like there is a check to ensure that nBits is between 1 and 64, but there is no check for getByteArray
|
|
||
| if (nBits > 64) { | ||
| dos.putBigInt(asBigInt(value), nBits, primNumeric.isSigned, finfo) | ||
| dos.putBigInt(asBigInt(value), nBits.toInt, primNumeric.isSigned, finfo) |
Contributor
There was a problem hiding this comment.
Couldn't this overflow as well or does checkMaxWidth ensure that nBits would be able to be stored in an int?
| if (binaryBooleanTrueRep.isDefined) binaryBooleanTrueRep.getULong | ||
| else ~binaryBooleanFalseRep | ||
| putNumber(dos, trueRep, nBits, state) | ||
| putNumber(dos, trueRep, nBits.toInt, state) |
Contributor
There was a problem hiding this comment.
Do these putNumber calls need to worry about integer overflow/truncation?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
getBitLength currently returns an Int, with implementations often using Long.toInt. This can lead to silent overflow/truncation (e.g. with dfdl:length runtime expressions). To resolve this, getBitLength is modified to return a Long so that no truncation needs to happen.
Ultimately these lengths need to be converted to integers for the various IO functions, so additional functions are added to ensure we also check the lengths prior to casting to an int. For types that have unlimited lengths (e.g. xs:integer, xs:decimal, packed decimals) a new tunable is added to restrict binary number lengths to reasonable values. This tunable has an xs:int base so if the checks pass the cast from long to int should never overflow.
Also replaces the various packed DelimiterUnparsers with the packed MinimumLengthUnparsers. The DelimiterUnparsers have getBitLength return 0 which fails the checks. The MimumLengthUnparers calculate the actual length of the packed fields, which can then be checked against the tunable.
DAFFODIL-3088