Repository navigation
memcpy() usage without #include <string.h> in pwdmodule.c #121617
Copy link
Copy link
Closed
Labels
type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
Bug description:
For a non-gcc/clang compiler, _Py_TYPEOF is not defined.
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.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.cLines 355 to 358 in c08a302
| static int pwdmodule_clear(PyObject *m) { | |
| Py_CLEAR(get_pwd_state(m)->StructPwdType); | |
| return 0; | |
| } |
without including
string.hLines 12 to 16 in c08a302
| #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
- gh-121617: Include string.h in pwdmodule.c if _Py_TYPEOF is undefined #121643
- gh-121617: Include <string.h> for Py_CLEAR() macro #144666
- gh-121617: Fix Py_CLEAR() in C++: replace NULL with _Py_NULL #157067
- [3.15] gh-121617: Fix Py_CLEAR() in C++: replace NULL with _Py_NULL (GH-157067) #157186
- [3.14] gh-121617: Fix Py_CLEAR() in C++: replace NULL with _Py_NULL (#157067) #157187
- gh-121617: Fix Py_CLEAR() memcpy in C++: replace NULL with _Py_NULL #157188
- [3.13] gh-121617: Fix Py_CLEAR() in C++: replace NULL with _Py_NULL (#157067) #157189
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Projects
- StatusShow more project fieldsDone
Activity
Zheaoli commentedon 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
fuhsnn commentedon Jul 11, 2024
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
The define logic of
_Py_TYPEOF()clearly has "implementations other than GCC and Clang" in mind,cpython/Include/pyport.h
Lines 548 to 551 in c08a302
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__.pythongh-121617: Include string.h in pwdmodule.c if _Py_TYPEOF is und…
encukou commentedon Feb 10, 2026
@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.pythongh-121617: Include <string.h> for Py_CLEAR()
pythongh-121617: Include <string.h> for Py_CLEAR() macro
pythongh-121617: Include <string.h> for Py_CLEAR() macro
vstinner commentedon Feb 10, 2026
I agree. I wrote #144666 to include
<string.h>header if the_Py_TYPEOFmacro is not defined.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).vstinner commentedon Feb 10, 2026
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).gh-121617: Include <string.h> for Py_CLEAR() macro (#144666)
vstinner commentedon Feb 12, 2026
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.
fuhsnn commentedon Feb 12, 2026
Thanks @vstinner!
3 remaining items
pythongh-121617: Include <string.h> for Py_CLEAR() macro (python#144666)
pythongh-121617: Include <string.h> for Py_CLEAR() macro (python#144666)
pythongh-121617: Include <string.h> for Py_CLEAR() macro (python#144666)
gh-121617: Fix Py_CLEAR() in C++: replace NULL with _Py_NULL (#157067)
pythongh-121617: Fix Py_CLEAR() memcpy in C++: replace NULL with _Py_…
pythongh-121617: Fix Py_CLEAR() in C++: replace NULL with _Py_NULL (p…
[3.14] gh-121617: Fix Py_CLEAR() in C++: replace NULL with _Py_NULL (#…
[3.13] gh-121617: Fix Py_CLEAR() in C++: replace NULL with _Py_NULL (#…
gh-121617: Fix Py_CLEAR() memcpy in C++: replace NULL with _Py_NULL (#…
Merge branch 'main' into tachyon-native-callable
pythongh-121617: Fix Py_CLEAR() in C++: replace NULL with _Py_NULL (p…
pythongh-121617: Fix Py_CLEAR() memcpy in C++: replace NULL with _Py_…
pythongh-121617: Fix Py_CLEAR() in C++: replace NULL with _Py_NULL (p…
pythongh-121617: Fix Py_CLEAR() memcpy in C++: replace NULL with _Py_…
[3.15] gh-121617: Fix Py_CLEAR() in C++: replace NULL with _Py_NULL (G…