Skip to content

fix: restrict wasm signed division overflow guard to MIN_VALUE - #142

Open
zhangga wants to merge 1 commit into
dcodeIO:mainfrom
zhangga:fix/wasm-signed-division-overflow
Open

zhangga wants to merge 1 commit into
dcodeIO:mainfrom
zhangga:fix/wasm-signed-division-overflow

Conversation

@zhangga

@zhangga zhangga commented Oct 29, 2026

Copy link
Copy Markdown

Resubmits the fix and regression tests from #138, which was automatically closed as stale without being merged. The original commit author is preserved.

The WASM signed division overflow guard currently checks only the high word of the dividend. Negative values near MIN_VALUE are therefore returned unchanged when divided by -1:

Long.fromString("-9223372036854775807").div(Long.NEG_ONE).toString();
// "-9223372036854775807", expected "9223372036854775807"

Require this.low === 0 as well, so only the exact MIN_VALUE / -1 overflow wraps. Other values proceed to WASM division, consistent with the JS path.

The regression covers two affected negative values and verifies that the exact MIN_VALUE / -1 overflow still wraps to MIN_VALUE.

Validation (Node 22.13.0):

  • Reproduced the incorrect result on unmodified main.
  • npm run build
  • npm test (unit tests, Closure library tests and all four TypeScript configurations)
  • npm run lint
  • Full unit/Closure suite also run against the generated UMD implementation.

The wasm-backed signed division short-circuits and returns the dividend
unchanged whenever this.high === 0x80000000 and the divisor is -1. That
guard is meant to catch only the true two's-complement overflow of
MIN_VALUE / -1, but it omits the low-word check, so any negative dividend
sharing that high word (for example -9223372036854775807) is wrongly
returned unchanged instead of being negated.

Add this.low === 0 so only the exact MIN_VALUE / -1 overflow is special
cased; every other value falls through to wasm.div_s. This matches the
non-wasm path, which guards on this.eq(MIN_VALUE).
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.

2 participants