[cfe-dev] -Warray-bounds false positive?

Ted Kremenek kremenek at apple.com
Thu Aug 25 15:12:44 PDT 2011


Ah, you're right.  We should adjust the array size in the warning based on the pointer type being accessed.

On Aug 25, 2011, at 3:01 PM, Jordy Rose wrote:

> But it's been casted to char *. This is equivalent to this code:
> 
> double foo[4096];
> char *bar = (char *)foo;
> bar[sizeof(foo)-1] = '\0';
> 
> I mean, it's probably a bad idea, but it's not an out-of-bounds error.
> 
> 
> On Aug 25, 2011, at 14:59, Ted Kremenek wrote:
> 
>> No, the warning is legit.  sizeof() returns the size in bytes, not elements.  This is an array of doubles.
>> 
>> On Aug 25, 2011, at 2:22 PM, Jordy Rose wrote:
>> 
>>> 
>>> On Aug 25, 2011, at 14:00, Sean McBride wrote:
>>> 
>>>> Hi all,
>>>> 
>>>> I'm not enough of a language lawyer to be 100% sure, but:
>>>> 
>>>> -----
>>>> int main (void)
>>>> {
>>>> 	double foo[4096];
>>>> 	((char *)(foo))[(sizeof(foo)-1)] = '\0';
>>>> 	
>>>> 	return 0;
>>>> }
>>>> -----
>>>> 
>>>> test.cp:4:12: warning: array index of '32767' indexes past the end of an array (that contains 4096 elements) [-Warray-bounds]
>>>>     ((char *)(foo))[(sizeof(foo)-1)] = '\0';
>>>>               ^      ~~~~~~~~~~~~~
>>>> 
>>>> Is a false positive, right?
>>> 
>>> Yes. File a bug? :-)
>>> 
>>> 
>>> _______________________________________________
>>> cfe-dev mailing list
>>> cfe-dev at cs.uiuc.edu
>>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>> 
> 




More information about the cfe-dev mailing list