joe@fluke.UUCP (Joe Kelsey) (07/27/84)
Index: /usr/include/stdio.h ALL UNIX SYSTEMS!!!
Description:
Remember all of the flack recently about sign extension and
getc? Whether or not you can compare (c = getc(stdin)) == EOF?
Well, how about the same discussion related to putc? Yes, there
are programmers who check the value returned by putc. The main
problem is to detect errors indicated by _flsbuf, as in quota
exceeded, or other disastrous errors. However, putc(0xff, stdout)
gets sign extended (on DEC hardware, anyway) so that you can't
tell it from EOF (-1).
Repeat-By:
#include <stdio.h>
main()
{
int x;
if ((x = putc(0xff, stdout)) == EOF) {
printf (stderr, "Error!\n");
} else {
printf (stderr, "OK!\n);
}
}
Fix:
Add a mask to the putc macro in <stdio.h>. Note that I have verified
this bug in VAX-11 C also!
old:
#define putc(x,p) (--(p)->_cnt>=0? ((int)(*(p)->_ptr++=(unsigned)(x))):_flsbuf((unsigned)(x),p))
new:
#define putc(x,p) (--(p)->_cnt>=0? ((int)(*(p)->_ptr++=(unsigned)(x)))&0xff:_flsbuf((unsigned)(x),p))
This change merely makes putc operate the same as getc when filling
the buffer before actually attempting to output the buffer.
/Joe