]> granicus.if.org Git - clang/commitdiff
[analyzer] ObjCContainersASTChecker: minor cleanup and an extra test case.
authorJordan Rose <jordan_rose@apple.com>
Tue, 16 Oct 2012 00:47:25 +0000 (00:47 +0000)
committerJordan Rose <jordan_rose@apple.com>
Tue, 16 Oct 2012 00:47:25 +0000 (00:47 +0000)
Follow-up to r165838, which fixed a potential crash.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@166002 91177308-0d34-0410-b5e6-96231b3b80d8

lib/StaticAnalyzer/Checkers/ObjCContainersASTChecker.cpp
test/Analysis/CFContainers-invalid.c [new file with mode: 0644]

index 9c0c3cd3b6fd41ab23e7d4250dca81991981eee3..63a84805e73e02813bd96266f4a54f7e38439965 100644 (file)
@@ -31,8 +31,6 @@ class WalkAST : public StmtVisitor<WalkAST> {
   ASTContext &ASTC;
   uint64_t PtrWidth;
 
-  static const unsigned InvalidArgIndex = UINT_MAX;
-
   /// Check if the type has pointer size (very conservative).
   inline bool isPointerSize(const Type *T) {
     if (!T)
@@ -102,7 +100,7 @@ void WalkAST::VisitCallExpr(CallExpr *CE) {
     return;
 
   const Expr *Arg = 0;
-  unsigned ArgNum = InvalidArgIndex;
+  unsigned ArgNum;
 
   if (Name.equals("CFArrayCreate") || Name.equals("CFSetCreate")) {
     if (CE->getNumArgs() != 4)
@@ -111,9 +109,7 @@ void WalkAST::VisitCallExpr(CallExpr *CE) {
     Arg = CE->getArg(ArgNum)->IgnoreParenCasts();
     if (hasPointerToPointerSizedType(Arg))
         return;
-  }
-
-  if (Arg == 0 && Name.equals("CFDictionaryCreate")) {
+  } else if (Name.equals("CFDictionaryCreate")) {
     if (CE->getNumArgs() != 6)
       return;
     // Check first argument.
@@ -129,13 +125,11 @@ void WalkAST::VisitCallExpr(CallExpr *CE) {
     }
   }
 
-  if (ArgNum != InvalidArgIndex) {
+  if (Arg) {
     assert(ArgNum == 1 || ArgNum == 2);
-    assert(Arg);
 
-    SmallString<256> BufName;
+    SmallString<64> BufName;
     llvm::raw_svector_ostream OsName(BufName);
-    assert(ArgNum == 1 || ArgNum == 2);
     OsName << " Invalid use of '" << Name << "'" ;
 
     SmallString<256> Buf;
diff --git a/test/Analysis/CFContainers-invalid.c b/test/Analysis/CFContainers-invalid.c
new file mode 100644 (file)
index 0000000..939af06
--- /dev/null
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 -analyze -analyzer-checker=osx.coreFoundation.containers.PointerSizedValues -triple x86_64-apple-darwin -verify %s
+
+typedef const struct __CFAllocator * CFAllocatorRef;
+typedef const struct __CFArray * CFArrayRef;
+typedef const struct __CFDictionary * CFDictionaryRef;
+typedef const struct __CFSet * CFSetRef;
+
+extern const CFAllocatorRef kCFAllocatorDefault;
+
+// Unexpected declarations for these:
+CFArrayRef CFArrayCreate(CFAllocatorRef);
+CFDictionaryRef CFDictionaryCreate(CFAllocatorRef);
+CFSetRef CFSetCreate(CFAllocatorRef);
+
+void testNoCrash() {
+  (void)CFArrayCreate(kCFAllocatorDefault);
+  (void)CFDictionaryCreate(kCFAllocatorDefault);
+  (void)CFSetCreate(kCFAllocatorDefault);
+}