Skip to content

memcpy() usage without #include <string.h> in pwdmodule.c #121617

Closed

Description

@fuhsnn

Bug report

Bug description:

For a non-gcc/clang compiler, _Py_TYPEOF is not defined.

cpython/Include/pyport.h

Lines 548 to 551 in c08a302

// The macro is only defined if GCC or clang compiler is used.
#if defined(__GNUC__) || defined(__clang__)
# define _Py_TYPEOF(expr) __typeof__(expr)
#endif

Which leads to Py_CLEAR() fallback to an implementation with memcpy() call.

cpython/Include/object.h

Lines 1016 to 1027 in c08a302

#else
#define Py_CLEAR(op) \
do { \
PyObject **_tmp_op_ptr = _Py_CAST(PyObject**, &(op)); \
PyObject *_tmp_old_op = (*_tmp_op_ptr); \
if (_tmp_old_op != NULL) { \
PyObject *_null_ptr = _Py_NULL; \
memcpy(_tmp_op_ptr, &_null_ptr, sizeof(PyObject*)); \
Py_DECREF(_tmp_old_op); \
} \
} while (0)
#endif

Py_CLEAR() is used in ./Modules/pwdmodule.c

cpython/Modules/pwdmodule.c

Lines 355 to 358 in c08a302

static int pwdmodule_clear(PyObject *m) {
Py_CLEAR(get_pwd_state(m)->StructPwdType);
return 0;
}

without including string.h
#include <errno.h> // ERANGE
#include <pwd.h> // getpwuid()
#include <unistd.h> // sysconf()

For a non-gcc/clang compiler, this may fail due to missing memcpy() declaration.

./Include/object.h:1017: #define Py_CLEAR(op)     do {         PyObject **_tmp_op_ptr = _Py_CAST(PyObject**, &(op));         PyObject *_tmp_old_op = (*_tmp_op_ptr);         if (_tmp_old_op != NULL) {             PyObject *_null_ptr = _Py_NULL;             memcpy(_tmp_op_ptr, &_null_ptr, sizeof(PyObject*));             Py_DECREF(_tmp_old_op);         }     } while (0)
                                                                                                                                                                                                                                                                ^ implicit declaration of a function
./Modules/pwdmodule.c:356:     Py_CLEAR(get_pwd_state(m)->StructPwdType);
                               ^ in expansion of macro

On a side note, typeof is widely implemented among alternative C compilers like TinyCC and cproc, kefir, chibicc, and is standardized in C23. It could be beneficial to enable typeof usage through configure option or probing, instead of hard-coded off on non-gcc/clang compilers.

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added a commit that references this issue on Feb 15, 2026
  2. added a commit that references this issue on Feb 28, 2026
  3. added a commit that references this issue on Apr 25, 2026
  4. added 6 commits that reference this issue on Sep 8, 2026
  5. added 2 commits that reference this issue on Sep 12, 2026
  6. added 2 commits that reference this issue on Sep 14, 2026
  7. added a commit that references this issue on Oct 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions