summaryrefslogtreecommitdiffstats
path: root/Modules/_io
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2022-11-22 13:22:22 (GMT)
committerGitHub <noreply@github.com>2022-11-22 13:22:22 (GMT)
commit7e3f09cad9b783d8968aa79ff6a8ee57beb8b83e (patch)
treea8d4dda9e9128545442794595f5ebc96a5c1c7b4 /Modules/_io
parent135ec7cefbaffd516b77362ad2b2ad1025af462e (diff)
downloadcpython-7e3f09cad9b783d8968aa79ff6a8ee57beb8b83e.zip
cpython-7e3f09cad9b783d8968aa79ff6a8ee57beb8b83e.tar.gz
cpython-7e3f09cad9b783d8968aa79ff6a8ee57beb8b83e.tar.bz2
gh-99537: Use Py_SETREF() function in C code (#99656)
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 'Modules/_io')
-rw-r--r--Modules/_io/stringio.c3
-rw-r--r--Modules/_io/textio.c9
2 files changed, 4 insertions, 8 deletions
diff --git a/Modules/_io/stringio.c b/Modules/_io/stringio.c
index 5c3bf35..ae6c312 100644
--- a/Modules/_io/stringio.c
+++ b/Modules/_io/stringio.c
@@ -193,8 +193,7 @@ write_str(stringio *self, PyObject *obj)
if (self->writenl) {
PyObject *translated = PyUnicode_Replace(
decoded, &_Py_STR(newline), self->writenl, -1);
- Py_DECREF(decoded);
- decoded = translated;
+ Py_SETREF(decoded, translated);
}
if (decoded == NULL)
return -1;
diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c
index 8b5d00f..ff903e9 100644
--- a/Modules/_io/textio.c
+++ b/Modules/_io/textio.c
@@ -320,8 +320,7 @@ _PyIncrementalNewlineDecoder_decode(PyObject *myself,
out = PyUnicode_DATA(modified);
PyUnicode_WRITE(kind, out, 0, '\r');
memcpy(out + kind, PyUnicode_DATA(output), kind * output_len);
- Py_DECREF(output);
- output = modified; /* output remains ready */
+ Py_SETREF(output, modified); /* output remains ready */
self->pendingcr = 0;
output_len++;
}
@@ -336,8 +335,7 @@ _PyIncrementalNewlineDecoder_decode(PyObject *myself,
PyObject *modified = PyUnicode_Substring(output, 0, output_len -1);
if (modified == NULL)
goto error;
- Py_DECREF(output);
- output = modified;
+ Py_SETREF(output, modified);
self->pendingcr = 1;
}
}
@@ -865,8 +863,7 @@ _textiowrapper_set_decoder(textio *self, PyObject *codec_info,
self->decoder, self->readtranslate ? Py_True : Py_False, NULL);
if (incrementalDecoder == NULL)
return -1;
- Py_CLEAR(self->decoder);
- self->decoder = incrementalDecoder;
+ Py_XSETREF(self->decoder, incrementalDecoder);
}
return 0;