Skip to content

[BUG]: Crash due to invalid free during cleanup, if any enum is registerd, due to attempting to free a string literal #5976

@adalisk-emikhaylov

Description

@adalisk-emikhaylov

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

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

4f81a12

Problem description

Importing this and then exiting Python causes a crash at

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

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");
}

Is this a regression? Put the last known working version here if it is.

15d9dae

Metadata

Metadata

Assignees

No one assigned

    Labels

    triageNew bug, unverified

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions