BUG: fix unchecked `new` uses in `numpy.h` #277
All Vouch review confidence checks passed
Residual risk 0.02 = Change Risk (0.27) × (1 − Review Confidence (0.93)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.27 · 22 lines diff across 1 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.95 · 5 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.188
Ai Authored
ai_authored
0.050
Real PR Conversation Timeline
7 items
Do we need to set the corresponding error code with set_error ?
Do we need to set the corresponding error code with set_error ?
not sure, @steppi do you know?
> Do we need to set the corresponding error code with set_error ?
> not sure, @steppi do you know?
Yeah. I think that would be pattern that's more consistent with the rest of the codebase, although it shouldn't really make much of a difference in practice.
updated to use set_error. Claude is suggesting that there are still problems, though — it reckons that this diff fixes a null pointer problem:
diff --git a/include/xsf/numpy.h b/include/xsf/numpy.h
index dddec5f..973dd7f 100644
--- a/include/xsf/numpy.h
+++ b/include/xsf/numpy.h
@@ -940,4 +940,4 @@ namespace numpy {
m_func(new (std::nothrow) PyUFuncGenericFunction[m_ntypes]),
- m_data(new (std::nothrow) data_handle_type[m_ntypes]),
- m_data_deleters(new (std::nothrow) data_deleter_type[m_ntypes]),
+ m_data(new (std::nothrow) data_handle_type[m_ntypes]()),
+ m_data_deleters(new (std::nothrow) data_deleter_type[m_ntypes]()),
m_types(new (std::nothrow) char[m_ntypes * m_nin_and_nout]) {
@@ -949,2 +949,3 @@ namespace numpy {
ufunc_wraps func[sizeof...(Funcs) + 1] = {func0, funcs...};
+ bool data_alloc_failed = false;
for (auto it = std::begin(func); it != std::end(func); ++it) {
@@ -956,2 +957,5 @@ namespace numpy {
}
+ if (it->data == nullptr) {
+ data_alloc_failed = true;
+ }
@@ -963,2 +967,6 @@ namespace numpy {
}
+
+ if (data_alloc_failed) {
+ xsf::set_error("ufunc_overloads", SF_ERROR_MEMORY, "memory allocation error");
+ }
}
@@ -971,6 +979,8 @@ namespace numpy {
~ufunc_overloads() {
- if (m_data) {
+ if (m_data && m_data_deleters) {
for (int i = 0; i < m_ntypes; ++i) {
data_deleter_type data_deleter = m_data_deleters[i];
- data_deleter(m_data[i]);
+ if (data_deleter != nullptr) {
+ data_deleter(m_data[i]);
+ }
}
but still:
Two problems remain:
1. Probably a crash.ufunc()andgufunc()only stop earlyif (PyErr_Occurred()). After that they callset_nameandset_map_dims, which dereference everym_data[i].set_erroronly sets a Python exception if SciPy's error settings make it raise. WithoutSP_SPECFUN_ERRORit does nothing, and when set to warn or ignore it sets no exception. In those cases a null data entry gets through andset_namecrashes on it. That means my first change (replacingPyErr_NoMemory()) left this worse than before. Two ways to fix it:
- also callPyErr_NoMemory()next toset_error, so the existing checks still catch it; or
- giveufunc_overloadsanok()flag and check it inufunc()andgufunc().
2. Reported twice. A failedufunc_wrapsallocation is reported once inufunc_wrapsand again inufunc_overloads. You could drop the call inufunc_wraps, but it's the only report for code that usesufunc_wrapsoutsideufunc_overloads.
> I'd go with calling PyErr_NoMemory() as well, since it's the smallest change.Can we use sf_error here instead? Thats what we use in other parts of the code base. Sry, I had mistyped it before.
sf_error lives in SciPy so can't be used directly here. xsf::set_error can be configured to use custom defined error handling functions and is configured to use sf_error when xsf is used i SciPy.
dschmitz89
lucascolley
<!--
Thanks for contributing a pull request! Please ensure that
your PR is ready before submitting:
pixi run tests.pixi run format-dry-error. To fix formatting,use
pixi run format.Note that we are a team of volunteers; we appreciate your
patience during the review process.
Again, thanks for contributing!
-->
Reference issue
Fixes gh-38 @WarrenWeckesser
AI Generation Disclosure
Changes drafted by claude code, following the pattern of https://github.com/scipy/scipy/commit/1cbdb24f432c55d766f6d3c073738c359234f2de.