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: IDL edge case involving Math.log2 function on BigInt bit count #598

Closed
wants to merge 2 commits into from

Conversation

rvanasa
Copy link
Contributor

@rvanasa rvanasa commented Jul 25, 2022

Description

Fixes a bug where Math.log2 is sometimes called on a BigInt value in @dfinity/candid (possibly a regression from #575), causing an unexpected error in Motoko Playground.

This PR adds a custom ilog2 implementation (based on this StackOverflow post) which accounts for BigInt values while providing clearer error messages when receiving invalid input.

How Has This Been Tested?

  • Full repository test suite
  • Additional test cases for ilog2 function

Checklist:

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

@rvanasa rvanasa requested a review from a team as a code owner July 25, 2022 16:17
@@ -647,7 +648,7 @@ export class FixedIntClass extends PrimitiveType<bigint | number> {
}

public encodeType() {
const offset = Math.log2(this._bits) - 3;
const offset = ilog2(this._bits) - 3;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this the right place for the fix? this._bits can only be 8,16,32,64. I would expect the problem comes from taking log2 of the actual number, not _bits?

@rvanasa rvanasa closed this Jul 25, 2022
@rvanasa
Copy link
Contributor Author

rvanasa commented Jul 25, 2022

Closing in favor of a different solution.

@rvanasa rvanasa deleted the ryan/ilog2-bigint branch July 25, 2022 17:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants