diff options
author | Victor Stinner <vstinner@python.org> | 2022-11-22 12:39:11 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-22 12:39:11 (GMT) |
commit | 135ec7cefbaffd516b77362ad2b2ad1025af462e (patch) | |
tree | 1f92fbda32d21f0efc9f54432c32af03fe49a608 /Objects/unicodeobject.c | |
parent | 3db0a21f731cec28a89f7495a82ee2670bce75fe (diff) | |
download | cpython-135ec7cefbaffd516b77362ad2b2ad1025af462e.zip cpython-135ec7cefbaffd516b77362ad2b2ad1025af462e.tar.gz cpython-135ec7cefbaffd516b77362ad2b2ad1025af462e.tar.bz2 |
gh-99537: Use Py_SETREF() function in C code (#99657)
Fix potential race condition in code patterns:
* Replace "Py_DECREF(var); var = new;" with "Py_SETREF(var, new);"
* Replace "Py_XDECREF(var); var = new;" with "Py_XSETREF(var, new);"
* Replace "Py_CLEAR(var); var = new;" with "Py_XSETREF(var, new);"
Other changes:
* Replace "old = var; var = new; Py_DECREF(var)"
with "Py_SETREF(var, new);"
* Replace "old = var; var = new; Py_XDECREF(var)"
with "Py_XSETREF(var, new);"
* And remove the "old" variable.
Diffstat (limited to 'Objects/unicodeobject.c')
-rw-r--r-- | Objects/unicodeobject.c | 6 |
1 files changed, 2 insertions, 4 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index b1acfc7..55f029d 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -13572,8 +13572,7 @@ _PyUnicode_FormatLong(PyObject *val, int alt, int prec, int type) for (i = 0; i < numdigits; i++) *b1++ = *buf++; *b1 = '\0'; - Py_DECREF(result); - result = r1; + Py_SETREF(result, r1); buf = PyBytes_AS_STRING(result); len = numnondigits + prec; } @@ -13590,8 +13589,7 @@ _PyUnicode_FormatLong(PyObject *val, int alt, int prec, int type) || buf != PyUnicode_DATA(result)) { PyObject *unicode; unicode = _PyUnicode_FromASCII(buf, len); - Py_DECREF(result); - result = unicode; + Py_SETREF(result, unicode); } else if (len != PyUnicode_GET_LENGTH(result)) { if (PyUnicode_Resize(&result, len) < 0) |