← Back to Pull requests

BUG: fix unchecked `new` uses in `numpy.h` #277

Open
lucascolley opened this pull request with 22 lines diff across 1 files
View on GitHub
lucascolley lucascolley commented on Sep 14, 2026 12:20 UTC
Author

<!--
Thanks for contributing a pull request! Please ensure that
your PR is ready before submitting:

  • Run the relevant tests locally. For the full test suite, use

  • pixi run tests.
  • Check formatting with pixi run format-dry-error. To fix formatting,

  • use pixi run format.
  • Name and describe your PR as you would write a commit message.
  • 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.

    Diff: 22 lines (+17 / -5) across 1 files Reviewer: dschmitz89 Review duration: 38828m
    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
    0.27
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.95 · 5 substantive comment(s)
    0.95
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    93%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    dschmitz89 dschmitz89 commented on Sep 16, 2026 11:40 UTC
    Contributor

    Do we need to set the corresponding error code with set_error ?

    lucascolley lucascolley commented on Sep 23, 2026 21:39 UTC
    Author

    Do we need to set the corresponding error code with set_error ?

    not sure, @steppi do you know?

    steppi steppi commented on Sep 27, 2026 18:31 UTC
    Contributor

    > 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.

    lucascolley lucascolley commented on Sep 28, 2026 10:22 UTC
    Author

    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() and gufunc() only stop early if (PyErr_Occurred()). After that they call set_name and set_map_dims, which dereference every m_data[i]. set_error only sets a Python exception if SciPy's error settings make it raise. Without SP_SPECFUN_ERROR it does nothing, and when set to warn or ignore it sets no exception. In those cases a null data entry gets through and set_name crashes on it. That means my first change (replacing PyErr_NoMemory()) left this worse than before. Two ways to fix it:

    - also call PyErr_NoMemory() next to set_error, so the existing checks still catch it; or

    - give ufunc_overloads an ok() flag and check it in ufunc() and gufunc().

    2. Reported twice. A failed ufunc_wraps allocation is reported once in ufunc_wraps and again in ufunc_overloads. You could drop the call in ufunc_wraps, but it's the only report for code that uses ufunc_wraps outside ufunc_overloads.

    > I'd go with calling PyErr_NoMemory() as well, since it's the smallest change.

    dschmitz89 dschmitz89 commented on code on Sep 29, 2026 06:07 UTC
    Code Review
    include/xsf/numpy.h:1
    @@ -908,9 +909,13 @@ namespace numpy {
             template <typename Func>
             ufunc_wraps(Func func)
                 : has_return(has_return_v<Func>), nin_and_nout(arity_of_v<Func> + has_return),
    -              func(ufunc_traits<Func>::loop), data(new ufunc_data<Func>{{nullptr}, func}),
    +              func(ufunc_traits<Func>::loop), data(new (std::nothrow) ufunc_data<Func>{{nullptr}, func}),
                   data_deleter([](void *ptr) { delete static_cast<ufunc_data<Func> *>(ptr); }),
    -              types(ufunc_traits<Func>::types) {}
    +              types(ufunc_traits<Func>::types) {
    +            if (data == nullptr) {
    +                xsf::set_error("ufunc_wraps", SF_ERROR_MEMORY, "memory allocation error");

    Can we use sf_error here instead? Thats what we use in other parts of the code base. Sry, I had mistyped it before.

    steppi steppi commented on code on Sep 29, 2026 15:24 UTC
    Code Review
    include/xsf/numpy.h:1
    @@ -908,9 +909,13 @@ namespace numpy {
             template <typename Func>
             ufunc_wraps(Func func)
                 : has_return(has_return_v<Func>), nin_and_nout(arity_of_v<Func> + has_return),
    -              func(ufunc_traits<Func>::loop), data(new ufunc_data<Func>{{nullptr}, func}),
    +              func(ufunc_traits<Func>::loop), data(new (std::nothrow) ufunc_data<Func>{{nullptr}, func}),
                   data_deleter([](void *ptr) { delete static_cast<ufunc_data<Func> *>(ptr); }),
    -              types(ufunc_traits<Func>::types) {}
    +              types(ufunc_traits<Func>::types) {
    +            if (data == nullptr) {
    +                xsf::set_error("ufunc_wraps", SF_ERROR_MEMORY, "memory allocation error");

    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.

    Reviewers
    Assignees
    lucascolley lucascolley
    Labels
    risk: low confidence: 93% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 26.8% (LOW) · Higher = larger blast radius
    26.8%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 93% · Lower = rushed or shallow review
    93%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 2% · Safe to merge
    2%
    Repository