Browse Source

Fix a pointer bug in setvbuf reported by Bart Visscher <magick@Linux-Fan.com>.

Manuel Novoa III 23 years ago
parent
commit
48c12e04ca
1 changed files with 6 additions and 2 deletions
  1. 6 2
      libc/stdio/stdio.c

+ 6 - 2
libc/stdio/stdio.c

@@ -2408,9 +2408,13 @@ int setvbuf(register FILE * __restrict stream, register char * __restrict buf,
 	allocated_buf_flag = 0;
 	if ((!buf) && (size != (stream->bufend - stream->bufstart))) {
 		/* No buffer supplied and requested size different from current. */
-		allocated_buf_flag = __FLAG_FREEBUF;
 		/* If size == 0, create a (hopefully) bogus non-null pointer... */
-		if (!(buf = ((size > 0) ? malloc(size) : ((char *)NULL) + 1))) {
+		if (!(buf = ((size > 0)
+					 ? ((allocated_buf_flag = __FLAG_FREEBUF), malloc(size))
+					 : ((char *)NULL) + 1))
+			) {
+			/* TODO -- should we really keep current buffer if it was passed
+			 * to us earlier by the app? */
 			goto DONE;		/* Keep current buffer. */
 		}
 	}