Skip to content

Fix security and correctness issues from #25 Array API - #40

Merged
mrecachinas merged 2 commits into
masterfrom
pr-25
Feb 16, 2026
Merged

Fix security and correctness issues from #25 Array API#40
mrecachinas merged 2 commits into
masterfrom
pr-25

Conversation

@mrecachinas

Copy link
Copy Markdown
Owner

Follow-up to #25

A post-merge review of the Array API introduced in #25 uncovered four bugs — two of which silently corrupt return values on 64-bit systems and one that can crash the interpreter.

Fixes

  1. Py_BuildValue format truncation"i" (32-bit C int) was used for int64_t/uint64_t return values, silently truncating results on arrays with >2³¹ elements or large distances. Changed to "L" / "K" as appropriate.

  2. Signed integer overflow in best_within_distbest_dist = max_dist + 1 is undefined behavior when max_dist is large. Replaced with a -1 sentinel pattern that avoids the overflow entirely.

  3. Uncaught std::bad_alloc in all_within_distnew uint64_t[] on a large allocation would throw an uncaught C++ exception, calling std::terminate() and killing the process. Replaced with malloc + NULL check that returns PyErr_NoMemory() gracefully.

  4. NEON max_dist=0 edge caseif (max_dist > 0) should be if (max_dist >= 0), causing check_bytes_within_dist(..., 0) to take the wrong code path on ARM and return incorrect results.

Also includes

  • Python 3.14 added to CI build matrix
  • #include <cstdlib> for malloc/free

@github-actions

github-actions Bot commented Feb 16, 2026

Copy link
Copy Markdown

📊 Benchmark Comparison Results

✅ All benchmarks within noise

42 benchmarks compared, no significant changes detected.

Legend: ✅ Faster (>5%) · ⚠️ Slower (5-30%) · ❌ Regression (>30%) · 🆕 New · 🗑️ Removed

mrecachinas and others added 2 commits February 16, 2026 12:55
- Fix Py_BuildValue format specifiers: use 'L' for int64_t and 'K' for
  uint64_t instead of 'i' which silently truncates 64-bit values
- Fix signed integer overflow UB in best_within_dist when max_dist is
  large: use -1 sentinel instead of max_dist+1
- Replace new[]/delete[] with malloc/free + NULL check to prevent
  uncaught std::bad_alloc from crashing the process
- Fix NEON bug: max_dist > 0 should be max_dist >= 0 so that max_dist=0
  correctly delegates to the native path

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@mrecachinas
mrecachinas merged commit 9e00e55 into master Feb 16, 2026
13 checks passed
@mrecachinas
mrecachinas deleted the pr-25 branch February 16, 2026 17:59
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