Mercurial > cpython-lru_cache-weakref
comparison Python/sysmodule.c @ 21127:dd28a4ecb37f legacy-trunk
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.
| author | Tim Peters <tim.peters@gmail.com> |
|---|---|
| date | Sun, 02 Dec 2001 08:29:16 +0000 |
| parents | 47f6533674fd |
| children | 87fa6e9bd77e |
comparison
equal
deleted
inserted
replaced
| 21126:6b6a9cae256e | 21127:dd28a4ecb37f |
|---|---|
| 1021 file = PySys_GetObject(name); | 1021 file = PySys_GetObject(name); |
| 1022 if (file == NULL || PyFile_AsFile(file) == fp) | 1022 if (file == NULL || PyFile_AsFile(file) == fp) |
| 1023 vfprintf(fp, format, va); | 1023 vfprintf(fp, format, va); |
| 1024 else { | 1024 else { |
| 1025 char buffer[1001]; | 1025 char buffer[1001]; |
| 1026 int written = PyOS_vsnprintf(buffer, sizeof(buffer), | 1026 const int written = PyOS_vsnprintf(buffer, sizeof(buffer), |
| 1027 format, va); | 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 } | |
| 1028 if (PyFile_WriteString(buffer, file) != 0) { | 1035 if (PyFile_WriteString(buffer, file) != 0) { |
| 1029 PyErr_Clear(); | 1036 PyErr_Clear(); |
| 1030 fputs(buffer, fp); | 1037 fputs(buffer, fp); |
| 1031 } | 1038 } |
| 1032 if (written == -1 || written > sizeof(buffer)) { | 1039 if (trouble) { |
| 1033 const char *truncated = "... truncated"; | 1040 const char *truncated = "... truncated"; |
| 1034 if (PyFile_WriteString(truncated, file) != 0) { | 1041 if (PyFile_WriteString(truncated, file) != 0) { |
| 1035 PyErr_Clear(); | 1042 PyErr_Clear(); |
| 1036 fputs(truncated, fp); | 1043 fputs(truncated, fp); |
| 1037 } | 1044 } |
