Skip to content

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

Closed
neeboo wants to merge 4 commits intodfinity:mainfrom
AstroxNetwork:main
Closed

fix: Cbor.encode(BigInt(n)) possible failure#624
neeboo wants to merge 4 commits intodfinity:mainfrom
AstroxNetwork:main

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
@ghost
Copy link

ghost commented Sep 4, 2022

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

@ghost
Copy link

ghost commented Sep 4, 2022

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

@ghost ghost added the cla:pending label Sep 4, 2022
@neeboo neeboo changed the title Fix Cbor.encode(BigInt(n)) possible failure fix: Cbor.encode(BigInt(n)) possible failure Sep 4, 2022
@ghost ghost added cla:agreed and removed cla:pending labels 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
* @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
Contributor

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?

@ilbertt ilbertt mentioned this pull request May 29, 2025
4 tasks
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