Skip to content

classmethod and staticmethod can crash with NULL / uninitialized callables #144330

Description

@colesbury

Crash report

The following code crashes in a debug build:

f = classmethod.__new__(classmethod, None)
print(f)

Or equivalently:

class MyStaticMethod(staticmethod):
    def __init__(self, func):
        print(self)

print(MyStaticMethod(lambda x: x))
Objects/unicodeobject.c:3103: const char *unicode_fromformat_arg(_PyUnicodeWriter *, const char *, va_list *): Assertion `obj' failed.

The problem is that sm_callable/cm_callable fields are only initialized in the __init__ function and not all code (like the repr) handles the NULL case.

Additionally, even though __func__ isn't writable, you can modify it by calling __init__ on an already initialized staticmethod or classmethod.

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

    3.13bugs and security fixes3.14bugs and security fixes3.15pre-release feature fixes, bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-crashA hard crash of the interpreter, possibly with a core dump

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions