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

Skip to content

Commit 5d3d134

Browse files
committed
Use PyOS_vsnprintf() and check its return value.
If it returns -1 (which indicates overflow on old Linux platforms and perhaps on Windows) or size greater than buffer, write a message indicating that the previous message was truncated.
1 parent 4b4ab20 commit 5d3d134

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

Python/sysmodule.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,12 +1023,19 @@ mywrite(char *name, FILE *fp, const char *format, va_list va)
10231023
vfprintf(fp, format, va);
10241024
else {
10251025
char buffer[1001];
1026-
if (vsprintf(buffer, format, va) >= sizeof(buffer))
1027-
Py_FatalError("PySys_WriteStdout/err: buffer overrun");
1026+
int written = PyOS_vsnprintf(buffer, sizeof(buffer),
1027+
format, va);
10281028
if (PyFile_WriteString(buffer, file) != 0) {
10291029
PyErr_Clear();
10301030
fputs(buffer, fp);
10311031
}
1032+
if (written == -1 || written > sizeof(buffer)) {
1033+
const char *truncated = "... truncated";
1034+
if (PyFile_WriteString(truncated, file) != 0) {
1035+
PyErr_Clear();
1036+
fputs(truncated, fp);
1037+
}
1038+
}
10321039
}
10331040
PyErr_Restore(error_type, error_value, error_traceback);
10341041
}

0 commit comments

Comments
 (0)