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.
This commit is contained in:
Tim Peters 2001-12-02 08:29:16 +00:00
parent 64be0b4aa5
commit 080d5b3f0b
1 changed files with 10 additions and 3 deletions

View File

@ -1023,13 +1023,20 @@ mywrite(char *name, FILE *fp, const char *format, va_list va)
vfprintf(fp, format, va); vfprintf(fp, format, va);
else { else {
char buffer[1001]; char buffer[1001];
int written = PyOS_vsnprintf(buffer, sizeof(buffer), const int written = PyOS_vsnprintf(buffer, sizeof(buffer),
format, va); format, va);
const int trouble = written < 0 || written >= sizeof(buffer);
if (trouble) {
/* Ensure there's a trailing null byte -- MS
vsnprintf fills the buffer to the very end
if it's not big enough. */
buffer[sizeof(buffer) - 1] = '\0';
}
if (PyFile_WriteString(buffer, file) != 0) { if (PyFile_WriteString(buffer, file) != 0) {
PyErr_Clear(); PyErr_Clear();
fputs(buffer, fp); fputs(buffer, fp);
} }
if (written == -1 || written > sizeof(buffer)) { if (trouble) {
const char *truncated = "... truncated"; const char *truncated = "... truncated";
if (PyFile_WriteString(truncated, file) != 0) { if (PyFile_WriteString(truncated, file) != 0) {
PyErr_Clear(); PyErr_Clear();