Skip to content

Argument Clinic reads a missing optional positional argument in functions with **kwds #156219

Description

@serhiy-storchaka

Bug report

Argument Clinic generates code which reads an optional positional argument even when it was not passed, if the function also has a **kwds parameter.

/*[clinic input]
m.f
    a: object
    b: object = None
    /
    **kwds: dict
[clinic start generated code]*/

generates

    if (!_PyArg_CheckPositional("f", PyTuple_GET_SIZE(args), 1, 2)) {
        goto exit;
    }
    a = PyTuple_GET_ITEM(args, 0);
    b = PyTuple_GET_ITEM(args, 1);

so f(1) reads past the end of the argument tuple and usually crashes. For the same signature without **kwds the optional argument is guarded with if (nargs < 2) { goto skip_optional; }.

No function in the stdlib uses this combination yet, so it is only reproducible with new code — I ran into it while converting _csv.reader() (gh-155496).

Support for **kwds was added in 3.15 (gh-64490), so 3.15 and the main branch are affected.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.15pre-release feature fixes, bugs and security fixes3.16new features, bugs and security fixestopic-argument-clinictype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions