summaryrefslogtreecommitdiff
path: root/Python/codecs.c
diff options
context:
space:
mode:
authorJeroen Demeyer <J.Demeyer@UGent.be>2019-07-05 12:57:32 +0200
committerInada Naoki <songofacandy@gmail.com>2019-07-05 19:57:32 +0900
commit6e43d07324ca799118e805751a10a7eff71d5a04 (patch)
tree1803af943221f810cd6b1e45858bb0a215658235 /Python/codecs.c
parent1da4462765b084dfa8d869b6cb5855e8f6014a11 (diff)
downloadcpython-git-6e43d07324ca799118e805751a10a7eff71d5a04.tar.gz
bpo-37483: fix reference leak in _PyCodec_Lookup (GH-14600)
Diffstat (limited to 'Python/codecs.c')
-rw-r--r--Python/codecs.c27
1 files changed, 14 insertions, 13 deletions
diff --git a/Python/codecs.c b/Python/codecs.c
index 386576256f..75b60ec6bd 100644
--- a/Python/codecs.c
+++ b/Python/codecs.c
@@ -99,40 +99,38 @@ PyObject *normalizestring(const char *string)
PyObject *_PyCodec_Lookup(const char *encoding)
{
- PyObject *result, *v;
- Py_ssize_t i, len;
-
if (encoding == NULL) {
PyErr_BadArgument();
- goto onError;
+ return NULL;
}
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
- if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
- goto onError;
+ if (interp->codec_search_path == NULL && _PyCodecRegistry_Init()) {
+ return NULL;
+ }
/* Convert the encoding to a normalized Python string: all
characters are converted to lower case, spaces and hyphens are
replaced with underscores. */
- v = normalizestring(encoding);
- if (v == NULL)
- goto onError;
+ PyObject *v = normalizestring(encoding);
+ if (v == NULL) {
+ return NULL;
+ }
PyUnicode_InternInPlace(&v);
/* First, try to lookup the name in the registry dictionary */
- result = PyDict_GetItemWithError(interp->codec_search_cache, v);
+ PyObject *result = PyDict_GetItemWithError(interp->codec_search_cache, v);
if (result != NULL) {
Py_INCREF(result);
Py_DECREF(v);
return result;
}
else if (PyErr_Occurred()) {
- Py_DECREF(v);
- return NULL;
+ goto onError;
}
/* Next, scan the search functions in order of registration */
- len = PyList_Size(interp->codec_search_path);
+ const Py_ssize_t len = PyList_Size(interp->codec_search_path);
if (len < 0)
goto onError;
if (len == 0) {
@@ -142,6 +140,7 @@ PyObject *_PyCodec_Lookup(const char *encoding)
goto onError;
}
+ Py_ssize_t i;
for (i = 0; i < len; i++) {
PyObject *func;
@@ -175,9 +174,11 @@ PyObject *_PyCodec_Lookup(const char *encoding)
Py_DECREF(result);
goto onError;
}
+ Py_DECREF(v);
return result;
onError:
+ Py_DECREF(v);
return NULL;
}