classification
Title: Py_BuildValue is causing crashes with the "L" and "K" format characters when upcasting sub 64-bit integer types
Type: crash Stage: resolved
Components: Interpreter Core, Windows Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: bup, paul.moore, serhiy.storchaka, steve.dower, tim.golden, zach.ware
Priority: normal Keywords:

Created on 2018-04-14 18:32 by bup, last changed 2018-04-14 18:46 by serhiy.storchaka. This issue is now closed.

Messages (2)
msg315300 - (view) Author: Dan Snider (bup) * Date: 2018-04-14 18:32
New to both C and the CPython api so I'm not sure what compiler details matter but this is with VS14 and default compile parameters on windows 10 with 32bit Python.

The following function:

static PyObject *spam(PyObject *module) {
    digit w = 9;
    int x = 9;
    long y = 9;
    int32_t z = 9;
    return Py_BuildValue("(HIK)(lll)(LLL)", w,w,w,x,y,z,x,y,z);  
}

returns:

>>> spam()
((9, 9, 38654705673), (9, 9, 9), (38654705673, 438244858264171835, 6133859984))

Above was the most I could fit in a single Py_BuildValue call without causing a crash (it instantly crashes if "O" or "N" fields are present). I believe this happens because the ints are being upcasted as pointers, which is undefined behavior.
msg315301 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-04-14 18:46
This isn't a bug in CPython API. You need to pass arguments of correct type to functions with a variable number of parameters. The compiler doesn't know what type is expected and can't coerce the value automatically. The same undefined behavior your can see with say printf().
History
Date User Action Args
2018-04-14 18:46:04serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg315301

resolution: not a bug
stage: resolved
2018-04-14 18:32:30bupsettitle: Py_Build value is causing crashes with the "L" and "K" format characters when upcasting sub 64-bit integer types -> Py_BuildValue is causing crashes with the "L" and "K" format characters when upcasting sub 64-bit integer types
2018-04-14 18:32:00bupcreate