Skip to content

Argument Clinic: Unsafe code generation with defining_class and no slash #117613

@neonene

Description

@neonene

Bug report

Bug description:

When using the Argument Clinic to implement a function with METH_METHOD calling convention, the generated code can cause a crash if defining_class is used without slashing. For example, datetime.now() in Modules/_datetimemodule.c produces:

/*[clinic input] 
@classmethod 
datetime.datetime.now 
 
+   cls: defining_class
    tz: object = None 
        Timezone object. 

Returns new datetime object representing current time local to tz.

If no tz is specified, uses local timezone.
[clinic start generated code]*/

static PyObject *
datetime_datetime_now_impl(PyTypeObject *type, ...
>python -m test test_datetime -m test_tzinfo_now
Running Debug|x64 interpreter...
Using random seed: 389474023
0:00:00 Run 1 test sequentially
0:00:00 [1/1] test_datetime
Windows fatal exception: access violation

Thread 0x00001298 (most recent call first):
  File "C:\cp\Lib\test\libregrtest\win_utils.py", line 47 in _update_load

No crash if NUM_KEYWORDS below is 1 in _datetimemodule.c.h:

static PyObject *
datetime_datetime_now(PyTypeObject *type, PyTypeObject *cls, ...
{
    ...
    #define NUM_KEYWORDS 2

CPython versions tested on:

3.13, CPython main branch

Operating systems tested on:

Windows

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions