Bug in putc

Barry Shein bzs at bu-cs.UUCP
Fri Mar 28 13:46:45 AEST 1986


>There is a bug in the putc macro, in stdio.h.
>Putc returns an int which contains the char you have just "put".
>However, (at least on both a Pixel and a Sun), the result is sign extended;
>if the 8th bit of the char is set, the return value from putc does not equal
>the character you put!  To fix, replace the putc macro with the following:
>
>#define putc(x,p) ((int) ((--(p)->_cnt>=0? ((unsigned char)(*(p)->_ptr++=(x))):(unsigned char)_flsbuf((x),p))))
>
>Steve Harris

WRONG

Putc (actually _flsbuf()) can return EOF which on my SUN* is defined as
(-1). Doing the above would cause the return value to become 0xff and
would not test negative. I believe (uh oh) that what you want here
is to change the (unsigned char)s to (unsigned int)s, your timing is
off as to when you want it to happen. I tried it on a SUN and it properly
returns 0xff for 0xff and -1 for -1. Someone here suggested that it was
too bad that EOF was not defined as some negative value like 0xffff0000
(ie. < -255) which is a nice thought but it's too late I suppose, it would
have thus been even more out of band (not easily confused with sign-extended
0xff.)

	-Barry Shein, Boston University



More information about the Net.bugs mailing list