bpo-42006: Stop using PyDict_GetItem, PyDict_GetItemString and _PyDict_GetItemId. (GH-22648)

These functions are considered not safe because they suppress all internal errors
and can return wrong result.  PyDict_GetItemString and _PyDict_GetItemId can
also silence current exception in rare cases.

Remove no longer used _PyDict_GetItemId.
Add _PyDict_ContainsId and rename _PyDict_Contains into
_PyDict_Contains_KnownHash.
This commit is contained in:
Serhiy Storchaka 2020-10-26 08:43:39 +02:00 committed by GitHub
parent 96a9eed245
commit fb5db7ec58
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 259 additions and 142 deletions

View file

@ -3186,6 +3186,31 @@ dotsep_as_utf8(const char *s)
return utf8;
}
static int
dict_get_item_string(PyObject *dict, const char *key, PyObject **valueobj, const char **valuestr)
{
*valueobj = NULL;
PyObject *keyobj = PyUnicode_FromString(key);
if (keyobj == NULL) {
return -1;
}
PyObject *value = PyDict_GetItemWithError(dict, keyobj);
Py_DECREF(keyobj);
if (value == NULL) {
if (PyErr_Occurred()) {
return -1;
}
return 0;
}
value = PyUnicode_AsUTF8String(value);
if (value == NULL) {
return -1;
}
*valueobj = value;
*valuestr = PyBytes_AS_STRING(value);
return 0;
}
/* Formatted representation of a PyDecObject. */
static PyObject *
dec_format(PyObject *dec, PyObject *args)
@ -3256,23 +3281,11 @@ dec_format(PyObject *dec, PyObject *args)
"optional argument must be a dict");
goto finish;
}
if ((dot = PyDict_GetItemString(override, "decimal_point"))) {
if ((dot = PyUnicode_AsUTF8String(dot)) == NULL) {
goto finish;
}
spec.dot = PyBytes_AS_STRING(dot);
}
if ((sep = PyDict_GetItemString(override, "thousands_sep"))) {
if ((sep = PyUnicode_AsUTF8String(sep)) == NULL) {
goto finish;
}
spec.sep = PyBytes_AS_STRING(sep);
}
if ((grouping = PyDict_GetItemString(override, "grouping"))) {
if ((grouping = PyUnicode_AsUTF8String(grouping)) == NULL) {
goto finish;
}
spec.grouping = PyBytes_AS_STRING(grouping);
if (dict_get_item_string(override, "decimal_point", &dot, &spec.dot) ||
dict_get_item_string(override, "thousands_sep", &sep, &spec.sep) ||
dict_get_item_string(override, "grouping", &grouping, &spec.grouping))
{
goto finish;
}
if (mpd_validate_lconv(&spec) < 0) {
PyErr_SetString(PyExc_ValueError,

View file

@ -816,10 +816,14 @@ local_clear(localobject *self)
for(tstate = PyInterpreterState_ThreadHead(tstate->interp);
tstate;
tstate = PyThreadState_Next(tstate))
if (tstate->dict && PyDict_GetItem(tstate->dict, self->key)) {
if (PyDict_DelItem(tstate->dict, self->key)) {
if (tstate->dict) {
PyObject *v = _PyDict_Pop(tstate->dict, self->key, Py_None);
if (v == NULL) {
PyErr_Clear();
}
else {
Py_DECREF(v);
}
}
}
return 0;

View file

@ -722,17 +722,16 @@ zoneinfo__unpickle(PyTypeObject *cls, PyObject *args)
static PyObject *
load_timedelta(long seconds)
{
PyObject *rv = NULL;
PyObject *rv;
PyObject *pyoffset = PyLong_FromLong(seconds);
if (pyoffset == NULL) {
return NULL;
}
int contains = PyDict_Contains(TIMEDELTA_CACHE, pyoffset);
if (contains == -1) {
goto error;
}
if (!contains) {
rv = PyDict_GetItemWithError(TIMEDELTA_CACHE, pyoffset);
if (rv == NULL) {
if (PyErr_Occurred()) {
goto error;
}
PyObject *tmp = PyDateTimeAPI->Delta_FromDelta(
0, seconds, 0, 1, PyDateTimeAPI->DeltaType);
@ -743,12 +742,9 @@ load_timedelta(long seconds)
rv = PyDict_SetDefault(TIMEDELTA_CACHE, pyoffset, tmp);
Py_DECREF(tmp);
}
else {
rv = PyDict_GetItem(TIMEDELTA_CACHE, pyoffset);
}
Py_XINCREF(rv);
Py_DECREF(pyoffset);
Py_INCREF(rv);
return rv;
error:
Py_DECREF(pyoffset);

View file

@ -1427,10 +1427,9 @@ signal_exec(PyObject *m)
return -1;
#endif
IntHandler = PyDict_GetItemString(d, "default_int_handler");
IntHandler = PyMapping_GetItemString(d, "default_int_handler");
if (!IntHandler)
return -1;
Py_INCREF(IntHandler);
_Py_atomic_store_relaxed(&Handlers[0].tripped, 0);
for (int i = 1; i < NSIG; i++) {

View file

@ -324,7 +324,7 @@ static FlagRuntimeInfo win_runtime_flags[] = {
{14393, "TCP_FASTOPEN"}
};
static void
static int
remove_unusable_flags(PyObject *m)
{
PyObject *dict;
@ -333,7 +333,7 @@ remove_unusable_flags(PyObject *m)
dict = PyModule_GetDict(m);
if (dict == NULL) {
return;
return -1;
}
/* set to Windows 10, except BuildNumber. */
@ -359,19 +359,19 @@ remove_unusable_flags(PyObject *m)
break;
}
else {
if (PyDict_GetItemString(
dict,
win_runtime_flags[i].flag_name) != NULL)
{
if (PyDict_DelItemString(
dict,
win_runtime_flags[i].flag_name))
{
PyErr_Clear();
}
PyObject *flag_name = PyUnicode_FromString(win_runtime_flags[i].flag_name);
if (flag_name == NULL) {
return -1;
}
PyObject *v = _PyDict_Pop(dict, flag_name, Py_None);
Py_DECREF(flag_name);
if (v == NULL) {
return -1;
}
Py_DECREF(v);
}
}
return 0;
}
#endif
@ -8382,7 +8382,10 @@ PyInit__socket(void)
#ifdef MS_WINDOWS
/* remove some flags on older version Windows during run-time */
remove_unusable_flags(m);
if (remove_unusable_flags(m) < 0) {
Py_DECREF(m);
return NULL;
}
#endif
return m;