Skip to content

numpy::from_data calls vector::front and crashes (or has UB) when called with a scalar object #526

Description

@mborgerding

In ndarray.cpp, the function from_data_impl calls &shape.front() and &strides.front() for possibly empty vectors. This is, at best, undefined behavior. At worst, it triggers a crash (observed on Redhat/rocky 8&9, where I'm assuming boost::python packages were built with -D_GLIBCXX_DEBUG).

RECOMMEND: change &vec.front() to vec.data() to avoid UB and potential crashes.

Activity

  1. mborgerding commented on Aug 12, 2026

    @mborgerding
    ContributorAuthor

    Here's an MRE using a cloned version of the function in question from ndarray.cpp (mildly edited for simplicity).
    ndarray_mre.cpp

    A compilation line for Ubuntu 24.04 at the bottom of the source file demonstrates the crash by compiling with _GLIBCXX_DEBUG and without NDEBUG:

    shape=(2,2) : array([[0.1, 0.2],
           [0.3, 0.4]], dtype=float32)
    shape=(4,) : array([0.1, 0.2, 0.3, 0.4], dtype=float32)
    /usr/include/c++/13/debug/vector:468:
    In function:
        std::debug::vector<_Tp, _Allocator>::const_reference std::
        debug::vector<_Tp, _Allocator>::front() const [with _Tp = long int; 
        _Allocator = std::allocator<long int>; const_reference = const long 
        int&]
    
    Error: attempt to access an element in an empty container.
    
    Objects involved in the operation:
        sequence "this" @ 0x7ffdcf4ae750 {
          type = std::debug::vector<long, std::allocator<long> >;
        }
    Aborted (core dumped)
    

    To see the proposed fix succeed, compile and run with -DTHE_FIX.

  2. added a commit that references this issue on Aug 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions