Skip to content

fix(utilities): lossless guard + unsigned BigInt parser - #58

Open
klodr wants to merge 1 commit into
session-foundation:mainfrom
klodr:fix/cppinteger-lossless-and-unsigned
Open

fix(utilities): lossless guard + unsigned BigInt parser#58
klodr wants to merge 1 commit into
session-foundation:mainfrom
klodr:fix/cppinteger-lossless-and-unsigned

Conversation

@klodr

@klodr klodr commented May 18, 2026

Copy link
Copy Markdown
Contributor

Adds a lossless guard on toCppIntegerB (rejects silent truncation past int64 range) and a new toCppUnsignedIntegerB / maybeNonemptyUintB pair that uses BigInt::ToWords to detect the sign bit (so a JS -1n doesn't silently wrap to 0xFFFFFFFFFFFFFFFF when assigned to a uint64 field like a bitset).

Test plan

  • toCppUnsignedIntegerB throws on negative BigInt
  • toCppIntegerB throws on values outside int64 range

`toCppIntegerB` called `BigInt::Int64Value(&lossless)` but never
checked the `lossless` flag. A BigInt outside the int64_t range
(e.g. bit 63 of a uint64 accidentally set on the JS side) silently
wrapped — the helper documented as a parser was acting as a
truncating cast. Throw on `!lossless`.

`toCppIntegerB` is also the wrong parser for fields the C++ side
stores as uint64 (bitsets, profile flags): a NEGATIVE BigInt (-1n)
is lossless as int64 and wraps to 0xFFFFFFFFFFFFFFFF when assigned
to a uint64 destination — the same high-bit injection the lossless
check was supposed to block. Add a dedicated `toCppUnsignedIntegerB`
(and matching optional wrapper `maybeNonemptyUintB`) that rejects
negative BigInts via `BigInt::ToWords()` sign-bit inspection BEFORE
the `Uint64Value` conversion.

Callers that need to store into uint64 should migrate to the
unsigned variant in a follow-up commit; this change only adds the
helpers + tightens the existing int64 helper. No behaviour change
on the existing call sites unless they pass a BigInt outside int64
range, in which case they were already storing the wrong value.

Signed-off-by: Claude Perrin <klodr@users.noreply.github.com>
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