Skip to content

Commit 70f106e

Browse files
committed
Resolve issue with CBOR encoding for large negative integers.
1 parent 3e8cda0 commit 70f106e

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

lib/cbor/types/int.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,14 @@ class CborIntValue implements CborNumeric {
1919
@override
2020
List<int> encode() {
2121
final bytes = CborBytesTracker();
22-
bytes.pushInt(value.isNegative ? MajorTags.negInt : MajorTags.posInt,
23-
value.isNegative ? ~value : value);
22+
if (value.bitLength > 31 && value.isNegative) {
23+
final value = (~BigInt.parse(this.value.toString())).toInt();
24+
bytes.pushInt(MajorTags.negInt, value);
25+
} else {
26+
// print("is here lower!");
27+
bytes.pushInt(value.isNegative ? MajorTags.negInt : MajorTags.posInt,
28+
value.isNegative ? ~value : value);
29+
}
2430
return bytes.toBytes();
2531
}
2632

test/cbor_test.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ void _decodeDateTime() {
106106
}
107107

108108
void main() {
109-
// decode
110109
test("cbor decode", () {
111110
_decodeInt();
112111
_decodeFloat();

0 commit comments

Comments
 (0)