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

Skip to content

Commit 8442af3

Browse files
committed
Patches for mywrite() by Marc Lemburg: save/restore the error state
reliably; check return value of vsprintf().
1 parent 41f0a98 commit 8442af3

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

Python/sysmodule.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -594,8 +594,7 @@ PySys_SetArgv(argc, argv)
594594
595595
The first function writes to sys.stdout; the second to sys.stderr. When
596596
there is a problem, they write to the real (C level) stdout or stderr;
597-
no exceptions are raised (but a pending exception may be cleared when a
598-
new exception is caught).
597+
no exceptions are raised.
599598
600599
Both take a printf-style format string as their first argument followed
601600
by a variable length argument list determined by the format string.
@@ -619,18 +618,22 @@ mywrite(name, fp, format, va)
619618
va_list va;
620619
{
621620
PyObject *file;
621+
PyObject *error_type, *error_value, *error_traceback;
622622

623+
PyErr_Fetch(&error_type, &error_value, &error_traceback);
623624
file = PySys_GetObject(name);
624625
if (file == NULL || PyFile_AsFile(file) == fp)
625626
vfprintf(fp, format, va);
626627
else {
627628
char buffer[1001];
628-
vsprintf(buffer, format, va);
629+
if (vsprintf(buffer, format, va) >= sizeof(buffer))
630+
Py_FatalError("PySys_WriteStdout/err: buffer overrun");
629631
if (PyFile_WriteString(buffer, file) != 0) {
630632
PyErr_Clear();
631633
fputs(buffer, fp);
632634
}
633635
}
636+
PyErr_Restore(error_type, error_value, error_traceback);
634637
}
635638

636639
void

0 commit comments

Comments
 (0)