Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

Warn about illegal type-punning across function boundaries #17860

Open Quuxplusone opened 11 years ago

Quuxplusone commented 11 years ago
Bugzilla Link PR17861
Status NEW
Importance P normal
Reported by Doug Richardson (dougie.richardson@gmail.com)
Reported on 2013-11-08 22:11:54 -0800
Last modified on 2013-11-11 19:11:41 -0800
Version 3.3
Hardware PC All
CC jrose@belkadan.com, llvm-bugs@lists.llvm.org
Fixed by commit(s)
Attachments MyAnalyzerTest.zip (26683 bytes, application/zip)
Blocks
Blocked by
See also
Created attachment 11504
Xcode project demonstrating problem

I have two structs, both packed and 12 bytes long. One struct uses bitfield and
the other does not. If I take a pointer to the first struct, and then pass it
to a function that in turn casts it to a pointer to the second struct and
initializes the values, the static analyzer reports the values are
uninitialized.

The attached Xcode project demonstrates the problem.

I was able to workaround the problem by creating a union that contains the two
structs and passing that around instead.
Quuxplusone commented 11 years ago

Attached MyAnalyzerTest.zip (26683 bytes, application/zip): Xcode project demonstrating problem

Quuxplusone commented 10 years ago
Yeah, the static analyzer doesn't handle reinterpret_casts very well, and
bitfields don't help either. For what it's worth, though, what you were doing
before is not actually legal according to the C standard (C11 6.5p7):

"An object shall have its stored value accessed only by an lvalue expression
that has one of the following types:

- a type compatible with the effective type of the object,
- a qualified version of a type compatible with the effective type of the
object,
- a type that is the signed or unsigned type corresponding to the effective
type of the object,
- a type that is the signed or unsigned type corresponding to a qualified
version of the effective type of the object,
- an aggregate or union type that includes one of the aforementioned types
among its members (including, recursively, a member of a subaggregate or
contained union), or
- a character type."

So you actually do need the union to do this properly. Or you could use memcpy.
Quuxplusone commented 10 years ago

Thanks for the fast response and for including the information from the C standard... I wasn't familiar with that. Feel free to close this as Not to be Fixed. That said, it took me a while to figure out how to correct the issue using the union, so if the analyzer could be magically enhanced to explain why the value is uninitialized it'd probably help others.

Quuxplusone commented 10 years ago

Repurposing the bug for this new warning.