-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
bpo-36907: fix refcount bug in _PyStack_UnpackDict() #13381
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Fix a crash when calling a C function with a keyword dict (``f(**kwargs)``) | ||
and changing the dict ``kwargs`` while that function is running. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -544,10 +544,14 @@ _PyMethodDef_RawFastCallDict(PyMethodDef *method, PyObject *self, | |
} | ||
|
||
result = (*fastmeth) (self, stack, nargs, kwnames); | ||
if (stack != args) { | ||
if (kwnames != NULL) { | ||
Py_ssize_t i, n = nargs + PyTuple_GET_SIZE(kwnames); | ||
for (i = 0; i < n; i++) { | ||
Py_DECREF(stack[i]); | ||
} | ||
PyMem_Free((PyObject **)stack); | ||
Py_DECREF(kwnames); | ||
} | ||
Py_XDECREF(kwnames); | ||
break; | ||
} | ||
|
||
|
@@ -1334,8 +1338,11 @@ _PyStack_UnpackDict(PyObject *const *args, Py_ssize_t nargs, PyObject *kwargs, | |
return -1; | ||
} | ||
|
||
/* Copy position arguments (borrowed references) */ | ||
memcpy(stack, args, nargs * sizeof(stack[0])); | ||
/* Copy positional arguments */ | ||
for (i = 0; i < nargs; i++) { | ||
Py_INCREF(args[i]); | ||
stack[i] = args[i]; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason to not keep There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Not really, it's mostly a matter of style. I like doing the |
||
|
||
kwstack = stack + nargs; | ||
pos = i = 0; | ||
|
@@ -1344,8 +1351,8 @@ _PyStack_UnpackDict(PyObject *const *args, Py_ssize_t nargs, PyObject *kwargs, | |
called in the performance critical hot code. */ | ||
while (PyDict_Next(kwargs, &pos, &key, &value)) { | ||
Py_INCREF(key); | ||
Py_INCREF(value); | ||
PyTuple_SET_ITEM(kwnames, i, key); | ||
/* The stack contains borrowed references */ | ||
kwstack[i] = value; | ||
i++; | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.