Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 Fluke 1/4/84; site fluke.UUCP Path: utzoo!watmath!clyde!burl!ulysses!mhuxl!houxm!houxz!vax135!cornell!uw-beaver!ssc-vax!fluke!joe From: joe@fluke.UUCP (Joe Kelsey) Newsgroups: net.bugs.4bsd Subject: bug in putc macro in Message-ID: <1747@vax4.fluke.UUCP> Date: Thu, 26-Jul-84 18:07:42 EDT Article-I.D.: vax4.1747 Posted: Thu Jul 26 18:07:42 1984 Date-Received: Sat, 28-Jul-84 09:17:25 EDT Organization: John Fluke Mfg. Co., Everett, WA Lines: 36 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 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 . 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