Skip to content

Commit

Permalink
Resolve issue with CBOR encoding for large negative integers.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtnetwork committed Oct 1, 2024
1 parent 3e8cda0 commit 70f106e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
10 changes: 8 additions & 2 deletions lib/cbor/types/int.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ class CborIntValue implements CborNumeric {
@override
List<int> encode() {
final bytes = CborBytesTracker();
bytes.pushInt(value.isNegative ? MajorTags.negInt : MajorTags.posInt,
value.isNegative ? ~value : value);
if (value.bitLength > 31 && value.isNegative) {
final value = (~BigInt.parse(this.value.toString())).toInt();
bytes.pushInt(MajorTags.negInt, value);
} else {
// print("is here lower!");
bytes.pushInt(value.isNegative ? MajorTags.negInt : MajorTags.posInt,
value.isNegative ? ~value : value);
}
return bytes.toBytes();
}

Expand Down
1 change: 0 additions & 1 deletion test/cbor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ void _decodeDateTime() {
}

void main() {
// decode
test("cbor decode", () {
_decodeInt();
_decodeFloat();
Expand Down

0 comments on commit 70f106e

Please sign in to comment.