Skip to content

ctypes: Windows callback argument reference leak when TryAddRef fails #162412

Description

@manabeai

Bug description

On Windows, _CallPythonObject() in Modules/_ctypes/callbacks.c leaks a reference to the current callback argument object when TryAddRef() fails.

The function creates a CDataObject, stores its reference in args[i], and then calls TryAddRef().

If TryAddRef() returns a negative value, execution jumps to the Error label. However, the cleanup loop at Done only decrements references for args[0] through args[i-1], leaving the current args[i] reference unreleased.

I reproduced this issue on CPython 3.14.10 (Windows, 64-bit) and confirmed that adding Py_DECREF(obj) before goto Error eliminates the observed leak.

Steps to reproduce

  1. Use CPython 3.14.10 on Windows.

  2. Run the attached probe_windows.py:

    py -3.14 probe_windows.py --label baseline --json-out baseline_v3.json
  3. The script creates a ctypes.c_void_p subclass with a specially constructed string-subclass key in its type dictionary. This key raises RuntimeError during comparison with _needs_com_addref_, causing PyDict_Contains() inside TryAddRef() to return -1.

  4. The script uses weak references and garbage collection to check whether the temporary callback argument objects are released.

Actual behavior

Results from the unmodified CPython 3.14.10 interpreter (128 iterations per case):

Test case Objects created Objects alive after GC Python callback calls
Callback raises an exception 128 0 128
Argument constructor raises 128 0 0
TryAddRef() dictionary lookup raises 128 128 0

The failing case also recorded 128 dictionary comparisons and 128 unraisable exceptions.

lookup_raises: created=128 alive=128 callback_calls=0 comparisons=128 unraisable=128
RESULT: LEAK_REPRODUCED

All 128 objects remained alive after garbage collection in the failing case. The Python callback itself was never invoked.

Expected behavior

All temporary argument objects should have their references released when callback argument conversion fails, including when TryAddRef() returns an error.

Root cause

In Modules/_ctypes/callbacks.c, _CallPythonObject() contains:

memcpy(obj->b_ptr, *pArgs, info->size);
args[i] = (PyObject *)obj;

#ifdef MS_WIN32
if (TryAddRef(cnv, obj) < 0) {
    goto Error;
}
#endif

However, the cleanup code is:

Done:
    for (j = 0; j < i; j++) {
        Py_DECREF(args[j]);
    }
    return;

Because i is not incremented before the error jump, the reference held by args[i] is not decremented.

This appears to be a reference ownership issue in the error path rather than an off-by-one error in the cleanup loop itself.

Proposed fix

Explicitly release the current object when TryAddRef() fails:

 #ifdef MS_WIN32
 if (TryAddRef(cnv, obj) < 0) {
+    Py_DECREF(obj);
     goto Error;
 }
 #endif

Verification of the fix

I applied the one-line patch to CPython 3.14.10, successfully built the interpreter using the Windows PCbuild build system, and reran the same reproduction script.

The patched interpreter produced:

lookup_raises: created=128 alive=0 callback_calls=0 comparisons=128 unraisable=128
RESULT: NO_LEAK_OBSERVED

The comparison of the original and patched interpreters produced:

baseline: LEAK_REPRODUCED
patched: NO_LEAK_OBSERVED

callback_raises      refs alive baseline=0 patched=0
constructor_raises   refs alive baseline=0 patched=0
lookup_raises        refs alive baseline=128 patched=0

CONFIRMED: leak in baseline and no leak after patch in this experiment

The two control cases continued to release their objects correctly.

Environment

  • Python version: CPython 3.14.10
  • Operating system: Windows
  • Architecture: 64-bit
  • Module: _ctypes
  • Affected function: _CallPythonObject()
  • Error source: TryAddRef() / PyDict_Contains()
  • Patched interpreter: Locally built CPython 3.14.10 using PCbuild

Impact and scope

This issue affects the Windows-specific callback argument conversion error path.

The reproducer requires a specially constructed ctypes argument type whose dictionary lookup raises an exception. Ordinary ctypes callbacks and typical COM usage have not been shown to trigger this condition.

The confirmed impact is a reference leak when this error path is reached repeatedly. No externally exploitable security impact has been established.

Attachments

CPython versions tested on:

3.14

Operating systems tested on:

Windows

Linked PRs

Activity

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

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions