[cfe-dev] static analyzer false positive? (when reinterpreting buffer as char*)

Jordan Rose jordan_rose at apple.com
Tue Sep 25 13:55:45 PDT 2012


Hm. Yes, it's a false positive, and we do have outstanding bugs for not handling type-punning well. PR7913 has a similar case, along with a handful of Radars.

Meanwhile, one way to avoid the false positive is by using a union.

Sorry for the inconvenience!
Jordan


On Sep 25, 2012, at 13:41 , Sean McBride <sean at rogue-research.com> wrote:

> Hi all,
> 
> Consider:
> 
> ----------------
> #include <stdint.h>
> 
> int main (void)
> {
> 	uint16_t bar[2] = {0, 0}; // 4 bytes
> 	char* foo = (char*)bar;
> 	int x = foo[3]; // warning here
>  (void)x;
> 	
> 	 return 0;
> }
> ----------------
> 
> $ clang --version
> clang version 3.2 (trunk 164470)
> 
> $ clang -Weverything test.c 
> (no output)
> 
> $ clang --analyze test.c
> test.c:7:2: warning: Assigned value is garbage or undefined
>        int x = foo[3];
>        ^       ~~~~~~
> 1 warning generated.
> 
> I'm not a language lawyer, but isn't this a false positive?  (It is new in Xcode 4.5 vs 4.4.1 and repros in ToT).
> 
> Cheers,
> 
> -- 
> ____________________________________________________________
> Sean McBride, B. Eng                 sean at rogue-research.com
> Rogue Research                        www.rogue-research.com 
> Mac Software Developer              Montréal, Québec, Canada
> 
> 
> 
> _______________________________________________
> 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