[cfe-commits] r86813 - in /cfe/trunk: lib/Analysis/CastToStructChecker.cpp test/Analysis/misc-ps.m

Ted Kremenek kremenek at apple.com
Tue Nov 10 22:43:42 PST 2009


Author: kremenek
Date: Wed Nov 11 00:43:42 2009
New Revision: 86813

URL: http://llvm.org/viewvc/llvm-project?rev=86813&view=rev
Log:
CastToStructChecker: use 'isStructureType()' instead of 'isRecordType()' to determine if a pointer is casted to a struct pointer.  This fixes an observed false positive when a value is casted to a union.

Modified:
    cfe/trunk/lib/Analysis/CastToStructChecker.cpp
    cfe/trunk/test/Analysis/misc-ps.m

Modified: cfe/trunk/lib/Analysis/CastToStructChecker.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/CastToStructChecker.cpp?rev=86813&r1=86812&r2=86813&view=diff

==============================================================================
--- cfe/trunk/lib/Analysis/CastToStructChecker.cpp (original)
+++ cfe/trunk/lib/Analysis/CastToStructChecker.cpp Wed Nov 11 00:43:42 2009
@@ -50,7 +50,7 @@
   QualType OrigPointeeTy = OrigPTy->getPointeeType();
   QualType ToPointeeTy = ToPTy->getPointeeType();
 
-  if (!ToPointeeTy->isRecordType())
+  if (!ToPointeeTy->isStructureType())
     return;
 
   // We allow cast from void*.

Modified: cfe/trunk/test/Analysis/misc-ps.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/misc-ps.m?rev=86813&r1=86812&r2=86813&view=diff

==============================================================================
--- cfe/trunk/test/Analysis/misc-ps.m (original)
+++ cfe/trunk/test/Analysis/misc-ps.m Wed Nov 11 00:43:42 2009
@@ -692,7 +692,6 @@
 // conversions of the symbol as necessary.
 //===----------------------------------------------------------------------===//
 
-
 // Previously this would crash once we started eagerly evaluating symbols whose 
 // values were constrained to a single value.
 void test_symbol_fold_1(signed char x) {
@@ -722,5 +721,27 @@
   if (x == 54)
     return (x << 8) | 0x5;
   return 0;
-}  
+} 
+
+//===----------------------------------------------------------------------===//
+// Tests for the warning of casting a non-struct type to a struct type
+//===----------------------------------------------------------------------===//
+
+typedef struct {unsigned int v;} NSSwappedFloat;
+
+NSSwappedFloat test_cast_nonstruct_to_struct(float x) {
+  struct hodor {
+    float number;
+    NSSwappedFloat sf;
+  };
+  return ((struct hodor *)&x)->sf; // expected-warning{{Casting a non-structure type to a structure type and accessing a field can lead to memory access errors or data corruption}}
+}
+
+NSSwappedFloat test_cast_nonstruct_to_union(float x) {
+  union bran {
+    float number;
+    NSSwappedFloat sf;
+  };
+  return ((union bran *)&x)->sf; // no-warning
+}
 





More information about the cfe-commits mailing list