bug-m4
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: HEAD: inclusion order wrong for input.c


From: Matthew Woehlke
Subject: Re: HEAD: inclusion order wrong for input.c
Date: Mon, 02 Apr 2007 15:54:15 -0500
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.10) Gecko/20070221 Thunderbird/1.5.0.10 Mnenhy/0.7.4.0

Eric Blake wrote:
Matthew Woehlke writes:
(WCTS) ...an explicit typecast to int doesn't solve this? Seems to work here:

$ gcc -Wall -Wextra -o aci arrayci.c
arrayci.c: In function `main':
arrayci.c:7: warning: array subscript has type `char'
$ gcc -Wall -Wextra -DCAST='(int)(unsigned)' -o aci arrayci.c
$ cat arrayci.c
#ifndef CAST
#define CAST
#endif
int main() {
         char c = 'q';
         char foo[256] = { 0 };
         return foo[CAST c];
}

That's buggy.  char can be signed, so promoting char to int,
whether or not by an explicit cast to int, means that you may
be derefencing negative indices into the array.  The correct
fix is to cast to unsigned char (or, in the m4 source code, use
the to_uchar inline function, which has better type safety).

Clearly you didn't read my command line very closely. It produces:

return foo[(int)(unsigned)c];

...so (in my own defense) I /was/ taking that into account :-). Good point about to_uchar, though, that does seem the right thing to do.

--
Matthew
Vs lbh pna ernq guvf jvgubhg fbsgjner, lbh ner n FREVBHF areq! -- Nqncgrq sebz Znggurj Jva (ivz-qri znvyvat yvfg)





reply via email to

[Prev in Thread] Current Thread [Next in Thread]