-
Notifications
You must be signed in to change notification settings - Fork 2.3k
[BUG]: Crash due to invalid free during cleanup, if any enum is registerd, due to attempting to free a string literal #5976
Description
EDIT (2026-03-27): Related: #5991, #6010. Root cause: def_property_static calls process_attributes::init on already-initialized function records (after initialize_generic's strdup loop has run), so the "self" arg added by append_self_arg_if_needed remains a string literal. destruct() then calls free() on that literal. Introduced by #5486.
Required prerequisites
- Make sure you've read the documentation. Your issue may be addressed there.
- Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
- Consider asking first in the Gitter chat room or in a Discussion.
What version (or hash if on master) of pybind11 are you using?
Problem description
Importing this and then exiting Python causes a crash at
pybind11/include/pybind11/pybind11.h
Line 824 in 4f81a12
| std::free(const_cast<char *>(arg.name)); |
arg.name is "self", and it appears to be a string literal instead of a malloced string.
Said literal is assigned to it at
pybind11/include/pybind11/attr.h
Line 494 in 4f81a12
| r->args.emplace_back("self", nullptr, handle(), /*convert=*/true, /*none=*/false); |
Editing that function to
malloc the string seems to fix the crash.
I've bisected this, the offending commit is 1b7aa0b
I'm not sure if it matters, but I'm on CPython 3.10 on Ubuntu 22.04.
Reproducible example code
#include <pybind11/pybind11.h>
enum E {};
PYBIND11_MODULE(example, m)
{
pybind11::enum_<E> e(m, "E");
}