diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2011-12-16 11:29:37 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2011-12-16 11:29:37 (GMT) |
commit | ab0e9f7089c04df546af6cacbc8751247cf4020a (patch) | |
tree | 3494351a976a17a62146a3ddd365fe2b5936fca4 /Modules | |
parent | e2b2bf55b30299e310b246b5b4b6f5a5f66e75b2 (diff) | |
parent | c345ce1a69b3c4a46d87ef56d859bc70abfc74b4 (diff) | |
download | cpython-ab0e9f7089c04df546af6cacbc8751247cf4020a.zip cpython-ab0e9f7089c04df546af6cacbc8751247cf4020a.tar.gz cpython-ab0e9f7089c04df546af6cacbc8751247cf4020a.tar.bz2 |
Issue #10350: Read and save errno before calling a function which might overwrite it.
Original patch by Hallvard B Furuseth.
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_io/fileio.c | 14 | ||||
-rw-r--r-- | Modules/_multiprocessing/semaphore.c | 4 | ||||
-rw-r--r-- | Modules/main.c | 3 | ||||
-rw-r--r-- | Modules/readline.c | 13 | ||||
-rw-r--r-- | Modules/timemodule.c | 4 |
5 files changed, 27 insertions, 11 deletions
diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c index 7364523..3829e0b 100644 --- a/Modules/_io/fileio.c +++ b/Modules/_io/fileio.c @@ -530,6 +530,7 @@ fileio_readinto(fileio *self, PyObject *args) { Py_buffer pbuf; Py_ssize_t n, len; + int err; if (self->fd < 0) return err_closed(); @@ -553,10 +554,12 @@ fileio_readinto(fileio *self, PyObject *args) Py_END_ALLOW_THREADS } else n = -1; + err = errno; PyBuffer_Release(&pbuf); if (n < 0) { - if (errno == EAGAIN) + if (err == EAGAIN) Py_RETURN_NONE; + errno = err; PyErr_SetFromErrno(PyExc_IOError); return NULL; } @@ -726,9 +729,11 @@ fileio_read(fileio *self, PyObject *args) n = -1; if (n < 0) { + int err = errno; Py_DECREF(bytes); - if (errno == EAGAIN) + if (err == EAGAIN) Py_RETURN_NONE; + errno = err; PyErr_SetFromErrno(PyExc_IOError); return NULL; } @@ -748,6 +753,7 @@ fileio_write(fileio *self, PyObject *args) { Py_buffer pbuf; Py_ssize_t n, len; + int err; if (self->fd < 0) return err_closed(); @@ -778,12 +784,14 @@ fileio_write(fileio *self, PyObject *args) Py_END_ALLOW_THREADS } else n = -1; + err = errno; PyBuffer_Release(&pbuf); if (n < 0) { - if (errno == EAGAIN) + if (err == EAGAIN) Py_RETURN_NONE; + errno = err; PyErr_SetFromErrno(PyExc_IOError); return NULL; } diff --git a/Modules/_multiprocessing/semaphore.c b/Modules/_multiprocessing/semaphore.c index e48936e..0c9b04c 100644 --- a/Modules/_multiprocessing/semaphore.c +++ b/Modules/_multiprocessing/semaphore.c @@ -255,7 +255,7 @@ sem_timedwait_save(sem_t *sem, struct timespec *deadline, PyThreadState *_save) static PyObject * semlock_acquire(SemLockObject *self, PyObject *args, PyObject *kwds) { - int blocking = 1, res; + int blocking = 1, res, err = 0; double timeout; PyObject *timeout_obj = Py_None; struct timespec deadline = {0}; @@ -301,11 +301,13 @@ semlock_acquire(SemLockObject *self, PyObject *args, PyObject *kwds) else res = sem_timedwait(self->handle, &deadline); Py_END_ALLOW_THREADS + err = errno; if (res == MP_EXCEPTION_HAS_BEEN_SET) break; } while (res < 0 && errno == EINTR && !PyErr_CheckSignals()); if (res < 0) { + errno = err; if (errno == EAGAIN || errno == ETIMEDOUT) Py_RETURN_FALSE; else if (errno == EINTR) diff --git a/Modules/main.c b/Modules/main.c index 747c12f..d4c3314 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -655,13 +655,14 @@ Py_Main(int argc, wchar_t **argv) if (fp == NULL) { char *cfilename_buffer; const char *cfilename; + int err = errno; cfilename_buffer = _Py_wchar2char(filename, NULL); if (cfilename_buffer != NULL) cfilename = cfilename_buffer; else cfilename = "<unprintable file name>"; fprintf(stderr, "%ls: can't open file '%s': [Errno %d] %s\n", - argv[0], cfilename, errno, strerror(errno)); + argv[0], cfilename, err, strerror(err)); if (cfilename_buffer) PyMem_Free(cfilename_buffer); return 2; diff --git a/Modules/readline.c b/Modules/readline.c index a5e48ab..a710652 100644 --- a/Modules/readline.c +++ b/Modules/readline.c @@ -154,6 +154,7 @@ write_history_file(PyObject *self, PyObject *args) { PyObject *filename_obj = Py_None, *filename_bytes; char *filename; + int err; if (!PyArg_ParseTuple(args, "|O:write_history_file", &filename_obj)) return NULL; if (filename_obj != Py_None) { @@ -164,10 +165,11 @@ write_history_file(PyObject *self, PyObject *args) filename_bytes = NULL; filename = NULL; } - errno = write_history(filename); - if (!errno && _history_length >= 0) + errno = err = write_history(filename); + if (!err && _history_length >= 0) history_truncate_file(filename, _history_length); Py_XDECREF(filename_bytes); + errno = err; if (errno) return PyErr_SetFromErrno(PyExc_IOError); Py_RETURN_NONE; @@ -969,7 +971,7 @@ readline_until_enter_or_signal(char *prompt, int *signal) completed_input_string = not_done_reading; while (completed_input_string == not_done_reading) { - int has_input = 0; + int has_input = 0, err = 0; while (!has_input) { struct timeval timeout = {0, 100000}; /* 0.1 seconds */ @@ -983,13 +985,14 @@ readline_until_enter_or_signal(char *prompt, int *signal) /* select resets selectset if no input was available */ has_input = select(fileno(rl_instream) + 1, &selectset, NULL, NULL, timeoutp); + err = errno; if(PyOS_InputHook) PyOS_InputHook(); } - if(has_input > 0) { + if (has_input > 0) { rl_callback_read_char(); } - else if (errno == EINTR) { + else if (err == EINTR) { int s; #ifdef WITH_THREAD PyEval_RestoreThread(_PyOS_ReadlineTState); diff --git a/Modules/timemodule.c b/Modules/timemodule.c index dff4641..001b311 100644 --- a/Modules/timemodule.c +++ b/Modules/timemodule.c @@ -515,12 +515,14 @@ time_strftime(PyObject *self, PyObject *args) * will be ahead of time... */ for (i = 1024; ; i += i) { + int err; outbuf = (time_char *)PyMem_Malloc(i*sizeof(time_char)); if (outbuf == NULL) { PyErr_NoMemory(); break; } buflen = format_time(outbuf, i, fmt, &buf); + err = errno; if (buflen > 0 || i >= 256 * fmtlen) { /* If the buffer is 256 times as long as the format, it's probably not failing for lack of room! @@ -538,7 +540,7 @@ time_strftime(PyObject *self, PyObject *args) PyMem_Free(outbuf); #if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__) /* VisualStudio .NET 2005 does this properly */ - if (buflen == 0 && errno == EINVAL) { + if (buflen == 0 && err == EINVAL) { PyErr_SetString(PyExc_ValueError, "Invalid format string"); break; } |