]> granicus.if.org Git - clang/commitdiff
Change *BugReport constructors to take StringRefs.
authorBenjamin Kramer <benny.kra@googlemail.com>
Sat, 14 Nov 2009 12:08:24 +0000 (12:08 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Sat, 14 Nov 2009 12:08:24 +0000 (12:08 +0000)
- Eliminates many calls to std::string.c_str()
- Fixes an invalid read in ReturnStackAddressChecker due to an unsafe call to
  StringRef.data() which doesn't guarantee null-termination.

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

18 files changed:
include/clang/Analysis/PathSensitive/BugReporter.h
lib/Analysis/ArrayBoundChecker.cpp
lib/Analysis/BadCallChecker.cpp
lib/Analysis/CallGraph.cpp
lib/Analysis/CastToStructChecker.cpp
lib/Analysis/DereferenceChecker.cpp
lib/Analysis/DivZeroChecker.cpp
lib/Analysis/FixedAddressChecker.cpp
lib/Analysis/MallocChecker.cpp
lib/Analysis/PointerArithChecker.cpp
lib/Analysis/PointerSubChecker.cpp
lib/Analysis/ReturnPointerRangeChecker.cpp
lib/Analysis/ReturnStackAddressChecker.cpp
lib/Analysis/ReturnUndefChecker.cpp
lib/Analysis/UndefinedArgChecker.cpp
lib/Analysis/UndefinedArraySubscriptChecker.cpp
lib/Analysis/UndefinedAssignmentChecker.cpp
lib/Analysis/VLASizeChecker.cpp

index 914118cb430d8a29376af143de2555b9d7200ce3..f4297350ec70179b0db5afeb3de4aa3ef04f0ac0 100644 (file)
@@ -81,10 +81,10 @@ public:
             getOriginalNode(const ExplodedNode* N) = 0;
   };
 
-  BugReport(BugType& bt, const char* desc, const ExplodedNode *n)
+  BugReport(BugType& bt, llvm::StringRef desc, const ExplodedNode *n)
     : BT(bt), Description(desc), EndNode(n) {}
 
-  BugReport(BugType& bt, const char* shortDesc, const char* desc,
+  BugReport(BugType& bt, llvm::StringRef shortDesc, llvm::StringRef desc,
             const ExplodedNode *n)
   : BT(bt), ShortDescription(shortDesc), Description(desc), EndNode(n) {}
 
@@ -193,11 +193,11 @@ public:
 class RangedBugReport : public BugReport {
   std::vector<SourceRange> Ranges;
 public:
-  RangedBugReport(BugType& D, const char* description, ExplodedNode *n)
+  RangedBugReport(BugType& D, llvm::StringRef description, ExplodedNode *n)
     : BugReport(D, description, n) {}
 
-  RangedBugReport(BugType& D, const char *shortDescription,
-                  const char *description, ExplodedNode *n)
+  RangedBugReport(BugType& D, llvm::StringRef shortDescription,
+                  llvm::StringRef description, ExplodedNode *n)
   : BugReport(D, shortDescription, description, n) {}
 
   ~RangedBugReport();
@@ -229,11 +229,11 @@ private:
   Creators creators;
 
 public:
-  EnhancedBugReport(BugType& D, const char* description, ExplodedNode *n)
+  EnhancedBugReport(BugType& D, llvm::StringRef description, ExplodedNode *n)
    : RangedBugReport(D, description, n) {}
 
-  EnhancedBugReport(BugType& D, const char *shortDescription,
-                  const char *description, ExplodedNode *n)
+  EnhancedBugReport(BugType& D, llvm::StringRef shortDescription,
+                   llvm::StringRef description, ExplodedNode *n)
     : RangedBugReport(D, shortDescription, description, n) {}
 
   ~EnhancedBugReport() {}
index 34a5631edd3e7e0c73e74028e8bd63b0504722dc..549a22bec1727e71f18f0c3d044a7ae2dcd4242a 100644 (file)
@@ -77,7 +77,7 @@ void ArrayBoundChecker::VisitLocation(CheckerContext &C, const Stmt *S, SVal l){
 
     // Generate a report for this bug.
     RangedBugReport *report = 
-      new RangedBugReport(*BT, BT->getDescription().c_str(), N);
+      new RangedBugReport(*BT, BT->getDescription(), N);
 
     report->addRange(S->getSourceRange());
 
index 4175e8d141057f382117119b7bfa3fa4e97ff587..7a7ea188ba599c4814efd304ac6e19676e4bdb7c 100644 (file)
@@ -46,7 +46,7 @@ void BadCallChecker::PreVisitCallExpr(CheckerContext &C, const CallExpr *CE) {
                 "Called function pointer is a null or undefined pointer value");
 
       EnhancedBugReport *R =
-        new EnhancedBugReport(*BT, BT->getDescription().c_str(), N);
+        new EnhancedBugReport(*BT, BT->getDescription(), N);
         
       R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue,
                            bugreporter::GetCalleeExpr(N));
index 17dc0685f82fcd3d073f4347d69cbb73029a9189..06e3317691e366153bf872a01fd481014339d74f 100644 (file)
@@ -117,7 +117,7 @@ void CallGraph::print(llvm::raw_ostream &os) {
          << " calls:\n";
       for (CallGraphNode::iterator CI = I->second->begin(),
              CE = I->second->end(); CI != CE; ++CI) {
-        os << "    " << CI->second->getName().c_str();
+        os << "    " << CI->second->getName();
       }
       os << '\n';
     }
index bda8ff47bf85a98b31297e63814623f6ed0b75e8..ccd4a3333e222f2d8927a82bc0ed2653cc44869d 100644 (file)
@@ -65,8 +65,7 @@ void CastToStructChecker::PreVisitCastExpr(CheckerContext &C,
                             "Casting a non-structure type to a structure type "
                             "and accessing a field can lead to memory access "
                             "errors or data corruption.");
-      RangedBugReport *R = new RangedBugReport(*BT,BT->getDescription().c_str(),
-                                               N);
+      RangedBugReport *R = new RangedBugReport(*BT,BT->getDescription(), N);
       R->addRange(CE->getSourceRange());
       C.EmitReport(R);
     }
index 6fdad5d2a40781e9f74d0e416c08a1878aa8a2b2..c3aa8f3a2879c0b36409204116d7257521e5850c 100644 (file)
@@ -62,7 +62,7 @@ void DereferenceChecker::VisitLocation(CheckerContext &C, const Stmt *S,
         BT_undef = new BuiltinBug("Dereference of undefined pointer value");
       
       EnhancedBugReport *report =
-        new EnhancedBugReport(*BT_undef, BT_undef->getDescription().c_str(), N);
+        new EnhancedBugReport(*BT_undef, BT_undef->getDescription(), N);
       report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue,
                                 bugreporter::GetDerefExpr(N));
       C.EmitReport(report);
@@ -93,7 +93,7 @@ void DereferenceChecker::VisitLocation(CheckerContext &C, const Stmt *S,
                                    "Dereference of null pointer");
 
         EnhancedBugReport *report =
-          new EnhancedBugReport(*BT_null, BT_null->getDescription().c_str(), N);
+          new EnhancedBugReport(*BT_null, BT_null->getDescription(), N);
         report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue,
                                   bugreporter::GetDerefExpr(N));
         
index 5f949fdc3924ac0daf8ae0b9722d2632824964e8..a8630f10088ef5fab1e7c41c3a917b8a15e107d0 100644 (file)
@@ -68,7 +68,7 @@ void DivZeroChecker::PreVisitBinaryOperator(CheckerContext &C,
         BT = new BuiltinBug("Division by zero");
 
       EnhancedBugReport *R = 
-        new EnhancedBugReport(*BT, BT->getDescription().c_str(), N);
+        new EnhancedBugReport(*BT, BT->getDescription(), N);
 
       R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue,
                            bugreporter::GetDenomExpr(N));
index e30e3d0ce1ca4d9e758b266f3f59c334e880ddef..80096dcb70d03b019edd84ec366ddca84c371655 100644 (file)
@@ -59,8 +59,7 @@ void FixedAddressChecker::PreVisitBinaryOperator(CheckerContext &C,
                           "Using a fixed address is not portable because that "
                           "address will probably not be valid in all "
                           "environments or platforms.");
-    RangedBugReport *R = new RangedBugReport(*BT, BT->getDescription().c_str(),
-                                             N);
+    RangedBugReport *R = new RangedBugReport(*BT, BT->getDescription(), N);
     R->addRange(B->getRHS()->getSourceRange());
     C.EmitReport(R);
   }
index 6655194d5e9285b8de5ed40899acae0547ab21d2..6129358cf7351e473bc428362f812093d7075091 100644 (file)
@@ -123,7 +123,7 @@ void MallocChecker::FreeMem(CheckerContext &C, const CallExpr *CE) {
                          "Try to free a memory block that has been released");
       // FIXME: should find where it's freed last time.
       BugReport *R = new BugReport(*BT_DoubleFree, 
-                                   BT_DoubleFree->getDescription().c_str(), N);
+                                   BT_DoubleFree->getDescription(), N);
       C.EmitReport(R);
     }
     return;
@@ -152,7 +152,7 @@ void MallocChecker::EvalDeadSymbols(CheckerContext &C, const Stmt *S,
                      "Allocated memory never released. Potential memory leak.");
         // FIXME: where it is allocated.
         BugReport *R = new BugReport(*BT_Leak,
-                                     BT_Leak->getDescription().c_str(), N);
+                                     BT_Leak->getDescription(), N);
         C.EmitReport(R);
       }
     }
index 5f5badb3d2df10e49937a389c0b0bcc6b12be0bb..93823484e1d02d568bfb8488169685de89999697 100644 (file)
@@ -59,8 +59,7 @@ void PointerArithChecker::PreVisitBinaryOperator(CheckerContext &C,
                             "Pointer arithmetic done on non-array variables "
                             "means reliance on memory layout, which is "
                             "dangerous.");
-      RangedBugReport *R = new RangedBugReport(*BT,BT->getDescription().c_str(),
-                                               N);
+      RangedBugReport *R = new RangedBugReport(*BT, BT->getDescription(), N);
       R->addRange(B->getSourceRange());
       C.EmitReport(R);
     }
index 20279c67b3eb886a110a9895844d5d019a876da3..4c7906f4beba84e4a2b85c3320bd2b4b8f68550c 100644 (file)
@@ -66,8 +66,7 @@ void PointerSubChecker::PreVisitBinaryOperator(CheckerContext &C,
       BT = new BuiltinBug("Pointer subtraction", 
                           "Subtraction of two pointers that do not point to "
                           "the same memory chunk may cause incorrect result.");
-    RangedBugReport *R = new RangedBugReport(*BT, BT->getDescription().c_str(),
-                                             N);
+    RangedBugReport *R = new RangedBugReport(*BT, BT->getDescription(), N);
     R->addRange(B->getSourceRange());
     C.EmitReport(R);
   }
index f007d0e0d1ac062feb0905ab32d6ca6ec773b31a..44887b2625da4bf954fd913c22056272e5dac666 100644 (file)
@@ -88,7 +88,7 @@ void ReturnPointerRangeChecker::PreVisitReturnStmt(CheckerContext &C,
 
     // Generate a report for this bug.
     RangedBugReport *report = 
-      new RangedBugReport(*BT, BT->getDescription().c_str(), N);
+      new RangedBugReport(*BT, BT->getDescription(), N);
 
     report->addRange(RetE->getSourceRange());
 
index 9f22b3c48e03a603e2a15c9b6dcb983a266fb8e8..e4be8712d09b3ff8746c8d68e59a879254f86c6b 100644 (file)
@@ -88,7 +88,7 @@ void ReturnStackAddressChecker::PreVisitReturnStmt(CheckerContext &C,
         << R->getString() << "' returned.";
   }
 
-  RangedBugReport *report = new RangedBugReport(*BT, os.str().data(), N);
+  RangedBugReport *report = new RangedBugReport(*BT, os.str(), N);
   report->addRange(RS->getSourceRange());
   if (range.isValid())
     report->addRange(range);
index adde3f5d25f01c836fcec41111afbe07c1a86e0d..796c7608c86d60d19e93a347ee77156e53ce6471 100644 (file)
@@ -60,7 +60,7 @@ void ReturnUndefChecker::PreVisitReturnStmt(CheckerContext &C,
                         "Undefined or garbage value returned to caller");
     
   EnhancedBugReport *report = 
-    new EnhancedBugReport(*BT, BT->getDescription().c_str(), N);  
+    new EnhancedBugReport(*BT, BT->getDescription(), N);
 
   report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, RetE);
 
index 43b4847cc4e0309f636dc2d00a8ddafe55d4b0cc..923a7e1bed0bd18c57dfbed25aab18a1a3c2b961 100644 (file)
@@ -46,8 +46,7 @@ void UndefinedArgChecker::PreVisitCallExpr(CheckerContext &C,
           BT = new BuiltinBug("Pass-by-value argument in function call is "
                               "undefined");
         // Generate a report for this bug.
-        EnhancedBugReport *R = new EnhancedBugReport(*BT, BT->getName().c_str(),
-                                                     N);
+        EnhancedBugReport *R = new EnhancedBugReport(*BT, BT->getName(), N);
         R->addRange((*I)->getSourceRange());
         R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, *I);
         C.EmitReport(R);
index 47d615dbbdf8302a71d06fb4f1a7e76935b527db..887c7755fe45ab407ba21e552e9a1b9c110bd31f 100644 (file)
@@ -46,8 +46,7 @@ UndefinedArraySubscriptChecker::PreVisitArraySubscriptExpr(CheckerContext &C,
         BT = new BuiltinBug("Array subscript is undefined");
 
       // Generate a report for this bug.
-      EnhancedBugReport *R = new EnhancedBugReport(*BT, BT->getName().c_str(),
-                                                   N);
+      EnhancedBugReport *R = new EnhancedBugReport(*BT, BT->getName(), N);
       R->addRange(A->getIdx()->getSourceRange());
       R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, 
                            A->getIdx());
index 26f9ee30d44d6fbe38750ee4cdbf85edbb37e3f3..b8062f3595622f69db57cfda88227284841c9410 100644 (file)
@@ -39,7 +39,7 @@ void UndefinedAssignmentChecker::PreVisitBind(CheckerContext &C,
     BT = new BuiltinBug("Assigned value is garbage or undefined");
 
   // Generate a report for this bug.
-  EnhancedBugReport *R = new EnhancedBugReport(*BT, BT->getName().c_str(), N);
+  EnhancedBugReport *R = new EnhancedBugReport(*BT, BT->getName(), N);
 
   if (AssignE) {
     const Expr *ex = 0;
index 5cb700ed67b58a4739f5aa2719156ad4d4d0da29..799a73e293c888ca4b9c36e94dee6b3d8323b9e7 100644 (file)
@@ -64,7 +64,7 @@ void VLASizeChecker::PreVisitDeclStmt(CheckerContext &C, const DeclStmt *DS) {
                                 "garbage value as its size");
 
     EnhancedBugReport *report =
-      new EnhancedBugReport(*BT_undef, BT_undef->getName().c_str(), N);
+      new EnhancedBugReport(*BT_undef, BT_undef->getName(), N);
     report->addRange(SE->getSourceRange());
     report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, SE);
     C.EmitReport(report);
@@ -84,7 +84,7 @@ void VLASizeChecker::PreVisitDeclStmt(CheckerContext &C, const DeclStmt *DS) {
                                "size");
 
     EnhancedBugReport *report =
-      new EnhancedBugReport(*BT_zero, BT_zero->getName().c_str(), N);
+      new EnhancedBugReport(*BT_zero, BT_zero->getName(), N);
     report->addRange(SE->getSourceRange());
     report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, SE);
     C.EmitReport(report);