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

Conversation

neeboo
Copy link

@neeboo neeboo commented Sep 4, 2022

Fix Cbor.encode(BigInt(n)) possible failure

Description

When use Cbor.encode, if passed value is BigInt, first will run the BigInt encoder and transform BigInt value to be a hex string. However this BigInt(n).toString(16) would probrably generate a hex string length not to be even, which won't pass the RegTest in the fromHex function.

const hexRe = new RegExp(/^([0-9A-F]{2})*$/i);

Fixes # (issue)

Added a zero-padding while passing hex string length is not even.

export function fromHex(hex: string): ArrayBuffer {
  //FIXME: always padding hex string length to be even
  // then run the reg test
  if (hex.length % 2 !== 0) {
    hex = '0' + hex;
  }

  if (!hexRe.test(hex)) {
    throw new Error('Invalid hexadecimal string.');
  }
  const buffer = [...hex]
    .reduce((acc, curr, i) => {
      // tslint:disable-next-line:no-bitwise
      acc[(i / 2) | 0] = (acc[(i / 2) | 0] || '') + curr;
      return acc;
    }, [] as string[])
    .map(x => Number.parseInt(x, 16));

  return new Uint8Array(buffer).buffer;
}

How Has This Been Tested?

test cases are in cbor.tests.ts

Checklist:

  • My changes follow the guidelines in CONTRIBUTING.md.
  • The title of this PR complies with Conventional Commits.
  • I have edited the CHANGELOG accordingly.
  • I have made corresponding changes to the documentation.

@neeboo neeboo requested a review from a team as a code owner September 4, 2022 06:19
@dfinity-droid-prod
Copy link

Dear @neeboo,

In order to potentially merge your code in this open-source repository and therefore proceed with your contribution, we need to have your approval on DFINITY's CLA1.

If you decide to agree with it, please visit this issue and read the instructions there.

— The DFINITY Foundation

Footnotes

  1. Contributor License Agreement

@dfinity-droid-prod
Copy link

Dear @neeboo,

In order to potentially merge your code in this open-source repository and therefore proceed with your contribution, we need to have your approval on DFINITY's CLA1.

If you decide to agree with it, please visit this issue and read the instructions there.

— The DFINITY Foundation

Footnotes

  1. Contributor License Agreement

@neeboo neeboo changed the title Fix Cbor.encode(BigInt(n)) possible failure fix: Cbor.encode(BigInt(n)) possible failure Sep 4, 2022
Co-authored-by: Kyle Peacock <kylpeacock@gmail.com>
@krpeacock krpeacock self-requested a review May 22, 2023 16:23
@krpeacock krpeacock changed the title fix: Cbor.encode(BigInt(n)) possible failure fix: Cbor.encode(BigInt(n)) possible failure May 22, 2023
@@ -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?

}
// // 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

@ilbertt
Copy link

ilbertt commented Aug 18, 2023

When serializing objects that contain BigInts, I'm getting the error:

Invalid hexadecimal string.

Has some progress been made on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants