[cfe-commits] r105097 - in /cfe/trunk: lib/Checker/RegionStore.cpp test/Analysis/PR7218.c

Zhongxing Xu xuzhongxing at gmail.com
Fri May 28 23:23:24 PDT 2010


Author: zhongxingxu
Date: Sat May 29 01:23:24 2010
New Revision: 105097

URL: http://llvm.org/viewvc/llvm-project?rev=105097&view=rev
Log:
Fix PR7218. Patch by Jordy Rose.

Added:
    cfe/trunk/test/Analysis/PR7218.c
Modified:
    cfe/trunk/lib/Checker/RegionStore.cpp

Modified: cfe/trunk/lib/Checker/RegionStore.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Checker/RegionStore.cpp?rev=105097&r1=105096&r2=105097&view=diff
==============================================================================
--- cfe/trunk/lib/Checker/RegionStore.cpp (original)
+++ cfe/trunk/lib/Checker/RegionStore.cpp Sat May 29 01:23:24 2010
@@ -213,6 +213,11 @@
 
   RegionStoreSubRegionMap *getRegionStoreSubRegionMap(Store store);
 
+  /// canHaveDirectBinding - Disallow direct bindings for certain types,
+  ///  like arrays. This lets us distinguish between x and x[0], which was
+  ///  causing PR7218 "Assigning to buf[0] makes buf[1] valid".
+  bool canHaveDirectBinding (const MemRegion *R);
+
   Optional<SVal> getBinding(RegionBindings B, const MemRegion *R);
   Optional<SVal> getDirectBinding(RegionBindings B, const MemRegion *R);
   /// getDefaultBinding - Returns an SVal* representing an optional default
@@ -944,11 +949,20 @@
 //===----------------------------------------------------------------------===//
 // Loading values from regions.
 //===----------------------------------------------------------------------===//
+bool RegionStoreManager::canHaveDirectBinding (const MemRegion *R) {
+  // Arrays can't have direct binding -- must bind to elements
+  if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))    
+    if (TR->getValueType(getContext())->isArrayType())   
+      return false;
+  
+  return true;
+}
 
 Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B,
-                                                 const MemRegion *R) {
-  if (const SVal *V = Lookup(B, R, BindingKey::Direct))
-    return *V;
+                                                    const MemRegion *R) {
+  if (canHaveDirectBinding(R))
+    if (const SVal *V = Lookup(B, R, BindingKey::Direct))
+      return *V;
 
   return Optional<SVal>();
 }

Added: cfe/trunk/test/Analysis/PR7218.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/PR7218.c?rev=105097&view=auto
==============================================================================
--- cfe/trunk/test/Analysis/PR7218.c (added)
+++ cfe/trunk/test/Analysis/PR7218.c Sat May 29 01:23:24 2010
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 -analyze -analyzer-check-objc-mem -analyzer-store region -verify %s
+char PR7218(char a) {
+    char buf[2];
+    buf[0] = a;
+    return buf[1]; // expected-warning {{Undefined or garbage value returned to caller}}
+}





More information about the cfe-commits mailing list