Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 31b92a5

Browse files
committed
Sanitize reference management in the utf-8 encoder
1 parent f72d4ef commit 31b92a5

1 file changed

Lines changed: 4 additions & 5 deletions

File tree

Objects/unicodeobject.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4722,6 +4722,7 @@ _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors)
47224722
int kind;
47234723
void *data;
47244724
Py_ssize_t size;
4725+
PyObject *rep = NULL;
47254726

47264727
if (!PyUnicode_Check(unicode)) {
47274728
PyErr_BadArgument();
@@ -4774,7 +4775,6 @@ _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors)
47744775
*p++ = (char)(0x80 | (ch & 0x3f));
47754776
} else if (0xD800 <= ch && ch <= 0xDFFF) {
47764777
Py_ssize_t newpos;
4777-
PyObject *rep;
47784778
Py_ssize_t repsize, k, startpos;
47794779
startpos = i-1;
47804780
rep = unicode_encode_call_errorhandler(
@@ -4822,10 +4822,8 @@ _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors)
48224822
enum PyUnicode_Kind repkind;
48234823
void *repdata;
48244824

4825-
if (PyUnicode_READY(rep) < 0) {
4826-
Py_DECREF(rep);
4825+
if (PyUnicode_READY(rep) < 0)
48274826
goto error;
4828-
}
48294827
repkind = PyUnicode_KIND(rep);
48304828
repdata = PyUnicode_DATA(rep);
48314829

@@ -4841,7 +4839,7 @@ _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors)
48414839
*p++ = (char)c;
48424840
}
48434841
}
4844-
Py_DECREF(rep);
4842+
Py_CLEAR(rep);
48454843
} else if (ch < 0x10000) {
48464844
*p++ = (char)(0xe0 | (ch >> 12));
48474845
*p++ = (char)(0x80 | ((ch >> 6) & 0x3f));
@@ -4872,6 +4870,7 @@ _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors)
48724870
Py_XDECREF(exc);
48734871
return result;
48744872
error:
4873+
Py_XDECREF(rep);
48754874
Py_XDECREF(errorHandler);
48764875
Py_XDECREF(exc);
48774876
Py_XDECREF(result);

0 commit comments

Comments
 (0)