Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Cbor.encode(BigInt(n)) possible failure #624

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions packages/agent/src/cbor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,85 @@ test('empty canister ID', () => {
expect(Principal.fromUint8Array(outputA as any).toText()).toBe('aaaaa-aa');
});

describe('cbor encode/decode bigint', () => {
test('encode decode 0 to 1_000_000', () => {
for (let i = 0; i < 1_000_000; i += 1) {
let buffer;

try {
buffer = encode(BigInt(i));
} catch (error) {
console.log(`error encoding on ${i.toString()}`);
expect((error as Error).message).toBeTruthy();
}

try {
const decoded = decode<bigint>(buffer);
expect(decoded).toBe(BigInt(i));
} catch (error) {
console.log(`error decoding on ${i.toString()}`);
expect((error as Error).message).toBeTruthy();
}
// // if we log the time
// if (i % 100000 === 0) {
// console.log(`${new Date(Date.now()).toLocaleTimeString()} with ${i.toString()}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason these comments are left in?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can delete those

// }
}
});
test('encode decode 1_000_000n to 1_000_000_000_000n', () => {
for (let i = 1_000_000; i < 1_000_000_000_000; i += 1_000_000) {
let buffer;

const random_num = Math.floor(Math.random() * Math.pow(10, i.toString().length - 1));

const actual = BigInt(i + random_num);
try {
buffer = encode(actual);
} catch (error) {
console.log(`error encoding on ${i.toString()}`);
expect((error as Error).message).toBeTruthy();
}

try {
const decoded = decode<bigint>(buffer);
expect(decoded).toBe(actual);
} catch (error) {
console.log(`error decoding on ${i.toString()}`);
expect((error as Error).message).toBeTruthy();
}
// // if we log the time
// if (i % 1_000_000_000_00 === 0) {
// console.log(`${new Date(Date.now()).toLocaleTimeString()} with ${actual.toString()}`);
// }
}
});
test('encode decode 1_000_000_000_000n to 1_000_000_000_000_000_000n', () => {
for (let i = BigInt(1_000_000_000_000); i < BigInt(1_000_000_000_000_000_000); i += BigInt(1_000_000_000_000)) {
let buffer;
const random_num = BigInt(Math.floor(Math.random() * Math.pow(10, i.toString().length - 1)));
const actual = i + random_num;
try {
buffer = encode(actual);
} catch (error) {
console.log(`error encoding on ${i.toString()}`);
expect((error as Error).message).toBeTruthy();
}

try {
const decoded = decode<bigint>(buffer);
expect(decoded).toBe(actual);
} catch (error) {
console.log(`error decoding on ${i.toString()}`);
expect((error as Error).message).toBeTruthy();
}
// // if we log the time
// if (i % 1_000_000_000_000_000_00n === 0n) {
// console.log(`${new Date(Date.now()).toLocaleTimeString()} with ${actual.toString()}`);
// }
}
});
});

function buf2hex(buffer: Uint8Array) {
// Construct an array such that each number is translated to the
// hexadecimal equivalent, ensure it is a string and padded then
Expand Down
6 changes: 6 additions & 0 deletions packages/agent/src/utils/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ const hexRe = new RegExp(/^([0-9A-F]{2})*$/i);
* @param hex The hexadecimal string to use.
*/
export function fromHex(hex: string): ArrayBuffer {
//FIXME: always padding hex string length to be even
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the rationale for this? Does decoding break for odd-length hex strings?

// then run the reg test
if (hex.length % 2 !== 0) {
hex = '0' + hex;
}

if (!hexRe.test(hex)) {
throw new Error('Invalid hexadecimal string.');
}
Expand Down