summaryrefslogtreecommitdiffstats
path: root/Objects/fileobject.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2013-12-17 12:40:06 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2013-12-17 12:40:06 (GMT)
commit6d562319d2ec43fb226861306b0d191aa1792489 (patch)
treefbad7d5761615b80cd99fbadfee0dfab825f998d /Objects/fileobject.c
parent2f43b635436b9c95338194feed8cc4540e6d96fd (diff)
downloadcpython-6d562319d2ec43fb226861306b0d191aa1792489.zip
cpython-6d562319d2ec43fb226861306b0d191aa1792489.tar.gz
cpython-6d562319d2ec43fb226861306b0d191aa1792489.tar.bz2
Issue #17976: Fixed potential problem with file.write() not detecting IO error
by inspecting the return value of fwrite(). Based on patches by Jaakko Moisio and test by Victor Stinner.
Diffstat (limited to 'Objects/fileobject.c')
-rw-r--r--Objects/fileobject.c6
1 files changed, 5 insertions, 1 deletions
diff --git a/Objects/fileobject.c b/Objects/fileobject.c
index 76cdf74..d3b1cf6 100644
--- a/Objects/fileobject.c
+++ b/Objects/fileobject.c
@@ -1804,6 +1804,7 @@ file_write(PyFileObject *f, PyObject *args)
const char *s;
Py_ssize_t n, n2;
PyObject *encoded = NULL;
+ int err = 0;
if (f->f_fp == NULL)
return err_closed();
@@ -1849,11 +1850,14 @@ file_write(PyFileObject *f, PyObject *args)
FILE_BEGIN_ALLOW_THREADS(f)
errno = 0;
n2 = fwrite(s, 1, n, f->f_fp);
+ if (n2 != n || ferror(f->f_fp))
+ err = errno;
FILE_END_ALLOW_THREADS(f)
Py_XDECREF(encoded);
if (f->f_binary)
PyBuffer_Release(&pbuf);
- if (n2 != n) {
+ if (err) {
+ errno = err;
PyErr_SetFromErrno(PyExc_IOError);
clearerr(f->f_fp);
return NULL;