Skip to content

PyType_GenericAlloc doesn't initialize the content to NULL #129508

@khatharsis42

Description

@khatharsis42

Bug report

Bug description:

I spent a few hours yesterday debugging this, and thus thought I had to point it out.
Here's my simplified C++ code:

struct Example {
   // Lots of stuff, pointers, stuff like that
}

typedef struct {
  PyObject ob_base;
  struct A Example;
} ExampleObject;

static PyTypeObject ExampleType = {
  .ob_base = PyVarObject_HEAD_INIT(NULL, 0).tp_name = "my_module.Example",
  .tp_basicsize = sizeof(ExampleObject),
  .tp_flags = Py_TPFLAGS_DEFAULT,
  .tp_new = PyType_GenericNew,
};

static PyObject * foo(PyObject* self, PyObject * args) {
     auto* temp = PyObject_New(ExampleObject, &ExampleType);
    // The issue is here
}

The issues lies in the fact that, according to the documentation, the content of temp should be initialized at NULL, but that is clearly not the case here: I printed each byte of temp, and some of them clearly were not NULL.
This silent error caused me many a troubles. I fixed this by using memset, but I really shouldn't have to, so please fix this.
Thanks !

Image

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions