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

Ted Kremenek kremenek at apple.com
Tue Sep 25 13:48:34 PDT 2012


Yes it is a false positive.  The analyzer doesn't handle well dealing with re-interpretation casts of arrays like this.  While we don't have a good way to reason about this now, it should be straightforward to suppress the false positive.

Please file a PR.

On Sep 25, 2012, at 1:41 PM, 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