Skip to content

fix: validate values passed to isPowerOfTwo() - #1814

Open
mmporong wants to merge 1 commit into
melonjs:masterfrom
mmporong:fix/power-of-two-validation
Open

mmporong wants to merge 1 commit into
melonjs:masterfrom
mmporong:fix/power-of-two-validation

Conversation

@mmporong

Copy link
Copy Markdown

Description

isPowerOfTwo() relied on JavaScript bitwise coercion without validating its input. This made zero, negative powers, fractional values, and some non-powers beyond the 32-bit range return true.

This change requires a positive integer, computes its base-2 exponent, and verifies that reconstructing the power produces the original value. Regression tests cover zero, a negative value, a fraction, values beyond the 32-bit range, and large non-powers whose logarithms round to integers.

Type of change

  • Bug fix
  • New feature
  • Documentation update
  • Performance improvement
  • Refactoring (no functional changes)

Checklist

  • I have read the Contributing Guide
  • My code follows the existing code style (pnpm lint passes)
  • I have tested my changes locally (pnpm test passes)
  • I have added tests that cover my changes (if applicable)
  • The build succeeds (pnpm build)

The focused Chromium suite passes all 72 math tests. The complete browser suite was not run locally.

Related issues

None.

This branch has not been deployed

No deployments
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.

1 participant