Repository navigation
memcpy() usage without #include <string.h> in pwdmodule.c #121617
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Jul 11, 2024 For now, GCC and Clang are the officially supported toolchains for Python. I'm not sure we need support extra compiler set.
But for now, this is expected behavior
For now, GCC and Clang are the officially supported toolchains
From what I'm seeing, there are new code written as recent as 3.13 that explicitly provide support for 1. standard C11 compiler and 2. MSVC.
cpython/Include/cpython/pyatomic.h
Lines 488 to 498 in 2bd960b
#if _Py_USE_GCC_BUILTIN_ATOMICS # define Py_ATOMIC_GCC_H # include "cpython/pyatomic_gcc.h" # undef Py_ATOMIC_GCC_H #elif __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_ATOMICS__) # define Py_ATOMIC_STD_H # include "cpython/pyatomic_std.h" # undef Py_ATOMIC_STD_H #elif defined(_MSC_VER) # define Py_ATOMIC_MSC_H The define logic of
_Py_TYPEOF()clearly has "implementations other than GCC and Clang" in mind,
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
even if the guarding condition only exists for MSVC, it would simply be!defined(_MSC_VER).If fact, were it be
!defined(_MSC_VER)this issue would not exist, since the tested compiler actually support__typeof__.Reacted by Petr Viktorin@vstinner, AFAIK you both rewrote
Py_CLEARand removedstring.h. Are you interested in this issue?I don't think fixing only
pwdmodule.his correct;Py_CLEARshould work for all users ofPython.h.I don't think fixing only pwdmodule.h is correct; Py_CLEAR should work for all users of Python.h.
I agree. I wrote #144666 to include
<string.h>header if the_Py_TYPEOFmacro is not defined.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.
My PR also modify
_Py_TYPEOF()macro to use C23typeof()if available.I wrote a configure check for
typeof()but it didn't work as expected. It says thattypeof()is available, but then building Python fails becausetypeof()is undefined. The problem is thatconfigureadds-std=c11toCFLAGS_NODIST, andtypeof()is only defined in C23 or newer. I abandoned theconfigurechange, it's too tricky for me to get it right (run the checks with the same C flags than the ones used to build Python).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.
For fun, I tried building Python 3.15 (main branch) with
tcc(TinyCC), but it failed to compile_Py_thread_local.The following program fails to build with:
error: _Thread_local is not implemented(usingtcc -std=c11).int main() { _Thread_local int myvar; return 0; }
I fixed the Py_CLEAR() issue in the main branch:
Python.hnow always includes<string.h>.I don't think that it's worth it to backport the change to 3.13 and 3.14 branches. I close the issue.
Thanks for the bug report @fuhsnn.
Thanks @vstinner!
Reacted by Victor Stinner3 remaining items
- added 6 commits that reference this issue
on Sep 8, 2026 - added a commit that references this issue
on Sep 10, 2026 - added a commit that references this issue
on Oct 10, 2026
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDone
Bug report
Bug description:
For a non-
gcc/clangcompiler,_Py_TYPEOFis not defined.cpython/Include/pyport.h
Lines 548 to 551 in c08a302
Which leads to
Py_CLEAR()fallback to an implementation withmemcpy()call.cpython/Include/object.h
Lines 1016 to 1027 in c08a302
Py_CLEAR()is used in./Modules/pwdmodule.ccpython/Modules/pwdmodule.c
Lines 355 to 358 in c08a302
without including
string.hcpython/Modules/pwdmodule.c
Lines 12 to 16 in c08a302
For a non-
gcc/clangcompiler, this may fail due to missingmemcpy()declaration.On a side note,
typeofis widely implemented among alternative C compilers like TinyCC and cproc, kefir, chibicc, and is standardized in C23. It could be beneficial to enabletypeofusage throughconfigureoption or probing, instead of hard-coded off on non-gcc/clangcompilers.CPython versions tested on:
3.13
Operating systems tested on:
Linux
Linked PRs