Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -509,15 +509,3 @@ Fix direction: in `applyArg`, when `VALUE_FLAGS[arg]` exists but `next` is
`undefined`, raise a clear "missing value for <flag>" usage error rather than
pushing the flag to `positional`; keep consuming/returning true when a value is
present.

### 5. Seeded PRNG `rand()` can return exactly 1

`test/lib/checkout-pricing-consistency.test.ts` — `makeRng`'s `rand` returns
`seed / 0x7fffffff`, which is `1` when `seed === 0x7fffffff`. That lets `pick`
index one past the array and `randInt` return `hi + 1`. Astronomically unlikely
for the fixed seeds in use (and identical to `main`), but incorrect.

Fix direction: normalize with `seed / 0x80000000` (or clamp below 1). Note this
changes the seeded sequence, so re-baseline any hardcoded expectations and
confirm the property test still passes; best landed on its own so the corpus
shift is reviewed deliberately.
5 changes: 4 additions & 1 deletion test/lib/checkout-pricing-consistency.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ const makeRng = (initialSeed: number): Rng => {
let seed = initialSeed;
const rand = () => {
seed = (seed * 1103515245 + 12345) & 0x7fffffff;
return seed / 0x7fffffff;
// Divide by 2^31 (not 2^31-1) so the result is always in [0, 1): at
// seed === 0x7fffffff the old divisor returned exactly 1, letting `pick`
// index past the array and `randInt` return hi + 1.
return seed / 0x80000000;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
};
const pick = <T>(xs: readonly T[]): T => xs[Math.floor(rand() * xs.length)]!;
const randInt = (lo: number, hi: number) =>
Expand Down
3 changes: 2 additions & 1 deletion test/shared/payment-signature.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ const excludedKeys: [string, Record<string, string>][] = [
/** Tiny deterministic PRNG so the fuzz loop is repeatable. */
const lcg = (seed: number) => () => {
seed = (seed * 1103515245 + 12345) & 0x7fffffff;
return seed / 0x7fffffff;
// Divide by 2^31 (not 2^31-1) so the result stays in [0, 1).
return seed / 0x80000000;
};

describeWithEnv("payment price signature", { encryptionKey: true }, () => {
Expand Down