pybind / pybind/pybind11

[BUG]: -Wmaybe-uninitialized warnings in O3 optimization

Open
#6,167 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

triage
Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

Required prerequisites
What version (or hash if on master) of pybind11 are you using?

3.0.4

Problem description

I get a lot of warnings, when compiling the example.cpp file found in the docs. I also put it below. Other than the warnings, it just compiles fine.
I am on Linux (fedora 44) using the pybind11 release 3.0.4
I first asked this in a Discussion, where i got asked to file a bug report. The Discussion is here.
I tried a few variations of the following command to compile:
gcc -O3 -Wall -shared -std=c++11 -fPIC $(python3-config --includes) -I "$PYBIND11_BASE/include" example.cpp -o "example$(python3-config --extension-suffix)"
I tried using -isystem instead of -I or using c++ or g++ instead of gcc, but the same warnings were always there.
The output i get from the compiler is the following:

In file included from /usr/include/c++/16/vector:69,
                 from /home/sytoplis/pythonModuleTest/Dependencies/pybind11/pybind11-3.0.4/include/pybind11/detail/common.h:240,
                 from /home/sytoplis/pythonModuleTest/Dependencies/pybind11/pybind11-3.0.4/include/pybind11/attr.h:13,
                 from /home/sytoplis/pythonModuleTest/Dependencies/pybind11/pybind11-3.0.4/include/pybind11/detail/class.h:12,
                 from /home/sytoplis/pythonModuleTest/Dependencies/pybind11/pybind11-3.0.4/include/pybind11/pybind11.h:12,
                 from example.cpp:4:
In member function ‘void std::_Bvector_base<_Alloc>::_M_deallocate() [with _Alloc = std::allocator<bool>]’,
    inlined from ‘std::_Bvector_base<_Alloc>::~_Bvector_base() [with _Alloc = std::allocator<bool>]’ at /usr/include/c++/16/bits/stl_bvector.h:676:28,
    inlined from ‘std::vector<bool, _Alloc>::~vector() [with _Alloc = std::allocator<bool>]’ at /usr/include/c++/16/bits/stl_bvector.h:926:37,
    inlined from ‘pybind11::detail::inline_array_or_vector<long unsigned int, 1, bool>::heap_vector::~heap_vector()’ at /home/sytoplis/pythonModuleTest/Dependencies/pybind11/pybind11-3.0.4/include/pybind11/detail/argument_vector.h:58:12,
    inlined from ‘pybind11::detail::inline_array_or_vector<ArrayT, InlineSize, VectorT>::~inline_array_or_vector() [with ArrayT = long unsigned int; long unsigned int InlineSize = 1; VectorT = bool]’ at /home/sytoplis/pythonModuleTest/Dependencies/pybind11/pybind11-3.0.4/include/pybind11/detail/argument_vector.h:75:33,
    inlined from ‘pybind11::detail::small_vector<bool, kRequestedInlineSize>::small_vector(std::size_t, bool) [with long unsigned int kRequestedInlineSize = 6]’ at /home/sytoplis/pythonModuleTest/Dependencies/pybind11/pybind11-3.0.4/include/pybind11/detail/argument_vector.h:241:5,
    inlined from ‘static PyObject* pybind11::cpp_function::dispatcher(PyObject*, PyObject* const*, pybind11::size_t, PyObject*)’ at /home/sytoplis/pythonModuleTest/Dependencies/pybind11/pybind11-3.0.4/include/pybind11/pybind11.h:1203:87:
/usr/include/c++/16/bits/stl_bvector.h:701:30: warning: ‘*(std::_Bvector_base<std::allocator<bool> >*)((char*)&<unnamed> + offsetof(pybind11::detail::args_convert_vector<6>,pybind11::detail::small_vector<bool, 6>::m_repr) + 8).std::_Bvector_base<std::allocator<bool> >::_M_impl.std::_Bvector_base<std::allocator<bool> >::_Bvector_impl::std::_Bvector_base<std::allocator<bool> >::_Bvector_impl_data.std::_Bvector_base<std::allocator<bool> >::_Bvector_impl_data::_M_start.std::_Bit_iterator::std::_Bit_iterator_base.std::_Bit_iterator_base::_M_p’ may be used uninitialized [-Wmaybe-uninitialized]
  701 |         if (_M_impl._M_start._M_p)
      |             ~~~~~~~~~~~~~~~~~^~~~
/home/sytoplis/pythonModuleTest/Dependencies/pybind11/pybind11-3.0.4/include/pybind11/pybind11.h: In static member function ‘static PyObject* pybind11::cpp_function::dispatcher(PyObject*, PyObject* const*, pybind11::size_t, PyObject*)’:
/home/sytoplis/pythonModuleTest/Dependencies/pybind11/pybind11-3.0.4/include/pybind11/pybind11.h:1203:87: note: ‘<anonymous>’ declared here
 1203 |                         = args_convert_vector<arg_vector_small_size>(func.nargs, false);
      |                                                                                       ^
In function ‘std::ptrdiff_t std::operator-(const _Bit_iterator_base&, const _Bit_iterator_base&)’,
    inlined from ‘std::vector<bool, _Alloc>::size_type std::vector<bool, _Alloc>::size() const [with _Alloc = std::allocator<bool>]’ at /usr/include/c++/16/bits/stl_bvector.h:1128:32,
    inlined from ‘std::size_t pybind11::detail::small_vector<bool, kRequestedInlineSize>::size() const [with long unsigned int kRequestedInlineSize = 6]’ at /home/sytoplis/pythonModuleTest/Dependencies/pybind11/pybind11-3.0.4/include/pybind11/detail/argument_vector.h:247:39,
    inlined from ‘std::size_t pybind11::detail::small_vector<bool, kRequestedInlineSize>::size() const [with long unsigned int kRequestedInlineSize = 6]’ at /home/sytoplis/pythonModuleTest/Dependencies/pybind11/pybind11-3.0.4/include/pybind11/detail/argument_vector.h:243:17,
    inlined from ‘static PyObject* pybind11::cpp_function::dispatcher(PyObject*, PyObject* const*, pybind11::size_t, PyObject*)’ at /home/sytoplis/pythonModuleTest/Dependencies/pybind11/pybind11-3.0.4/include/pybind11/pybind11.h:1145:77:
/usr/include/c++/16/bits/stl_bvector.h:302:44: warning: ‘((const std::_Bit_iterator*)((char*)&used_kwargs + offsetof(pybind11::detail::small_vector<bool, 6>,pybind11::detail::small_vector<bool, 6>::m_repr) + 8))[1].std::_Bit_iterator::std::_Bit_iterator_base.std::_Bit_iterator_base::_M_p’ may be used uninitialized [-Wmaybe-uninitialized]
  302 |       return (int(_S_word_bit) * (__x._M_p - __y._M_p)
      |                                   ~~~~~~~~~^~~~~~~~~~
/home/sytoplis/pythonModuleTest/Dependencies/pybind11/pybind11-3.0.4/include/pybind11/pybind11.h: In static member function ‘static PyObject* pybind11::cpp_function::dispatcher(PyObject*, PyObject* const*, pybind11::size_t, PyObject*)’:
/home/sytoplis/pythonModuleTest/Dependencies/pybind11/pybind11-3.0.4/include/pybind11/pybind11.h:1100:59: note: ‘((const std::_Bit_iterator*)((char*)&used_kwargs + offsetof(pybind11::detail::small_vector<bool, 6>,pybind11::detail::small_vector<bool, 6>::m_repr) + 8))[1].std::_Bit_iterator::std::_Bit_iterator_base.std::_Bit_iterator_base::_M_p’ was declared here
 1100 |                 small_vector<bool, arg_vector_small_size> used_kwargs(
      |                                                           ^~~~~~~~~~~
In function ‘std::ptrdiff_t std::operator-(const _Bit_iterator_base&, const _Bit_iterator_base&)’,
    inlined from ‘std::vector<bool, _Alloc>::size_type std::vector<bool, _Alloc>::size() const [with _Alloc = std::allocator<bool>]’ at /usr/include/c++/16/bits/stl_bvector.h:1128:32,
    inlined from ‘std::size_t pybind11::detail::small_vector<bool, kRequestedInlineSize>::size() const [with long unsigned int kRequestedInlineSize = 6]’ at /home/sytoplis/pythonModuleTest/Dependencies/pybind11/pybind11-3.0.4/include/pybind11/detail/argument_vector.h:247:39,
    inlined from ‘std::size_t pybind11::detail::small_vector<bool, kRequestedInlineSize>::size() const [with long unsigned int kRequestedInlineSize = 6]’ at /home/sytoplis/pythonModuleTest/Dependencies/pybind11/pybind11-3.0.4/include/pybind11/detail/argument_vector.h:243:17,
    inlined from ‘static PyObject* pybind11::cpp_function::dispatcher(PyObject*, PyObject* const*, pybind11::size_t, PyObject*)’ at /home/sytoplis/pythonModuleTest/Dependencies/pybind11/pybind11-3.0.4/include/pybind11/pybind11.h:1145:77:
/usr/include/c++/16/bits/stl_bvector.h:303:21: warning: ‘((const std::_Bit_iterator*)((char*)&used_kwargs + offsetof(pybind11::detail::small_vector<bool, 6>,pybind11::detail::small_vector<bool, 6>::m_repr) + 8))[1].std::_Bit_iterator::std::_Bit_iterator_base.std::_Bit_iterator_base::_M_offset’ may be used uninitialized [-Wmaybe-uninitialized]
  303 |               + __x._M_offset - __y._M_offset);
      |                 ~~~~^~~~~~~~~
/home/sytoplis/pythonModuleTest/Dependencies/pybind11/pybind11-3.0.4/include/pybind11/pybind11.h: In static member function ‘static PyObject* pybind11::cpp_function::dispatcher(PyObject*, PyObject* const*, pybind11::size_t, PyObject*)’:
/home/sytoplis/pythonModuleTest/Dependencies/pybind11/pybind11-3.0.4/include/pybind11/pybind11.h:1100:59: note: ‘((const std::_Bit_iterator*)((char*)&used_kwargs + offsetof(pybind11::detail::small_vector<bool, 6>,pybind11::detail::small_vector<bool, 6>::m_repr) + 8))[1].std::_Bit_iterator::std::_Bit_iterator_base.std::_Bit_iterator_base::_M_offset’ was declared here
 1100 |                 small_vector<bool, arg_vector_small_size> used_kwargs(
      |                                                           ^~~~~~~~~~~

From the Discussion i read that this should have been working in 3.0.1, but I didnt test that myself.
In the Discussion there was also a possible solution hypothesized, but i know the codebase too little to judge if that would actually work.
My g++ version is g++ (GCC) 16.1.1 20260515 (Red Hat 16.1.1-2)

Reproducible example code
#include <pybind11/pybind11.h>

namespace py = pybind11;

int add(int i, int j) {
    return i + j;
}

PYBIND11_MODULE(example, m, py::mod_gil_not_used()) {
    m.doc() = "pybind11 example plugin"; // optional module docstring

    m.def("add", &add, "A function that adds two numbers");
}
Is this a regression? Put the last known working version here if it is.

3.0.1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with include/pybind11/detail/argument_vector.h and the dispatcher paths in include/pybind11/pybind11.h, then reproduce the report using the docs example.cpp and its GCC 16 -O3 command. Done means the example still compiles and works while the reported -Wmaybe-uninitialized warnings no longer appear.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.