This is specifically mentioned in K&R, it's been around a LONG time. The macro putc (et al.) are not safe to use with arguments that have side-effects, because of the nature of the implementation of such a macro. Nothing new here, nothing Linux-special either.
You could blame the authors of libc, I suppose, for violating the then-convention that all macros be in uppercase so that one knew they needed special attention. Probably what really happened is that there was a libc function that was a bottleneck, and so they macro-ized it for speed, and kept the old spelling so that they wouldn't have to go through the massive pain of changing all ten (?) programs that called it. -- Jim -----Original Message----- From: busybox-boun...@busybox.net [mailto:busybox-boun...@busybox.net] On Behalf Of Ralf Friedl Sent: Wednesday, September 15, 2010 9:06 AM To: busybox@busybox.net Subject: Re: [PATCH] Avoid side effects in putc(), which may be implementedas a macro Dan Fandrich wrote > On Wed, Sep 15, 2010 at 12:01:00PM +0200, Alessandro Rubini wrote: > >> But putchar and fwrite cannot evaluate the arguments more than once. >> So the patch is fine for putc (the part I don't quote here), but >> is wrong (i.e., unneeded) for the two hunks above. >> > > OpenWatcom for one, defines putchar() as a macro in some configurations, > so there, at at least, it's not wrong. I changed the fwrite() loop only > for consistency with the putc() loop immediately below. > The Linux man-page says: putc() is equivalent to fputc() except that it may be implemented as a macro which evaluates stream more than once. putchar(c); is equivalent to putc(c,stdout). There is no mention of c being evaluated more then once, but that may be part of the Linux implementation. Ralf Friedl _______________________________________________ busybox mailing list busybox@busybox.net http://lists.busybox.net/mailman/listinfo/busybox _______________________________________________ busybox mailing list busybox@busybox.net http://lists.busybox.net/mailman/listinfo/busybox