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

Skip to content

Commit 080d5b3

Browse files
committed
mywrite(): The test for trouble in PyOS_vsnprintf was wrong on both
ends. Also, when there is trouble, ensure the buffer has a traiing 0 byte.
1 parent 64be0b4 commit 080d5b3

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

Python/sysmodule.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,13 +1023,20 @@ mywrite(char *name, FILE *fp, const char *format, va_list va)
10231023
vfprintf(fp, format, va);
10241024
else {
10251025
char buffer[1001];
1026-
int written = PyOS_vsnprintf(buffer, sizeof(buffer),
1027-
format, va);
1026+
const int written = PyOS_vsnprintf(buffer, sizeof(buffer),
1027+
format, va);
1028+
const int trouble = written < 0 || written >= sizeof(buffer);
1029+
if (trouble) {
1030+
/* Ensure there's a trailing null byte -- MS
1031+
vsnprintf fills the buffer to the very end
1032+
if it's not big enough. */
1033+
buffer[sizeof(buffer) - 1] = '\0';
1034+
}
10281035
if (PyFile_WriteString(buffer, file) != 0) {
10291036
PyErr_Clear();
10301037
fputs(buffer, fp);
10311038
}
1032-
if (written == -1 || written > sizeof(buffer)) {
1039+
if (trouble) {
10331040
const char *truncated = "... truncated";
10341041
if (PyFile_WriteString(truncated, file) != 0) {
10351042
PyErr_Clear();

0 commit comments

Comments
 (0)