Re: [BUG] Bad #define, nonportable C, missing {}

Andreas Schwab (schwab@suse.de)
21 Nov 2001 12:10:17 +0100


vda <vda@port.imtp.ilyichevsk.odessa.ua> writes:

|> Upon random browsing in the kernel tree I noticed in accel.c:
|> *a++ = byte_rev[*a]
|> which isn't 100% correct C AFAIK. At least Stroustrup in his C++ book
|> warns that this kind of code has to be avoided.

This is definitely causing undefined behaviour. AFAIK, gcc 3.1 (current
CVS version) can warn about such errors (-Wsequence-point).

|> ======= bad_c.diff =======
|> diff -u --recursive linux-2.4.13-old/arch/mips/lib/tinycon.c
|> linux-2.4.13-new/arch/mips/lib/tinycon.c
|> --- linux-2.4.13-old/arch/mips/lib/tinycon.c Thu Jun 26 17:33:37 1997
|> +++ linux-2.4.13-new/arch/mips/lib/tinycon.c Wed Nov 21 00:54:05 2001
|> @@ -83,14 +83,18 @@
|> register int i;
|>
|> caddr = vram_addr;
|> - for(i=0; i<size_x * (size_y-1); i++)
|> - *(caddr++) = *(caddr + size_x);
|> + for(i=0; i<size_x * (size_y-1); i++) {
|> + *caddr = *(caddr + size_x);
|> + caddr++;
|> + }

Alternatively you can write:

for(i=0; i<size_x * (size_y-1); i++, caddr++)
*caddr = *(caddr + size_x);

or even:

for(i=0; i<size_x * (size_y-1); i++, caddr++)
*caddr = caddr[size_x];

Andreas.

-- 
Andreas Schwab                                  "And now for something
Andreas.Schwab@suse.de				completely different."
SuSE Labs, SuSE GmbH, Schanzäckerstr. 10, D-90443 Nürnberg
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/