Skip to content

fix: avoid integer overflow in stats/base/dists/hypergeometric - #14972

Draft
Planeshifter wants to merge 1 commit into
developfrom
claude/ecstatic-heisenberg-jznoh2
Draft

fix: avoid integer overflow in stats/base/dists/hypergeometric#14972
Planeshifter wants to merge 1 commit into
developfrom
claude/ecstatic-heisenberg-jznoh2

Conversation

@Planeshifter

Copy link
Copy Markdown
Member

Description

What is the purpose of this pull request?

This pull request:

  • propagates fixes merged to develop between 2026-09-03 and 2026-09-04 to sibling packages.

Propagates the int32_t overflow fix from 493166d (PR #14933) into the three remaining hypergeometric packages that share the defect: pmf, logpmf, and quantile each compute n+K-N in 32-bit integer arithmetic prior to conversion to double, overflowing for in-domain inputs where n+K > 2^31-1 (e.g. N = K = n = 2e9). Applies the same idiom as the fixed cdf: dn/dK/dN double locals feeding stdlib_base_max( 0.0, dn+dK-dN ), with quantile additionally converting lower/upper/x and its search loop to double (x += 1.0) and collapsing its signature to the single-line form used across every other quantile/src/main.c in stats/base/dists.

Source: 493166d ("fix: perform support checks before integer narrowing")

  • @stdlib/stats/base/dists/hypergeometric/pmf
  • @stdlib/stats/base/dists/hypergeometric/logpmf
  • @stdlib/stats/base/dists/hypergeometric/quantile

Related Issues

Does this pull request have any related issues?

This pull request has no related issues.

Questions

Any questions for reviewers of this pull request?

No.

Other

Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.

Validation performed before applying the propagation:

  • All commits merged to develop in the last 24 hours were screened for generalizable fixes; five candidate patterns were extracted, and pattern searches were run across the respective sibling namespaces.
  • Two independent validation passes confirmed the defect at each of the three target sites (signed int32_t overflow of n+K-N is reachable for guard-passing inputs) and confirmed the double-arithmetic rewrite is bit-exact for all non-overflowing inputs.
  • An adaptation pass produced the per-site patches and a style pass confirmed conformance with the fixed cdf implementation's idiom (declaration ordering, dn/dK/dN locals, single-line signature convention).
  • Deliberately excluded: 17 candidate JSDoc sites for a second pattern ({PositiveNumber}{NonNegativeNumber} lambda, source 0fdb09d) — all rejected because zero-acceptance at those sites is incidental (no degenerate-case branch) or diverges between JS and native paths; one site (wald/pdf/lib/native.js) is already fixed by open PR fix: apply follow-up fixes from automated review of recent commits #14969.
  • All three modified files compile cleanly with gcc -std=c99 -Wall -Wextra -fsyntax-only.

Checklist

Please ensure the following tasks are completed before submitting this pull request.

AI Assistance

When authoring the changes proposed in this PR, did you use any kind of AI assistance?

  • Yes
  • No

If you answered "yes" above, how did you use AI assistance?

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

Disclosure

If you answered "yes" to using AI assistance, please provide a short disclosure indicating how you used AI assistance. This helps reviewers determine how much scrutiny to apply when reviewing your contribution. Example disclosures: "This PR was written primarily by Claude Code." or "I consulted ChatGPT to understand the codebase, but the proposed changes were fully authored manually by myself.".

This PR was written by Claude Code as part of an automated fix-propagation routine; each target site was verified by two independent validation passes plus adaptation and style-consistency passes before the patches were applied.


@stdlib-js/reviewers

🤖 Generated with Claude Code

https://claude.ai/code/session_015CyugoNH33WVr8ghD2PhZr


Generated by Claude Code

Propagates fix from 493166d ("fix: perform support checks before
integer narrowing") to sibling packages with the same int32
support-bound arithmetic.

The expression `n+K-N` in the C implementations of `pmf`, `logpmf`,
and `quantile` was evaluated in int32 arithmetic before conversion to
double, overflowing (undefined behavior) for in-domain inputs where
`n+K` exceeds the int32 range. Mirror the fixed `cdf` implementation
by computing support bounds with double intermediates; in `quantile`,
additionally convert the loop variable and bounds to doubles.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015CyugoNH33WVr8ghD2PhZr
@stdlib-bot stdlib-bot added the Statistics Issue or pull request related to statistical functionality. label Sep 4, 2026
@stdlib-bot

Copy link
Copy Markdown
Contributor

Coverage Report

Package Statements Branches Functions Lines
stats/base/dists/hypergeometric/logpmf $\\color{green}381/381$
$\\color{green}+100.00\\%$
$\\color{green}40/40$
$\\color{green}+100.00\\%$
$\\color{green}4/4$
$\\color{green}+100.00\\%$
$\\color{green}381/381$
$\\color{green}+100.00\\%$
stats/base/dists/hypergeometric/pmf $\\color{green}377/377$
$\\color{green}+100.00\\%$
$\\color{green}40/40$
$\\color{green}+100.00\\%$
$\\color{green}4/4$
$\\color{green}+100.00\\%$
$\\color{green}377/377$
$\\color{green}+100.00\\%$
stats/base/dists/hypergeometric/quantile $\\color{green}364/364$
$\\color{green}+100.00\\%$
$\\color{green}53/53$
$\\color{green}+100.00\\%$
$\\color{green}4/4$
$\\color{green}+100.00\\%$
$\\color{green}364/364$
$\\color{green}+100.00\\%$

The above coverage report was generated for the changes in this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Statistics Issue or pull request related to statistical functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants