Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
neonene opened this issue Apr 7, 2024 · 3 comments
Closed
Labels
topic-argument-clinic type-bug An unexpected behavior, bug, or error

Comments

@neonene
Copy link
Contributor

neonene commented Apr 7, 2024

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

@neonene neonene added the type-bug An unexpected behavior, bug, or error label Apr 7, 2024
@erlend-aasland
Copy link
Contributor

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

@erlend-aasland
Copy link
Contributor

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

vstinner added a commit to vstinner/cpython that referenced this issue Apr 15, 2024
diegorusso pushed a commit to diegorusso/cpython that referenced this issue Apr 17, 2024
erlend-aasland pushed a commit that referenced this issue Apr 17, 2024
@neonene
Copy link
Contributor Author

neonene commented Apr 18, 2024

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

@neonene neonene closed this as completed Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-argument-clinic type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants