Skip to content

[KNOWN TRAP]: segfault with stl.h wrapper for std::map (and no optimization enabled) #4395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -47,6 +47,12 @@ jobs:
args: >
-DPYBIND11_FINDPYTHON=ON
-DCMAKE_CXX_FLAGS="-D_=1"
- runs-on: ubuntu-20.04
python: '3.8'
# Add a build with no optimizations
args: >
-DPYBIND11_FINDPYTHON=ON
-DCMAKE_BUILD_TYPE=none -DCMAKE_CXX_FLAGS="-DNDEBUG"
- runs-on: ubuntu-20.04
python: 'pypy-3.8'
args: >
@@ -293,6 +299,9 @@ jobs:
std: 20
- clang: 14
std: 20
- clang: 10
std: 14
args: -DCMAKE_BUILD_TYPE=none -DCMAKE_CXX_FLAGS="-DNDEBUG"

name: "🐍 3 • Clang ${{ matrix.clang }} • C++${{ matrix.std }} • x64"
container: "silkeh/clang:${{ matrix.clang }}"
@@ -310,6 +319,7 @@ jobs:
-DPYBIND11_WERROR=ON
-DDOWNLOAD_CATCH=ON
-DCMAKE_CXX_STANDARD=${{ matrix.std }}
${{ matrix.args }}
-DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)")

- name: Build
44 changes: 41 additions & 3 deletions include/pybind11/stl.h
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@
#include "detail/common.h"

#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <ostream>
@@ -116,6 +117,10 @@ struct map_caster {
void reserve_maybe(const dict &, void *) {}

public:
#define DEBUG(value) std::cerr << __func__ << ": " << &value << " #: " << value.size() << "\n"

map_caster() { DEBUG(value); }
~map_caster() { DEBUG(value); }
bool load(handle src, bool convert) {
if (!isinstance<dict>(src)) {
return false;
@@ -131,11 +136,13 @@ struct map_caster {
}
value.emplace(cast_op<Key &&>(std::move(kconv)), cast_op<Value &&>(std::move(vconv)));
}
DEBUG(value);
return true;
}

template <typename T>
static handle cast(T &&src, return_value_policy policy, handle parent) {
DEBUG(src);
dict d;
return_value_policy policy_key = policy;
return_value_policy policy_value = policy;
@@ -156,9 +163,40 @@ struct map_caster {
return d.release();
}

PYBIND11_TYPE_CASTER(Type,
const_name("Dict[") + key_conv::name + const_name(", ") + value_conv::name
+ const_name("]"));
protected:
Type value;

public:
static constexpr auto name = _("Dict[") + key_conv::name + _(", ") + value_conv::name + _("]");
template <typename T, enable_if_t<std::is_same<Type, remove_cv_t<T>>::value, int> = 0>
static handle cast(T *src, return_value_policy policy, handle parent) {
if (!src)
return none().release();
if (policy == return_value_policy::take_ownership) {
auto h = cast(std::move(*src), policy, parent);
delete src;
return h;
} else {
return cast(*src, policy, parent);
}
}
operator Type *() {
DEBUG(value);
return &value;
}
operator Type &() {
DEBUG(value);
return value;
}
operator Type &&() && {
DEBUG(value);
return std::move(value);
}

template <typename T>
using cast_op_type = pybind11::detail::movable_cast_op_type<T>;

#undef DEBUG
};

template <typename Type, typename Value>
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -526,7 +526,7 @@ set(PYBIND11_TEST_PREFIX_COMMAND
# A single command to compile and run the tests
add_custom_target(
pytest
COMMAND ${PYBIND11_TEST_PREFIX_COMMAND} ${PYTHON_EXECUTABLE} -m pytest
COMMAND ${PYBIND11_TEST_PREFIX_COMMAND} ${PYTHON_EXECUTABLE} -m pytest -v
${PYBIND11_ABS_PYTEST_FILES}
DEPENDS ${test_targets}
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
4 changes: 4 additions & 0 deletions tests/test_stl.cpp
Original file line number Diff line number Diff line change
@@ -204,6 +204,10 @@ TEST_SUBMODULE(stl, m) {
m.def("load_map", [](const std::map<std::string, std::string> &map) {
return map.at("key") == "value" && map.at("key2") == "value2";
});
m.def("copy_map", [](std::map<std::string, double> map) {
std::cerr << "copy_map: " << &map << " #: " << map.size() << std::endl;
auto m = std::move(map);
});

// test_set
m.def("cast_set", []() { return std::set<std::string>{"key1", "key2"}; });
5 changes: 5 additions & 0 deletions tests/test_stl.py
Original file line number Diff line number Diff line change
@@ -67,6 +67,11 @@ def test_map(doc):
assert doc(m.load_map) == "load_map(arg0: Dict[str, str]) -> bool"


def test_copy_map():
# this segfaults in my use case, but not here:
m.copy_map({"pi": 3.14, "zero": 0.0, "one": 0.0})


def test_set(doc):
"""std::set <-> set"""
s = m.cast_set()