From 90cc2418ee2a9d9b91e8a16b40b82786b1dd16b3 Mon Sep 17 00:00:00 2001 From: Klaas Dannen Date: Fri, 13 Mar 2026 16:05:34 +0100 Subject: [PATCH 1/2] Fix bigIntToBytes: non-growable list and broken comparisons The method created a non-growable List.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. --- lib/utilities.dart | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/lib/utilities.dart b/lib/utilities.dart index cfd68e9..4dc7ed9 100644 --- a/lib/utilities.dart +++ b/lib/utilities.dart @@ -75,24 +75,21 @@ class ByteUtils { static Uint8List bigIntToBytes( BigInt? value, int length, { - endianness = Endianness.Big, + Endianness endianness = Endianness.Big, }) { - Uint8List? list = List.filled(0, null, growable: false) as Uint8List; - BigInt? v = value; + var list = []; + 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) { + if (v != BigInt.zero) { throw ArgumentError("Value $value overflows $length bytes"); } if (endianness == Endianness.Big) { - list = list.reversed as Uint8List?; + list = list.reversed.toList(); } - return Uint8List.fromList(list!); + return Uint8List.fromList(list); } /// Converts a single byte [value] to a 2-character hex string. @@ -119,7 +116,9 @@ class ByteUtils { }, ); if (hex.length % 2 != 0) { - throw FormatException("Hex string length must be even, got ${hex.length}"); + throw FormatException( + "Hex string length must be even, got ${hex.length}", + ); } var result = []; for (int i = 0; i < hex.length; i += 2) { @@ -188,8 +187,7 @@ extension IntConvert on int { String toHexStringAsBytes( int length, { Endianness endianness = Endianness.Big, - }) => - ByteUtils.intToHexString(this, length, endianness: endianness); + }) => ByteUtils.intToHexString(this, length, endianness: endianness); /// Converts an integer to bytes with specified [length] and [endianness]. Uint8List toBytes(int length, {Endianness endianness = Endianness.Big}) => @@ -318,7 +316,9 @@ class ByteStream { /// Throws an error if not enough bytes are available. void checkBytesAvailable(int number) { if (number > unreadLength) { - throw RangeError("Not enough bytes in stream: need $number, have $unreadLength"); + throw RangeError( + "Not enough bytes in stream: need $number, have $unreadLength", + ); } } @@ -327,7 +327,9 @@ class ByteStream { /// Throws an error if there are unread bytes. void checkEmpty() { if (unreadLength != 0) { - throw FormatException("Stream has $unreadLength unexpected bytes after decode"); + throw FormatException( + "Stream has $unreadLength unexpected bytes after decode", + ); } } } From 85550e969de277c46783cc9314274899351f2951 Mon Sep 17 00:00:00 2001 From: Klaas Dannen Date: Fri, 13 Mar 2026 16:30:44 +0100 Subject: [PATCH 2/2] Run dart format to fix CI formatting check --- lib/utilities.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/utilities.dart b/lib/utilities.dart index 4dc7ed9..ff2fd07 100644 --- a/lib/utilities.dart +++ b/lib/utilities.dart @@ -187,7 +187,8 @@ extension IntConvert on int { String toHexStringAsBytes( int length, { Endianness endianness = Endianness.Big, - }) => ByteUtils.intToHexString(this, length, endianness: endianness); + }) => + ByteUtils.intToHexString(this, length, endianness: endianness); /// Converts an integer to bytes with specified [length] and [endianness]. Uint8List toBytes(int length, {Endianness endianness = Endianness.Big}) =>