summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTed Kremenek <kremenek@apple.com>2009-11-11 06:43:42 +0000
committerTed Kremenek <kremenek@apple.com>2009-11-11 06:43:42 +0000
commit04552cbef008efe6f5d660da7a97592962f97a2e (patch)
tree614f200692dfe534f4ba62656a477e72697d459c
parent55d59bf785289cab52591344a284489351b7646a (diff)
downloadbcm5719-llvm-04552cbef008efe6f5d660da7a97592962f97a2e.tar.gz
bcm5719-llvm-04552cbef008efe6f5d660da7a97592962f97a2e.zip
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.
llvm-svn: 86813
-rw-r--r--clang/lib/Analysis/CastToStructChecker.cpp2
-rw-r--r--clang/test/Analysis/misc-ps.m25
2 files changed, 24 insertions, 3 deletions
diff --git a/clang/lib/Analysis/CastToStructChecker.cpp b/clang/lib/Analysis/CastToStructChecker.cpp
index 5d0110e4eb2..bda8ff47bf8 100644
--- a/clang/lib/Analysis/CastToStructChecker.cpp
+++ b/clang/lib/Analysis/CastToStructChecker.cpp
@@ -50,7 +50,7 @@ void CastToStructChecker::PreVisitCastExpr(CheckerContext &C,
QualType OrigPointeeTy = OrigPTy->getPointeeType();
QualType ToPointeeTy = ToPTy->getPointeeType();
- if (!ToPointeeTy->isRecordType())
+ if (!ToPointeeTy->isStructureType())
return;
// We allow cast from void*.
diff --git a/clang/test/Analysis/misc-ps.m b/clang/test/Analysis/misc-ps.m
index d9bba7d1020..b3b4e9ab7ab 100644
--- a/clang/test/Analysis/misc-ps.m
+++ b/clang/test/Analysis/misc-ps.m
@@ -692,7 +692,6 @@ void *rdar7152418_bar();
// 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 @@ unsigned test_symbol_fold_3(void) {
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
+}
OpenPOWER on IntegriCloud