cpython/Modules/_testcapi
Miss Islington (bot) a2e551610f
[3.15] gh-151297: Fix undefined behavior in _PyObject_MiRealloc (GH-151358) (GH-151388)
The standard says that a call to `memcpy` must pass a valid source and
destination pointer even if the size is 0, so we must avoid calling
`memcpy` when our source pointer is NULL. If we don't, an optimizing
compiler can decide that the pointer must be non-NULL based on the
presence of UB, and optimize out checks for null pointers.

Specifically, note that the standard says:

    Where an argument declared as size_t n specifies the length of the
    array for a function, n can have the value zero on a call to that
    function. Unless explicitly stated otherwise in the description of
    a particular function in this subclause, pointer arguments on such
    a call shall still have valid values, as described in 7.1.4.

And section 7.1.4 says:

    If an argument to a function has an invalid value (such as a value
    outside the domain of the function, or a pointer outside the address
    space of the program, or a null pointer, or a pointer to
    non-modifiable storage when the corresponding parameter is not
    const-qualified) or a type (after default argument promotion) not
    expected by a function with a variable number of arguments, the
    behavior is undefined.

The specification for `memcpy` doesn't state that it's allowed to be
called with null pointers, and Linux's `/usr/include/string.h` declares
`memcpy` as `__nonnull ((1, 2))`.
(cherry picked from commit c37599200f)

Co-authored-by: Matt Wozniski <mwozniski@bloomberg.net>
2026-06-12 01:47:01 +00:00
..
clinic gh-130317: Fix test_pack_unpack_roundtrip() and add docs (#133204) 2025-05-01 16:20:36 +02:00
abstract.c gh-135075: Make PyObject_SetAttr() fail with NULL value and exception (#136180) 2025-07-03 14:51:44 +02:00
buffer.c gh-140557: Force alignment of empty bytearray and array.array buffers (GH-140559) 2026-01-26 16:45:17 +01:00
bytes.c gh-129813: Fix PyBytesWriter tests (#139892) 2025-10-10 13:01:06 +02:00
code.c gh-128911: Use PyImport_ImportModuleAttr() function (#129657) 2025-02-05 11:03:58 +01:00
codec.c gh-111495: Add tests for PyCodec_* C API (#123343) 2024-09-29 15:22:39 +00:00
complex.c gh-128813: hide mixed-mode functions for complex arithmetic from C-API (#131703) 2025-04-22 14:18:18 +02:00
config.c gh-107954, PEP 741: Add PyConfig_Get()/Set() functions (#123472) 2024-09-02 23:25:08 +02:00
datetime.c gh-115754: Use Py_GetConstant(Py_CONSTANT_EMPTY_STR) (#125194) 2024-10-09 17:15:23 +02:00
dict.c gh-141510: Test frozendict C API (#145081) 2026-02-21 17:00:23 +01:00
docstring.c gh-111178: remove redundant casts for functions with correct signatures (#131673) 2025-04-01 17:18:11 +02:00
exceptions.c GH-137623: Use an AC decorator for docstring line length enforcement (#137690) 2025-08-18 18:29:00 +01:00
file.c gh-132162: Add tests for Py_UniversalNewlineFgets() (#132164) 2025-04-24 17:43:48 +02:00
float.c gh-143429: Use compile-time NaN encoding detection for test_struct (#143432) 2026-01-09 10:46:48 +00:00
frame.c gh-93649: Move PyFrame C API tests to test_capi (#129512) 2025-01-31 13:51:58 +01:00
function.c gh-144629: Add test for the PyFunction_GetAnnotations() function (#144630) 2026-02-10 14:38:24 +00:00
gc.c gh-111178: remove redundant casts for functions with correct signatures (#131673) 2025-04-01 17:18:11 +02:00
getargs.c
hash.c gh-122854: Add Py_HashBuffer() function (#122855) 2024-08-30 15:42:27 +00:00
heaptype.c gh-145921: Add "_DuringGC" functions for tp_traverse (GH-145925) 2026-04-08 09:15:11 +02:00
immortal.c gh-143050: Correct PyLong_FromString() to use _PyLong_Negate() (#145901) 2026-03-31 13:17:49 +00:00
import.c [3.15] gh-149321: Remove lazy_imports=none startup mode (GH-149389) (#150129) 2026-06-08 22:55:57 +00:00
list.c gh-93649: Move _testcapi tests to specific files (#129544) 2025-02-01 13:39:16 +01:00
long.c gh-147988: Initialize digits in long_alloc() in debug mode (#147989) 2026-04-02 11:55:34 +00:00
mem.c [3.15] gh-151297: Fix undefined behavior in _PyObject_MiRealloc (GH-151358) (GH-151388) 2026-06-12 01:47:01 +00:00
modsupport.c gh-144175: Add PyArg_ParseArray() function (#144283) 2026-03-06 21:57:44 +00:00
module.c gh-149044: Implement PEP 820 – PySlot: Unified slot system for the C API (GH-149055) 2026-05-05 09:18:04 +02:00
monitoring.c gh-79315: Remove Include/pylock.h and Include/monitoring.h (#139731) 2025-10-08 00:49:24 +02:00
numbers.c gh-111495: Add tests for PyNumber C API (#111996) 2024-08-26 15:59:22 +02:00
object.c [3.15] gh-148829: Make sentinels' repr and module customizable (GH-149654) (#150092) 2026-05-22 07:44:34 -07:00
parts.h [3.15] gh-151130: Add more tests for PyWeakref_* C API (GH-151131) (GH-151140) 2026-06-09 11:37:56 +00:00
pyatomic.c gh-121368: Fix seq lock memory ordering in _PyType_Lookup (#121388) 2024-07-08 14:52:07 -04:00
README.txt
run.c Fix typos in docs, error messages and comments (#123336) 2024-08-28 14:41:04 +03:00
set.c gh-93649: Move _testcapi tests to specific files (#129544) 2025-02-01 13:39:16 +01:00
structmember.c gh-127572: Fix test_structmembers initialization (GH-127577) 2024-12-04 09:58:22 +01:00
time.c gh-136300: Modify C tests to conform to PEP-737 (GH-136301) 2025-07-11 15:18:35 +02:00
tuple.c gh-111489: Add PyTuple_FromArray() function (#139691) 2025-10-10 08:54:12 +02:00
type.c gh-149044: Implement PEP 820 – PySlot: Unified slot system for the C API (GH-149055) 2026-05-05 09:18:04 +02:00
unicode.c Improve tests for the PyUnicodeWriter C API (GH-146157) 2026-03-19 19:15:30 +02:00
util.h gh-111495: Add tests for PyCodec_* C API (#123343) 2024-09-29 15:22:39 +00:00
vectorcall.c GH-137623: Use an AC decorator for docstring line length enforcement (#137690) 2025-08-18 18:29:00 +01:00
watchers.c [3.15] gh-145235: Make dict watcher API thread-safe for free-threaded builds (gh-145233) (#149690) 2026-05-11 14:23:39 -04:00
weakref.c [3.15] gh-151130: Add more tests for PyWeakref_* C API (GH-151131) (GH-151140) 2026-06-09 11:37:56 +00:00

Tests in this directory are compiled into the _testcapi extension.
The main file for the extension is Modules/_testcapimodule.c, which
calls `_PyTestCapi_Init_*` from these functions.

General guideline when writing test code for C API.
* Use Argument Clinic to minimise the amount of boilerplate code.
* Add a newline between the argument spec and the docstring.
* If a test description is needed, make sure the added docstring clearly and succinctly describes purpose of the function.
* DRY, use the clone feature of Argument Clinic.
* Try to avoid adding new interned strings; reuse existing parameter names if possible. Use the `as` feature of Argument Clinic to override the C variable name, if needed.