Skip to content

Never pass a non-string function wrapper binding to str only C API - #371

Merged
GrahamDumpleton merged 1 commit into
developfrom
bugfix/non-string-binding
Nov 8, 2026
Merged

GrahamDumpleton merged 1 commit into
developfrom
bugfix/non-string-binding

Conversation

@GrahamDumpleton

@GrahamDumpleton GrahamDumpleton commented Nov 8, 2026 •

Copy link
Copy Markdown
Owner

When using the C extension, a wrapt.BoundFunctionWrapper or wrapt.FunctionWrapper whose binding was not a string could crash the interpreter, as reported in #368:

import wrapt

def f():
    return 1

def w(wrapped, instance, args, kwargs):
    return "ok"

wrapt.BoundFunctionWrapper(f, None, w, None, object())()  # SIGSEGV

The C extension compared the binding using PyUnicode_CompareWithASCIIString(), which does not check the type of the object it is given. The binding is normally one of the interned strings wrapt chooses itself, but a wrapper can be constructed directly with any object as the binding, and a wrapt.FunctionWrapper wrapping an existing wrapper, as happens when a decorator is applied to it, inherits that binding. The comparison was made in five places in src/wrapt/_wrappers.c, in the following code paths:

  • BoundFunctionWrapper.__call__(), the case in the report.
  • _FunctionWrapperBase.__call__() when there is no instance, so any FunctionWrapper which inherited such a binding.
  • __get__() on an unbound wrapper, checking for the "builtin" and "class" bindings.
  • __get__() on a bound wrapper with no instance, which rebinds via the parent.

All of these now use the existing wrapt_name_equals() helper against the interned binding names held in module state. It returns false for anything which is not a str, so a non-string binding takes the same fallback path as in the pure Python implementation. A str subclass is still compared by value. The comment on wrapt_name_equals() has been updated to state that it accepts any object.

Tests

tests/core/test_non_string_binding.py covers each of the crash sites, with expected behaviour matching the pure Python implementation, plus string bindings, including str subclasses, to check these are still recognised.

  • Pure Python implementation: all 15 tests pass before and after the change.
  • C extension before the fix: 10 of the 15 tests crash with SIGSEGV, run one per process. The remaining five are cases which never compared a non-string binding.
  • C extension after the fix: all pass. Removing the guard at any one of the five sites in turn causes at least one test to crash, so each site is covered.
  • just test passes on all 24 runs: Python 3.10 to 3.15, plus 3.14t and 3.15t, each without C extensions, with C extensions, and with C extensions disabled at runtime.

Acknowledgements

Thanks to @theosotr for the report in #368, and to @edsonsims for #369, which fixed the case of calling a wrapt.BoundFunctionWrapper. This PR extends that fix to the other places the binding is checked, and supersedes #369. Both are credited in the change note in docs/changes.rst.

Closes #368.
Closes #369.

The C extension compared the binding of a function wrapper using
PyUnicode_CompareWithASCIIString(), which does not check the type of the
object it is given, so a binding which was not a string crashed the
interpreter. A wrapper can be constructed directly with any object as
the binding, and a FunctionWrapper wrapping an existing wrapper, such as
when a decorator is applied to it, inherits that binding. The crash
occurred when calling a bound or unbound wrapper and when binding one via
the descriptor protocol.

All binding comparisons now use wrapt_name_equals() against the interned
binding names in module state. A non-string binding never matches, so
the wrapper takes the same fallback path as in the pure Python
implementation.

Closes #368.
Closes #369.
@GrahamDumpleton
GrahamDumpleton merged commit 71835a8 into develop Nov 8, 2026
42 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BoundFunctionWrapper segfaults when binding is not a string

1 participant