Fix bigIntToBytes: non-growable list and broken comparisons#28
Merged
Harry-Chen merged 2 commits intonfcim:masterfrom Mar 13, 2026
Merged
Fix bigIntToBytes: non-growable list and broken comparisons#28Harry-Chen merged 2 commits intonfcim:masterfrom
Harry-Chen merged 2 commits intonfcim:masterfrom
Conversation
Contributor
|
@klaasdannen Thank you very much for all the fixes! Please rebase this PR to master and run |
There was a problem hiding this comment.
Pull request overview
This PR fixes ByteUtils.bigIntToBytes() so it no longer crashes at runtime when appending bytes, and corrects BigInt overflow checking and endianness reversal behavior.
Changes:
- Replace invalid/non-growable list initialization with a growable
List<int>and convert viaUint8List.fromList. - Fix overflow check by using
BigInt.zeroand improve reversal by materializing.reversedinto aList. - Add a concrete type annotation to the
endiannessparameter.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+78
to
+79
| var list = <int>[]; | ||
| BigInt v = value!; |
Comment on lines
80
to
+82
| for (int i = 0; i < length; i++) { | ||
| list.add((v! % (BigInt.from(256))).toInt()); | ||
| v ~/= (BigInt.from(256)); | ||
| list.add((v % BigInt.from(256)).toInt()); | ||
| v ~/= BigInt.from(256); |
Comment on lines
+84
to
86
| if (v != BigInt.zero) { | ||
| throw ArgumentError("Value $value is overflow from range of $length bytes"); | ||
| } |
Comment on lines
73
to
+90
| static Uint8List bigIntToBytes( | ||
| BigInt? value, | ||
| int length, { | ||
| endianness = Endianness.Big, | ||
| Endianness endianness = Endianness.Big, | ||
| }) { | ||
| Uint8List? list = List<int?>.filled(0, null, growable: false) as Uint8List; | ||
| BigInt? v = value; | ||
| var list = <int>[]; | ||
| BigInt v = value!; | ||
| for (int i = 0; i < length; i++) { | ||
| list.add((v! % (BigInt.from(256))).toInt()); | ||
| v ~/= (BigInt.from(256)); | ||
| list.add((v % BigInt.from(256)).toInt()); | ||
| v ~/= BigInt.from(256); | ||
| } | ||
|
|
||
| /// unrelated_type_equality_check checked! | ||
| BigInt zero = 0 as BigInt; | ||
| if (v != zero) { | ||
| throw "Value $value is overflow from range of $length bytes"; | ||
| if (v != BigInt.zero) { | ||
| throw ArgumentError("Value $value is overflow from range of $length bytes"); | ||
| } | ||
| if (endianness == Endianness.Big) { | ||
| list = list.reversed as Uint8List?; | ||
| list = list.reversed.toList(); | ||
| } | ||
| return Uint8List.fromList(list!); | ||
| return Uint8List.fromList(list); |
The method created a non-growable List<int?>.filled(0, null, growable: false) then cast it to Uint8List and called .add() on it, which crashes at runtime. Also fixed: invalid cast of int 0 to BigInt for overflow check, missing type annotation on endianness parameter, and .reversed producing an Iterable being cast directly to Uint8List instead of converting to List first.
11dd1ff to
90cc241
Compare
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.
Summary
bigIntToBytes()created a non-growableList<int?>.filled(0, null, growable: false)then cast it toUint8Listand called.add(), which crashes at runtime with aRangeError0 as BigIntis an invalid cast, replaced withBigInt.zero.reversedreturns anIterable, notUint8List-- now uses.reversed.toList()endiannessparameterTest plan
test/ndef_test.dartcontinue to passBigIntround-trip encoding/decoding works for values that exercise this path (e.g., Signature records)