Skip to content

Commit ea8a9c8

Browse files
jdemeyerlisroach
authored andcommitted
bpo-37483: fix reference leak in _PyCodec_Lookup (pythonGH-14600)
1 parent 1133999 commit ea8a9c8

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

Python/codecs.c

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,40 +99,38 @@ PyObject *normalizestring(const char *string)
9999

100100
PyObject *_PyCodec_Lookup(const char *encoding)
101101
{
102-
PyObject *result, *v;
103-
Py_ssize_t i, len;
104-
105102
if (encoding == NULL) {
106103
PyErr_BadArgument();
107-
goto onError;
104+
return NULL;
108105
}
109106

110107
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
111-
if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
112-
goto onError;
108+
if (interp->codec_search_path == NULL && _PyCodecRegistry_Init()) {
109+
return NULL;
110+
}
113111

114112
/* Convert the encoding to a normalized Python string: all
115113
characters are converted to lower case, spaces and hyphens are
116114
replaced with underscores. */
117-
v = normalizestring(encoding);
118-
if (v == NULL)
119-
goto onError;
115+
PyObject *v = normalizestring(encoding);
116+
if (v == NULL) {
117+
return NULL;
118+
}
120119
PyUnicode_InternInPlace(&v);
121120

122121
/* First, try to lookup the name in the registry dictionary */
123-
result = PyDict_GetItemWithError(interp->codec_search_cache, v);
122+
PyObject *result = PyDict_GetItemWithError(interp->codec_search_cache, v);
124123
if (result != NULL) {
125124
Py_INCREF(result);
126125
Py_DECREF(v);
127126
return result;
128127
}
129128
else if (PyErr_Occurred()) {
130-
Py_DECREF(v);
131-
return NULL;
129+
goto onError;
132130
}
133131

134132
/* Next, scan the search functions in order of registration */
135-
len = PyList_Size(interp->codec_search_path);
133+
const Py_ssize_t len = PyList_Size(interp->codec_search_path);
136134
if (len < 0)
137135
goto onError;
138136
if (len == 0) {
@@ -142,6 +140,7 @@ PyObject *_PyCodec_Lookup(const char *encoding)
142140
goto onError;
143141
}
144142

143+
Py_ssize_t i;
145144
for (i = 0; i < len; i++) {
146145
PyObject *func;
147146

@@ -175,9 +174,11 @@ PyObject *_PyCodec_Lookup(const char *encoding)
175174
Py_DECREF(result);
176175
goto onError;
177176
}
177+
Py_DECREF(v);
178178
return result;
179179

180180
onError:
181+
Py_DECREF(v);
181182
return NULL;
182183
}
183184

0 commit comments

Comments
 (0)