Skip to content

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

Closed

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

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Apr 7, 2024
  2. erlend-aasland commented on Apr 8, 2024

    @erlend-aasland
    Contributor

    Yes, Argument Clinic should explicitly check that defining_class is a positional parameter.

  3. erlend-aasland commented on Apr 8, 2024

    @erlend-aasland
    Contributor

    Hm, we should already be checking that. So, perhaps that check is malfunctioning.

  4. added 2 commits that reference this issue on Apr 15, 2024
  5. added a commit that references this issue on Apr 16, 2024
  6. added 2 commits that reference this issue on Apr 17, 2024
  7. added 2 commits that reference this issue on Apr 17, 2024
  8. neonene commented on Apr 18, 2024

    @neonene
    ContributorAuthor

    Closing. Thanks, @erlend-aasland, @vstinner.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions