blob: 8a868bddc8651f8c93aa27dc41e1611b02ede09b [file] [log] [blame]
Anna Zaksc800f682011-10-11 16:49:54 +00001//= CStringChecker.cpp - Checks calls to C string functions --------*- C++ -*-//
Jordy Roseccbf7ee2010-07-06 23:11:01 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This defines CStringChecker, which is an assortment of checks on calls
11// to functions in <string.h>.
12//
13//===----------------------------------------------------------------------===//
14
Argyrios Kyrtzidisa0decc92011-02-15 21:25:03 +000015#include "ClangSACheckers.h"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000016#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000017#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +000018#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000019#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Ted Kremenek18c66fd2011-08-15 22:09:50 +000020#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
Benjamin Kramer00bd44d2012-02-04 12:31:12 +000021#include "llvm/ADT/STLExtras.h"
Jordy Roseccbf7ee2010-07-06 23:11:01 +000022#include "llvm/ADT/StringSwitch.h"
23
24using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000025using namespace ento;
Jordy Roseccbf7ee2010-07-06 23:11:01 +000026
27namespace {
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000028class CStringChecker : public Checker< eval::Call,
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +000029 check::PreStmt<DeclStmt>,
30 check::LiveSymbols,
31 check::DeadSymbols,
32 check::RegionChanges
33 > {
Jordy Rose9e49d9f2011-06-20 02:06:40 +000034 mutable llvm::OwningPtr<BugType> BT_Null, BT_Bounds,
Jordy Rosed5af0e12011-06-15 05:52:56 +000035 BT_Overlap, BT_NotCString,
36 BT_AdditionOverflow;
Jordy Rose9e49d9f2011-06-20 02:06:40 +000037 mutable const char *CurrentFunctionDescription;
38
Jordy Roseccbf7ee2010-07-06 23:11:01 +000039public:
Jordy Roseccbf7ee2010-07-06 23:11:01 +000040 static void *getTag() { static int tag; return &tag; }
41
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +000042 bool evalCall(const CallExpr *CE, CheckerContext &C) const;
43 void checkPreStmt(const DeclStmt *DS, CheckerContext &C) const;
Ted Kremenek8bef8232012-01-26 21:29:00 +000044 void checkLiveSymbols(ProgramStateRef state, SymbolReaper &SR) const;
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +000045 void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const;
Ted Kremenek8bef8232012-01-26 21:29:00 +000046 bool wantsRegionChangeUpdate(ProgramStateRef state) const;
Jordy Rosea5261542010-08-14 21:02:52 +000047
Ted Kremenek8bef8232012-01-26 21:29:00 +000048 ProgramStateRef
49 checkRegionChanges(ProgramStateRef state,
Ted Kremenek18c66fd2011-08-15 22:09:50 +000050 const StoreManager::InvalidatedSymbols *,
Jordy Rose537716a2011-08-27 22:51:26 +000051 ArrayRef<const MemRegion *> ExplicitRegions,
52 ArrayRef<const MemRegion *> Regions) const;
Jordy Roseccbf7ee2010-07-06 23:11:01 +000053
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +000054 typedef void (CStringChecker::*FnCheck)(CheckerContext &,
55 const CallExpr *) const;
Jordy Roseccbf7ee2010-07-06 23:11:01 +000056
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +000057 void evalMemcpy(CheckerContext &C, const CallExpr *CE) const;
Lenny Maioranib8b875b2011-03-31 21:36:53 +000058 void evalMempcpy(CheckerContext &C, const CallExpr *CE) const;
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +000059 void evalMemmove(CheckerContext &C, const CallExpr *CE) const;
60 void evalBcopy(CheckerContext &C, const CallExpr *CE) const;
Lenny Maioranib8b875b2011-03-31 21:36:53 +000061 void evalCopyCommon(CheckerContext &C, const CallExpr *CE,
Ted Kremenek8bef8232012-01-26 21:29:00 +000062 ProgramStateRef state,
Ted Kremenek18c66fd2011-08-15 22:09:50 +000063 const Expr *Size,
64 const Expr *Source,
65 const Expr *Dest,
Lenny Maioranib8b875b2011-03-31 21:36:53 +000066 bool Restricted = false,
67 bool IsMempcpy = false) const;
Jordy Rosed325ffb2010-07-08 23:57:29 +000068
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +000069 void evalMemcmp(CheckerContext &C, const CallExpr *CE) const;
Jordy Roseccbf7ee2010-07-06 23:11:01 +000070
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +000071 void evalstrLength(CheckerContext &C, const CallExpr *CE) const;
72 void evalstrnLength(CheckerContext &C, const CallExpr *CE) const;
Ted Kremenek18c66fd2011-08-15 22:09:50 +000073 void evalstrLengthCommon(CheckerContext &C,
74 const CallExpr *CE,
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +000075 bool IsStrnlen = false) const;
Jordy Rose19c5dd12010-07-27 01:37:31 +000076
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +000077 void evalStrcpy(CheckerContext &C, const CallExpr *CE) const;
78 void evalStrncpy(CheckerContext &C, const CallExpr *CE) const;
79 void evalStpcpy(CheckerContext &C, const CallExpr *CE) const;
Ted Kremenek18c66fd2011-08-15 22:09:50 +000080 void evalStrcpyCommon(CheckerContext &C,
81 const CallExpr *CE,
82 bool returnEnd,
83 bool isBounded,
84 bool isAppending) const;
Lenny Maiorani067bbd02011-04-09 15:12:58 +000085
86 void evalStrcat(CheckerContext &C, const CallExpr *CE) const;
87 void evalStrncat(CheckerContext &C, const CallExpr *CE) const;
Jordy Rosee64f3112010-08-16 07:51:42 +000088
Lenny Maiorani318dd922011-04-12 17:08:43 +000089 void evalStrcmp(CheckerContext &C, const CallExpr *CE) const;
Lenny Maiorani357f6ee2011-04-25 22:21:00 +000090 void evalStrncmp(CheckerContext &C, const CallExpr *CE) const;
Lenny Maioranibd1d16a2011-04-28 15:09:11 +000091 void evalStrcasecmp(CheckerContext &C, const CallExpr *CE) const;
Lenny Maiorani454fd2d2011-05-02 19:05:49 +000092 void evalStrncasecmp(CheckerContext &C, const CallExpr *CE) const;
Ted Kremenek18c66fd2011-08-15 22:09:50 +000093 void evalStrcmpCommon(CheckerContext &C,
94 const CallExpr *CE,
95 bool isBounded = false,
96 bool ignoreCase = false) const;
Lenny Maiorani318dd922011-04-12 17:08:43 +000097
Jordy Roseccbf7ee2010-07-06 23:11:01 +000098 // Utility methods
Ted Kremenek8bef8232012-01-26 21:29:00 +000099 std::pair<ProgramStateRef , ProgramStateRef >
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +0000100 static assumeZero(CheckerContext &C,
Ted Kremenek8bef8232012-01-26 21:29:00 +0000101 ProgramStateRef state, SVal V, QualType Ty);
Jordy Rosed325ffb2010-07-08 23:57:29 +0000102
Ted Kremenek8bef8232012-01-26 21:29:00 +0000103 static ProgramStateRef setCStringLength(ProgramStateRef state,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000104 const MemRegion *MR,
105 SVal strLength);
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +0000106 static SVal getCStringLengthForRegion(CheckerContext &C,
Ted Kremenek8bef8232012-01-26 21:29:00 +0000107 ProgramStateRef &state,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000108 const Expr *Ex,
109 const MemRegion *MR,
Jordy Rosed5af0e12011-06-15 05:52:56 +0000110 bool hypothetical);
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000111 SVal getCStringLength(CheckerContext &C,
Ted Kremenek8bef8232012-01-26 21:29:00 +0000112 ProgramStateRef &state,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000113 const Expr *Ex,
114 SVal Buf,
Jordy Rosed5af0e12011-06-15 05:52:56 +0000115 bool hypothetical = false) const;
Jordy Rose19c5dd12010-07-27 01:37:31 +0000116
Lenny Maiorani318dd922011-04-12 17:08:43 +0000117 const StringLiteral *getCStringLiteral(CheckerContext &C,
Ted Kremenek8bef8232012-01-26 21:29:00 +0000118 ProgramStateRef &state,
Lenny Maiorani318dd922011-04-12 17:08:43 +0000119 const Expr *expr,
120 SVal val) const;
121
Ted Kremenek8bef8232012-01-26 21:29:00 +0000122 static ProgramStateRef InvalidateBuffer(CheckerContext &C,
123 ProgramStateRef state,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000124 const Expr *Ex, SVal V);
Jordy Rosee64f3112010-08-16 07:51:42 +0000125
Ted Kremenek9c378f72011-08-12 23:37:29 +0000126 static bool SummarizeRegion(raw_ostream &os, ASTContext &Ctx,
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +0000127 const MemRegion *MR);
Jordy Rose19c5dd12010-07-27 01:37:31 +0000128
129 // Re-usable checks
Ted Kremenek8bef8232012-01-26 21:29:00 +0000130 ProgramStateRef checkNonNull(CheckerContext &C,
131 ProgramStateRef state,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000132 const Expr *S,
133 SVal l) const;
Ted Kremenek8bef8232012-01-26 21:29:00 +0000134 ProgramStateRef CheckLocation(CheckerContext &C,
135 ProgramStateRef state,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000136 const Expr *S,
137 SVal l,
138 const char *message = NULL) const;
Ted Kremenek8bef8232012-01-26 21:29:00 +0000139 ProgramStateRef CheckBufferAccess(CheckerContext &C,
140 ProgramStateRef state,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000141 const Expr *Size,
142 const Expr *FirstBuf,
143 const Expr *SecondBuf,
144 const char *firstMessage = NULL,
145 const char *secondMessage = NULL,
146 bool WarnAboutSize = false) const;
147
Ted Kremenek8bef8232012-01-26 21:29:00 +0000148 ProgramStateRef CheckBufferAccess(CheckerContext &C,
149 ProgramStateRef state,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000150 const Expr *Size,
151 const Expr *Buf,
152 const char *message = NULL,
153 bool WarnAboutSize = false) const {
Jordy Rose9e49d9f2011-06-20 02:06:40 +0000154 // This is a convenience override.
Jordy Rose5e5f1502011-06-20 03:49:16 +0000155 return CheckBufferAccess(C, state, Size, Buf, NULL, message, NULL,
156 WarnAboutSize);
Jordy Rose9e49d9f2011-06-20 02:06:40 +0000157 }
Ted Kremenek8bef8232012-01-26 21:29:00 +0000158 ProgramStateRef CheckOverlap(CheckerContext &C,
159 ProgramStateRef state,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000160 const Expr *Size,
161 const Expr *First,
162 const Expr *Second) const;
163 void emitOverlapBug(CheckerContext &C,
Ted Kremenek8bef8232012-01-26 21:29:00 +0000164 ProgramStateRef state,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000165 const Stmt *First,
166 const Stmt *Second) const;
167
Ted Kremenek8bef8232012-01-26 21:29:00 +0000168 ProgramStateRef checkAdditionOverflow(CheckerContext &C,
169 ProgramStateRef state,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000170 NonLoc left,
171 NonLoc right) const;
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000172};
Jordy Rosea5261542010-08-14 21:02:52 +0000173
174class CStringLength {
175public:
176 typedef llvm::ImmutableMap<const MemRegion *, SVal> EntryMap;
177};
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000178} //end anonymous namespace
179
Jordy Rosea5261542010-08-14 21:02:52 +0000180namespace clang {
Ted Kremenek9ef65372010-12-23 07:20:52 +0000181namespace ento {
Jordy Rosea5261542010-08-14 21:02:52 +0000182 template <>
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000183 struct ProgramStateTrait<CStringLength>
184 : public ProgramStatePartialTrait<CStringLength::EntryMap> {
Jordy Rosea5261542010-08-14 21:02:52 +0000185 static void *GDMIndex() { return CStringChecker::getTag(); }
186 };
187}
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +0000188}
Jordy Rosea5261542010-08-14 21:02:52 +0000189
Jordy Rosed325ffb2010-07-08 23:57:29 +0000190//===----------------------------------------------------------------------===//
191// Individual checks and utility methods.
192//===----------------------------------------------------------------------===//
193
Ted Kremenek8bef8232012-01-26 21:29:00 +0000194std::pair<ProgramStateRef , ProgramStateRef >
195CStringChecker::assumeZero(CheckerContext &C, ProgramStateRef state, SVal V,
Jordy Rosed325ffb2010-07-08 23:57:29 +0000196 QualType Ty) {
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000197 DefinedSVal *val = dyn_cast<DefinedSVal>(&V);
198 if (!val)
Ted Kremenek8bef8232012-01-26 21:29:00 +0000199 return std::pair<ProgramStateRef , ProgramStateRef >(state, state);
Jordy Rosea6b808c2010-07-07 07:48:06 +0000200
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000201 SValBuilder &svalBuilder = C.getSValBuilder();
202 DefinedOrUnknownSVal zero = svalBuilder.makeZeroVal(Ty);
203 return state->assume(svalBuilder.evalEQ(state, *val, zero));
Jordy Rosed325ffb2010-07-08 23:57:29 +0000204}
Jordy Rosea6b808c2010-07-07 07:48:06 +0000205
Ted Kremenek8bef8232012-01-26 21:29:00 +0000206ProgramStateRef CStringChecker::checkNonNull(CheckerContext &C,
207 ProgramStateRef state,
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +0000208 const Expr *S, SVal l) const {
Jordy Rosed325ffb2010-07-08 23:57:29 +0000209 // If a previous check has failed, propagate the failure.
210 if (!state)
211 return NULL;
212
Ted Kremenek8bef8232012-01-26 21:29:00 +0000213 ProgramStateRef stateNull, stateNonNull;
Ted Kremenek28f47b92010-12-01 22:16:56 +0000214 llvm::tie(stateNull, stateNonNull) = assumeZero(C, state, l, S->getType());
Jordy Rosed325ffb2010-07-08 23:57:29 +0000215
216 if (stateNull && !stateNonNull) {
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000217 ExplodedNode *N = C.generateSink(stateNull);
Jordy Rosea6b808c2010-07-07 07:48:06 +0000218 if (!N)
219 return NULL;
220
Jordy Rosed325ffb2010-07-08 23:57:29 +0000221 if (!BT_Null)
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +0000222 BT_Null.reset(new BuiltinBug("API",
223 "Null pointer argument in call to byte string function"));
Jordy Rosea6b808c2010-07-07 07:48:06 +0000224
Jordy Rose9e49d9f2011-06-20 02:06:40 +0000225 llvm::SmallString<80> buf;
226 llvm::raw_svector_ostream os(buf);
227 assert(CurrentFunctionDescription);
228 os << "Null pointer argument in call to " << CurrentFunctionDescription;
229
Jordy Rosea6b808c2010-07-07 07:48:06 +0000230 // Generate a report for this bug.
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +0000231 BuiltinBug *BT = static_cast<BuiltinBug*>(BT_Null.get());
Anna Zakse172e8b2011-08-17 23:00:25 +0000232 BugReport *report = new BugReport(*BT, os.str(), N);
Jordy Rosea6b808c2010-07-07 07:48:06 +0000233
234 report->addRange(S->getSourceRange());
Anna Zaks50bbc162011-08-19 22:33:38 +0000235 report->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N, S));
Jordy Rosea6b808c2010-07-07 07:48:06 +0000236 C.EmitReport(report);
237 return NULL;
238 }
239
240 // From here on, assume that the value is non-null.
Jordy Rosed325ffb2010-07-08 23:57:29 +0000241 assert(stateNonNull);
242 return stateNonNull;
Jordy Rosea6b808c2010-07-07 07:48:06 +0000243}
244
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000245// FIXME: This was originally copied from ArrayBoundChecker.cpp. Refactor?
Ted Kremenek8bef8232012-01-26 21:29:00 +0000246ProgramStateRef CStringChecker::CheckLocation(CheckerContext &C,
247 ProgramStateRef state,
Jordy Rosee64f3112010-08-16 07:51:42 +0000248 const Expr *S, SVal l,
Jordy Rose9e49d9f2011-06-20 02:06:40 +0000249 const char *warningMsg) const {
Jordy Rosed325ffb2010-07-08 23:57:29 +0000250 // If a previous check has failed, propagate the failure.
251 if (!state)
252 return NULL;
253
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000254 // Check for out of bound array element access.
255 const MemRegion *R = l.getAsRegion();
256 if (!R)
257 return state;
258
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000259 const ElementRegion *ER = dyn_cast<ElementRegion>(R);
260 if (!ER)
261 return state;
262
Zhongxing Xu018220c2010-08-11 06:10:55 +0000263 assert(ER->getValueType() == C.getASTContext().CharTy &&
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000264 "CheckLocation should only be called with char* ElementRegions");
265
266 // Get the size of the array.
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000267 const SubRegion *superReg = cast<SubRegion>(ER->getSuperRegion());
268 SValBuilder &svalBuilder = C.getSValBuilder();
Jordy Rose1e022412011-06-16 05:51:02 +0000269 SVal Extent =
270 svalBuilder.convertToArrayIndex(superReg->getExtent(svalBuilder));
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000271 DefinedOrUnknownSVal Size = cast<DefinedOrUnknownSVal>(Extent);
272
273 // Get the index of the accessed element.
Gabor Greif89b06582010-09-09 10:51:37 +0000274 DefinedOrUnknownSVal Idx = cast<DefinedOrUnknownSVal>(ER->getIndex());
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000275
Ted Kremenek8bef8232012-01-26 21:29:00 +0000276 ProgramStateRef StInBound = state->assumeInBound(Idx, Size, true);
277 ProgramStateRef StOutBound = state->assumeInBound(Idx, Size, false);
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000278 if (StOutBound && !StInBound) {
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000279 ExplodedNode *N = C.generateSink(StOutBound);
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000280 if (!N)
281 return NULL;
282
Jordy Rose9e49d9f2011-06-20 02:06:40 +0000283 if (!BT_Bounds) {
284 BT_Bounds.reset(new BuiltinBug("Out-of-bound array access",
285 "Byte string function accesses out-of-bound array element"));
286 }
287 BuiltinBug *BT = static_cast<BuiltinBug*>(BT_Bounds.get());
288
289 // Generate a report for this bug.
Anna Zakse172e8b2011-08-17 23:00:25 +0000290 BugReport *report;
Jordy Rose9e49d9f2011-06-20 02:06:40 +0000291 if (warningMsg) {
Anna Zakse172e8b2011-08-17 23:00:25 +0000292 report = new BugReport(*BT, warningMsg, N);
Jordy Rosee64f3112010-08-16 07:51:42 +0000293 } else {
Jordy Rose9e49d9f2011-06-20 02:06:40 +0000294 assert(CurrentFunctionDescription);
295 assert(CurrentFunctionDescription[0] != '\0');
296
297 llvm::SmallString<80> buf;
298 llvm::raw_svector_ostream os(buf);
299 os << (char)toupper(CurrentFunctionDescription[0])
300 << &CurrentFunctionDescription[1]
301 << " accesses out-of-bound array element";
Anna Zakse172e8b2011-08-17 23:00:25 +0000302 report = new BugReport(*BT, os.str(), N);
Jordy Rosee64f3112010-08-16 07:51:42 +0000303 }
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000304
305 // FIXME: It would be nice to eventually make this diagnostic more clear,
306 // e.g., by referencing the original declaration or by saying *why* this
307 // reference is outside the range.
308
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000309 report->addRange(S->getSourceRange());
310 C.EmitReport(report);
311 return NULL;
312 }
313
314 // Array bound check succeeded. From this point forward the array bound
315 // should always succeed.
316 return StInBound;
317}
318
Ted Kremenek8bef8232012-01-26 21:29:00 +0000319ProgramStateRef CStringChecker::CheckBufferAccess(CheckerContext &C,
320 ProgramStateRef state,
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000321 const Expr *Size,
322 const Expr *FirstBuf,
Jordy Rosee64f3112010-08-16 07:51:42 +0000323 const Expr *SecondBuf,
Jordy Rose9e49d9f2011-06-20 02:06:40 +0000324 const char *firstMessage,
Jordy Rose5e5f1502011-06-20 03:49:16 +0000325 const char *secondMessage,
326 bool WarnAboutSize) const {
Jordy Rosed325ffb2010-07-08 23:57:29 +0000327 // If a previous check has failed, propagate the failure.
328 if (!state)
329 return NULL;
330
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000331 SValBuilder &svalBuilder = C.getSValBuilder();
Jordy Rose1e022412011-06-16 05:51:02 +0000332 ASTContext &Ctx = svalBuilder.getContext();
Ted Kremenek5eca4822012-01-06 22:09:28 +0000333 const LocationContext *LCtx = C.getLocationContext();
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000334
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000335 QualType sizeTy = Size->getType();
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000336 QualType PtrTy = Ctx.getPointerType(Ctx.CharTy);
337
Jordy Rosea6b808c2010-07-07 07:48:06 +0000338 // Check that the first buffer is non-null.
Ted Kremenek5eca4822012-01-06 22:09:28 +0000339 SVal BufVal = state->getSVal(FirstBuf, LCtx);
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000340 state = checkNonNull(C, state, FirstBuf, BufVal);
Jordy Rosea6b808c2010-07-07 07:48:06 +0000341 if (!state)
342 return NULL;
343
Jordy Rosed325ffb2010-07-08 23:57:29 +0000344 // Get the access length and make sure it is known.
Jordy Rose9e49d9f2011-06-20 02:06:40 +0000345 // FIXME: This assumes the caller has already checked that the access length
346 // is positive. And that it's unsigned.
Ted Kremenek5eca4822012-01-06 22:09:28 +0000347 SVal LengthVal = state->getSVal(Size, LCtx);
Jordy Rosed325ffb2010-07-08 23:57:29 +0000348 NonLoc *Length = dyn_cast<NonLoc>(&LengthVal);
349 if (!Length)
350 return state;
351
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000352 // Compute the offset of the last element to be accessed: size-1.
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000353 NonLoc One = cast<NonLoc>(svalBuilder.makeIntVal(1, sizeTy));
354 NonLoc LastOffset = cast<NonLoc>(svalBuilder.evalBinOpNN(state, BO_Sub,
355 *Length, One, sizeTy));
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000356
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000357 // Check that the first buffer is sufficiently long.
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000358 SVal BufStart = svalBuilder.evalCast(BufVal, PtrTy, FirstBuf->getType());
Jordy Roseb6a40262010-08-05 23:11:30 +0000359 if (Loc *BufLoc = dyn_cast<Loc>(&BufStart)) {
Jordy Rose5e5f1502011-06-20 03:49:16 +0000360 const Expr *warningExpr = (WarnAboutSize ? Size : FirstBuf);
361
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000362 SVal BufEnd = svalBuilder.evalBinOpLN(state, BO_Add, *BufLoc,
363 LastOffset, PtrTy);
Jordy Rose5e5f1502011-06-20 03:49:16 +0000364 state = CheckLocation(C, state, warningExpr, BufEnd, firstMessage);
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000365
Jordy Roseb6a40262010-08-05 23:11:30 +0000366 // If the buffer isn't large enough, abort.
367 if (!state)
368 return NULL;
369 }
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000370
371 // If there's a second buffer, check it as well.
372 if (SecondBuf) {
Ted Kremenek5eca4822012-01-06 22:09:28 +0000373 BufVal = state->getSVal(SecondBuf, LCtx);
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000374 state = checkNonNull(C, state, SecondBuf, BufVal);
Jordy Rosea6b808c2010-07-07 07:48:06 +0000375 if (!state)
376 return NULL;
377
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000378 BufStart = svalBuilder.evalCast(BufVal, PtrTy, SecondBuf->getType());
Jordy Roseb6a40262010-08-05 23:11:30 +0000379 if (Loc *BufLoc = dyn_cast<Loc>(&BufStart)) {
Jordy Rose5e5f1502011-06-20 03:49:16 +0000380 const Expr *warningExpr = (WarnAboutSize ? Size : SecondBuf);
381
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000382 SVal BufEnd = svalBuilder.evalBinOpLN(state, BO_Add, *BufLoc,
383 LastOffset, PtrTy);
Jordy Rose5e5f1502011-06-20 03:49:16 +0000384 state = CheckLocation(C, state, warningExpr, BufEnd, secondMessage);
Jordy Roseb6a40262010-08-05 23:11:30 +0000385 }
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000386 }
387
388 // Large enough or not, return this state!
389 return state;
390}
391
Ted Kremenek8bef8232012-01-26 21:29:00 +0000392ProgramStateRef CStringChecker::CheckOverlap(CheckerContext &C,
393 ProgramStateRef state,
Jordy Rosed325ffb2010-07-08 23:57:29 +0000394 const Expr *Size,
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000395 const Expr *First,
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +0000396 const Expr *Second) const {
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000397 // Do a simple check for overlap: if the two arguments are from the same
398 // buffer, see if the end of the first is greater than the start of the second
399 // or vice versa.
400
Jordy Rosed325ffb2010-07-08 23:57:29 +0000401 // If a previous check has failed, propagate the failure.
402 if (!state)
403 return NULL;
404
Ted Kremenek8bef8232012-01-26 21:29:00 +0000405 ProgramStateRef stateTrue, stateFalse;
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000406
407 // Get the buffer values and make sure they're known locations.
Ted Kremenek5eca4822012-01-06 22:09:28 +0000408 const LocationContext *LCtx = C.getLocationContext();
409 SVal firstVal = state->getSVal(First, LCtx);
410 SVal secondVal = state->getSVal(Second, LCtx);
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000411
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000412 Loc *firstLoc = dyn_cast<Loc>(&firstVal);
413 if (!firstLoc)
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000414 return state;
415
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000416 Loc *secondLoc = dyn_cast<Loc>(&secondVal);
417 if (!secondLoc)
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000418 return state;
419
420 // Are the two values the same?
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000421 SValBuilder &svalBuilder = C.getSValBuilder();
422 llvm::tie(stateTrue, stateFalse) =
423 state->assume(svalBuilder.evalEQ(state, *firstLoc, *secondLoc));
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000424
425 if (stateTrue && !stateFalse) {
426 // If the values are known to be equal, that's automatically an overlap.
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000427 emitOverlapBug(C, stateTrue, First, Second);
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000428 return NULL;
429 }
430
Ted Kremenek28f47b92010-12-01 22:16:56 +0000431 // assume the two expressions are not equal.
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000432 assert(stateFalse);
433 state = stateFalse;
434
435 // Which value comes first?
Jordy Roseee2fde12011-06-16 05:56:50 +0000436 QualType cmpTy = svalBuilder.getConditionType();
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000437 SVal reverse = svalBuilder.evalBinOpLL(state, BO_GT,
438 *firstLoc, *secondLoc, cmpTy);
439 DefinedOrUnknownSVal *reverseTest = dyn_cast<DefinedOrUnknownSVal>(&reverse);
440 if (!reverseTest)
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000441 return state;
442
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000443 llvm::tie(stateTrue, stateFalse) = state->assume(*reverseTest);
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000444 if (stateTrue) {
445 if (stateFalse) {
446 // If we don't know which one comes first, we can't perform this test.
447 return state;
448 } else {
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000449 // Switch the values so that firstVal is before secondVal.
450 Loc *tmpLoc = firstLoc;
451 firstLoc = secondLoc;
452 secondLoc = tmpLoc;
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000453
454 // Switch the Exprs as well, so that they still correspond.
455 const Expr *tmpExpr = First;
456 First = Second;
457 Second = tmpExpr;
458 }
459 }
460
461 // Get the length, and make sure it too is known.
Ted Kremenek5eca4822012-01-06 22:09:28 +0000462 SVal LengthVal = state->getSVal(Size, LCtx);
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000463 NonLoc *Length = dyn_cast<NonLoc>(&LengthVal);
464 if (!Length)
465 return state;
466
467 // Convert the first buffer's start address to char*.
468 // Bail out if the cast fails.
Jordy Roseee2fde12011-06-16 05:56:50 +0000469 ASTContext &Ctx = svalBuilder.getContext();
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000470 QualType CharPtrTy = Ctx.getPointerType(Ctx.CharTy);
Jordy Rose1e022412011-06-16 05:51:02 +0000471 SVal FirstStart = svalBuilder.evalCast(*firstLoc, CharPtrTy,
472 First->getType());
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000473 Loc *FirstStartLoc = dyn_cast<Loc>(&FirstStart);
474 if (!FirstStartLoc)
475 return state;
476
477 // Compute the end of the first buffer. Bail out if THAT fails.
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000478 SVal FirstEnd = svalBuilder.evalBinOpLN(state, BO_Add,
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000479 *FirstStartLoc, *Length, CharPtrTy);
480 Loc *FirstEndLoc = dyn_cast<Loc>(&FirstEnd);
481 if (!FirstEndLoc)
482 return state;
483
484 // Is the end of the first buffer past the start of the second buffer?
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000485 SVal Overlap = svalBuilder.evalBinOpLL(state, BO_GT,
486 *FirstEndLoc, *secondLoc, cmpTy);
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000487 DefinedOrUnknownSVal *OverlapTest = dyn_cast<DefinedOrUnknownSVal>(&Overlap);
488 if (!OverlapTest)
489 return state;
490
Ted Kremenek28f47b92010-12-01 22:16:56 +0000491 llvm::tie(stateTrue, stateFalse) = state->assume(*OverlapTest);
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000492
493 if (stateTrue && !stateFalse) {
494 // Overlap!
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000495 emitOverlapBug(C, stateTrue, First, Second);
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000496 return NULL;
497 }
498
Ted Kremenek28f47b92010-12-01 22:16:56 +0000499 // assume the two expressions don't overlap.
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000500 assert(stateFalse);
501 return stateFalse;
502}
503
Ted Kremenek8bef8232012-01-26 21:29:00 +0000504void CStringChecker::emitOverlapBug(CheckerContext &C, ProgramStateRef state,
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +0000505 const Stmt *First, const Stmt *Second) const {
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000506 ExplodedNode *N = C.generateSink(state);
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000507 if (!N)
508 return;
509
510 if (!BT_Overlap)
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +0000511 BT_Overlap.reset(new BugType("Unix API", "Improper arguments"));
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000512
513 // Generate a report for this bug.
Anna Zakse172e8b2011-08-17 23:00:25 +0000514 BugReport *report =
515 new BugReport(*BT_Overlap,
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000516 "Arguments must not be overlapping buffers", N);
517 report->addRange(First->getSourceRange());
518 report->addRange(Second->getSourceRange());
519
520 C.EmitReport(report);
521}
522
Ted Kremenek8bef8232012-01-26 21:29:00 +0000523ProgramStateRef CStringChecker::checkAdditionOverflow(CheckerContext &C,
524 ProgramStateRef state,
Jordy Rosed5af0e12011-06-15 05:52:56 +0000525 NonLoc left,
526 NonLoc right) const {
527 // If a previous check has failed, propagate the failure.
528 if (!state)
529 return NULL;
530
531 SValBuilder &svalBuilder = C.getSValBuilder();
532 BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
533
534 QualType sizeTy = svalBuilder.getContext().getSizeType();
535 const llvm::APSInt &maxValInt = BVF.getMaxValue(sizeTy);
536 NonLoc maxVal = svalBuilder.makeIntVal(maxValInt);
537
Anna Zakse3d250e2011-12-11 18:43:40 +0000538 SVal maxMinusRight;
539 if (isa<nonloc::ConcreteInt>(right)) {
540 maxMinusRight = svalBuilder.evalBinOpNN(state, BO_Sub, maxVal, right,
541 sizeTy);
542 } else {
Jordy Rosed5af0e12011-06-15 05:52:56 +0000543 // Try switching the operands. (The order of these two assignments is
544 // important!)
545 maxMinusRight = svalBuilder.evalBinOpNN(state, BO_Sub, maxVal, left,
546 sizeTy);
547 left = right;
548 }
549
550 if (NonLoc *maxMinusRightNL = dyn_cast<NonLoc>(&maxMinusRight)) {
551 QualType cmpTy = svalBuilder.getConditionType();
552 // If left > max - right, we have an overflow.
553 SVal willOverflow = svalBuilder.evalBinOpNN(state, BO_GT, left,
554 *maxMinusRightNL, cmpTy);
555
Ted Kremenek8bef8232012-01-26 21:29:00 +0000556 ProgramStateRef stateOverflow, stateOkay;
Jordy Rosed5af0e12011-06-15 05:52:56 +0000557 llvm::tie(stateOverflow, stateOkay) =
558 state->assume(cast<DefinedOrUnknownSVal>(willOverflow));
559
560 if (stateOverflow && !stateOkay) {
561 // We have an overflow. Emit a bug report.
562 ExplodedNode *N = C.generateSink(stateOverflow);
563 if (!N)
564 return NULL;
565
566 if (!BT_AdditionOverflow)
567 BT_AdditionOverflow.reset(new BuiltinBug("API",
568 "Sum of expressions causes overflow"));
569
Jordy Rosed5af0e12011-06-15 05:52:56 +0000570 // This isn't a great error message, but this should never occur in real
571 // code anyway -- you'd have to create a buffer longer than a size_t can
572 // represent, which is sort of a contradiction.
Jordy Rose8cc24912011-06-20 03:51:53 +0000573 const char *warning =
574 "This expression will create a string whose length is too big to "
575 "be represented as a size_t";
Jordy Rosed5af0e12011-06-15 05:52:56 +0000576
577 // Generate a report for this bug.
Jordy Rose8cc24912011-06-20 03:51:53 +0000578 BugReport *report = new BugReport(*BT_AdditionOverflow, warning, N);
Jordy Rosed5af0e12011-06-15 05:52:56 +0000579 C.EmitReport(report);
580
581 return NULL;
582 }
583
584 // From now on, assume an overflow didn't occur.
585 assert(stateOkay);
586 state = stateOkay;
587 }
588
589 return state;
590}
591
Ted Kremenek8bef8232012-01-26 21:29:00 +0000592ProgramStateRef CStringChecker::setCStringLength(ProgramStateRef state,
Jordy Rosee64f3112010-08-16 07:51:42 +0000593 const MemRegion *MR,
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000594 SVal strLength) {
595 assert(!strLength.isUndef() && "Attempt to set an undefined string length");
Jordy Rosee64f3112010-08-16 07:51:42 +0000596
597 MR = MR->StripCasts();
598
599 switch (MR->getKind()) {
600 case MemRegion::StringRegionKind:
601 // FIXME: This can happen if we strcpy() into a string region. This is
602 // undefined [C99 6.4.5p6], but we should still warn about it.
603 return state;
604
605 case MemRegion::SymbolicRegionKind:
606 case MemRegion::AllocaRegionKind:
607 case MemRegion::VarRegionKind:
608 case MemRegion::FieldRegionKind:
609 case MemRegion::ObjCIvarRegionKind:
Jordy Rose210c05b2011-06-15 05:14:03 +0000610 // These are the types we can currently track string lengths for.
611 break;
Jordy Rosee64f3112010-08-16 07:51:42 +0000612
613 case MemRegion::ElementRegionKind:
614 // FIXME: Handle element regions by upper-bounding the parent region's
615 // string length.
616 return state;
617
618 default:
619 // Other regions (mostly non-data) can't have a reliable C string length.
620 // For now, just ignore the change.
621 // FIXME: These are rare but not impossible. We should output some kind of
622 // warning for things like strcpy((char[]){'a', 0}, "b");
623 return state;
624 }
Jordy Rose210c05b2011-06-15 05:14:03 +0000625
626 if (strLength.isUnknown())
627 return state->remove<CStringLength>(MR);
628
629 return state->set<CStringLength>(MR, strLength);
Jordy Rosee64f3112010-08-16 07:51:42 +0000630}
631
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000632SVal CStringChecker::getCStringLengthForRegion(CheckerContext &C,
Ted Kremenek8bef8232012-01-26 21:29:00 +0000633 ProgramStateRef &state,
Jordy Rosea5261542010-08-14 21:02:52 +0000634 const Expr *Ex,
Jordy Rosed5af0e12011-06-15 05:52:56 +0000635 const MemRegion *MR,
636 bool hypothetical) {
637 if (!hypothetical) {
638 // If there's a recorded length, go ahead and return it.
639 const SVal *Recorded = state->get<CStringLength>(MR);
640 if (Recorded)
641 return *Recorded;
642 }
Jordy Rosea5261542010-08-14 21:02:52 +0000643
644 // Otherwise, get a new symbol and update the state.
Anna Zaks5d0ea6d2011-10-04 20:43:05 +0000645 unsigned Count = C.getCurrentBlockCount();
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000646 SValBuilder &svalBuilder = C.getSValBuilder();
647 QualType sizeTy = svalBuilder.getContext().getSizeType();
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +0000648 SVal strLength = svalBuilder.getMetadataSymbolVal(CStringChecker::getTag(),
649 MR, Ex, sizeTy, Count);
Jordy Rosed5af0e12011-06-15 05:52:56 +0000650
651 if (!hypothetical)
652 state = state->set<CStringLength>(MR, strLength);
653
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000654 return strLength;
Jordy Rosea5261542010-08-14 21:02:52 +0000655}
656
Ted Kremenek8bef8232012-01-26 21:29:00 +0000657SVal CStringChecker::getCStringLength(CheckerContext &C, ProgramStateRef &state,
Jordy Rosed5af0e12011-06-15 05:52:56 +0000658 const Expr *Ex, SVal Buf,
659 bool hypothetical) const {
Jordy Rose19c5dd12010-07-27 01:37:31 +0000660 const MemRegion *MR = Buf.getAsRegion();
661 if (!MR) {
662 // If we can't get a region, see if it's something we /know/ isn't a
663 // C string. In the context of locations, the only time we can issue such
664 // a warning is for labels.
665 if (loc::GotoLabel *Label = dyn_cast<loc::GotoLabel>(&Buf)) {
Anna Zaks0bd6b112011-10-26 21:06:34 +0000666 if (ExplodedNode *N = C.addTransition(state)) {
Jordy Rose19c5dd12010-07-27 01:37:31 +0000667 if (!BT_NotCString)
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +0000668 BT_NotCString.reset(new BuiltinBug("API",
669 "Argument is not a null-terminated string."));
Jordy Rose19c5dd12010-07-27 01:37:31 +0000670
671 llvm::SmallString<120> buf;
672 llvm::raw_svector_ostream os(buf);
Jordy Rose9e49d9f2011-06-20 02:06:40 +0000673 assert(CurrentFunctionDescription);
674 os << "Argument to " << CurrentFunctionDescription
675 << " is the address of the label '" << Label->getLabel()->getName()
Jordy Rose19c5dd12010-07-27 01:37:31 +0000676 << "', which is not a null-terminated string";
677
678 // Generate a report for this bug.
Anna Zakse172e8b2011-08-17 23:00:25 +0000679 BugReport *report = new BugReport(*BT_NotCString,
Jordy Rose19c5dd12010-07-27 01:37:31 +0000680 os.str(), N);
681
682 report->addRange(Ex->getSourceRange());
683 C.EmitReport(report);
684 }
685
686 return UndefinedVal();
687 }
688
Jordy Rosea5261542010-08-14 21:02:52 +0000689 // If it's not a region and not a label, give up.
690 return UnknownVal();
Jordy Rose19c5dd12010-07-27 01:37:31 +0000691 }
692
Jordy Rosea5261542010-08-14 21:02:52 +0000693 // If we have a region, strip casts from it and see if we can figure out
694 // its length. For anything we can't figure out, just return UnknownVal.
695 MR = MR->StripCasts();
696
697 switch (MR->getKind()) {
698 case MemRegion::StringRegionKind: {
699 // Modifying the contents of string regions is undefined [C99 6.4.5p6],
700 // so we can assume that the byte length is the correct C string length.
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000701 SValBuilder &svalBuilder = C.getSValBuilder();
702 QualType sizeTy = svalBuilder.getContext().getSizeType();
703 const StringLiteral *strLit = cast<StringRegion>(MR)->getStringLiteral();
704 return svalBuilder.makeIntVal(strLit->getByteLength(), sizeTy);
Jordy Rosea5261542010-08-14 21:02:52 +0000705 }
706 case MemRegion::SymbolicRegionKind:
707 case MemRegion::AllocaRegionKind:
708 case MemRegion::VarRegionKind:
709 case MemRegion::FieldRegionKind:
710 case MemRegion::ObjCIvarRegionKind:
Jordy Rosed5af0e12011-06-15 05:52:56 +0000711 return getCStringLengthForRegion(C, state, Ex, MR, hypothetical);
Jordy Rosea5261542010-08-14 21:02:52 +0000712 case MemRegion::CompoundLiteralRegionKind:
713 // FIXME: Can we track this? Is it necessary?
714 return UnknownVal();
715 case MemRegion::ElementRegionKind:
716 // FIXME: How can we handle this? It's not good enough to subtract the
717 // offset from the base string length; consider "123\x00567" and &a[5].
718 return UnknownVal();
719 default:
720 // Other regions (mostly non-data) can't have a reliable C string length.
721 // In this case, an error is emitted and UndefinedVal is returned.
722 // The caller should always be prepared to handle this case.
Anna Zaks0bd6b112011-10-26 21:06:34 +0000723 if (ExplodedNode *N = C.addTransition(state)) {
Jordy Rosea5261542010-08-14 21:02:52 +0000724 if (!BT_NotCString)
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +0000725 BT_NotCString.reset(new BuiltinBug("API",
726 "Argument is not a null-terminated string."));
Jordy Rosea5261542010-08-14 21:02:52 +0000727
728 llvm::SmallString<120> buf;
729 llvm::raw_svector_ostream os(buf);
730
Jordy Rose9e49d9f2011-06-20 02:06:40 +0000731 assert(CurrentFunctionDescription);
732 os << "Argument to " << CurrentFunctionDescription << " is ";
Jordy Rosea5261542010-08-14 21:02:52 +0000733
734 if (SummarizeRegion(os, C.getASTContext(), MR))
735 os << ", which is not a null-terminated string";
736 else
737 os << "not a null-terminated string";
738
739 // Generate a report for this bug.
Anna Zakse172e8b2011-08-17 23:00:25 +0000740 BugReport *report = new BugReport(*BT_NotCString,
Jordy Rosea5261542010-08-14 21:02:52 +0000741 os.str(), N);
742
743 report->addRange(Ex->getSourceRange());
744 C.EmitReport(report);
745 }
746
747 return UndefinedVal();
748 }
Jordy Rose19c5dd12010-07-27 01:37:31 +0000749}
750
Lenny Maiorani318dd922011-04-12 17:08:43 +0000751const StringLiteral *CStringChecker::getCStringLiteral(CheckerContext &C,
Ted Kremenek8bef8232012-01-26 21:29:00 +0000752 ProgramStateRef &state, const Expr *expr, SVal val) const {
Lenny Maiorani318dd922011-04-12 17:08:43 +0000753
754 // Get the memory region pointed to by the val.
755 const MemRegion *bufRegion = val.getAsRegion();
756 if (!bufRegion)
757 return NULL;
758
759 // Strip casts off the memory region.
760 bufRegion = bufRegion->StripCasts();
761
762 // Cast the memory region to a string region.
763 const StringRegion *strRegion= dyn_cast<StringRegion>(bufRegion);
764 if (!strRegion)
765 return NULL;
766
767 // Return the actual string in the string region.
768 return strRegion->getStringLiteral();
769}
770
Ted Kremenek8bef8232012-01-26 21:29:00 +0000771ProgramStateRef CStringChecker::InvalidateBuffer(CheckerContext &C,
772 ProgramStateRef state,
Jordy Rosee64f3112010-08-16 07:51:42 +0000773 const Expr *E, SVal V) {
774 Loc *L = dyn_cast<Loc>(&V);
775 if (!L)
776 return state;
777
778 // FIXME: This is a simplified version of what's in CFRefCount.cpp -- it makes
779 // some assumptions about the value that CFRefCount can't. Even so, it should
780 // probably be refactored.
781 if (loc::MemRegionVal* MR = dyn_cast<loc::MemRegionVal>(L)) {
782 const MemRegion *R = MR->getRegion()->StripCasts();
783
784 // Are we dealing with an ElementRegion? If so, we should be invalidating
785 // the super-region.
786 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
787 R = ER->getSuperRegion();
788 // FIXME: What about layers of ElementRegions?
789 }
790
791 // Invalidate this region.
Anna Zaks5d0ea6d2011-10-04 20:43:05 +0000792 unsigned Count = C.getCurrentBlockCount();
Jordy Rose537716a2011-08-27 22:51:26 +0000793 return state->invalidateRegions(R, E, Count);
Jordy Rosee64f3112010-08-16 07:51:42 +0000794 }
795
796 // If we have a non-region value by chance, just remove the binding.
797 // FIXME: is this necessary or correct? This handles the non-Region
798 // cases. Is it ever valid to store to these?
799 return state->unbindLoc(*L);
800}
801
Ted Kremenek9c378f72011-08-12 23:37:29 +0000802bool CStringChecker::SummarizeRegion(raw_ostream &os, ASTContext &Ctx,
Jordy Rose19c5dd12010-07-27 01:37:31 +0000803 const MemRegion *MR) {
Ted Kremenek96979342011-08-12 20:02:48 +0000804 const TypedValueRegion *TVR = dyn_cast<TypedValueRegion>(MR);
Jordy Rose19c5dd12010-07-27 01:37:31 +0000805
Jordy Rose096aef92011-08-12 21:41:07 +0000806 switch (MR->getKind()) {
Jordy Rose19c5dd12010-07-27 01:37:31 +0000807 case MemRegion::FunctionTextRegionKind: {
Jordy Rose096aef92011-08-12 21:41:07 +0000808 const FunctionDecl *FD = cast<FunctionTextRegion>(MR)->getDecl();
Jordy Rose19c5dd12010-07-27 01:37:31 +0000809 if (FD)
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000810 os << "the address of the function '" << *FD << '\'';
Jordy Rose19c5dd12010-07-27 01:37:31 +0000811 else
812 os << "the address of a function";
813 return true;
814 }
815 case MemRegion::BlockTextRegionKind:
816 os << "block text";
817 return true;
818 case MemRegion::BlockDataRegionKind:
819 os << "a block";
820 return true;
821 case MemRegion::CXXThisRegionKind:
Zhongxing Xu02fe28c2010-11-26 08:52:48 +0000822 case MemRegion::CXXTempObjectRegionKind:
Ted Kremenek96979342011-08-12 20:02:48 +0000823 os << "a C++ temp object of type " << TVR->getValueType().getAsString();
Jordy Rose19c5dd12010-07-27 01:37:31 +0000824 return true;
825 case MemRegion::VarRegionKind:
Ted Kremenek96979342011-08-12 20:02:48 +0000826 os << "a variable of type" << TVR->getValueType().getAsString();
Jordy Rose19c5dd12010-07-27 01:37:31 +0000827 return true;
828 case MemRegion::FieldRegionKind:
Ted Kremenek96979342011-08-12 20:02:48 +0000829 os << "a field of type " << TVR->getValueType().getAsString();
Jordy Rose19c5dd12010-07-27 01:37:31 +0000830 return true;
831 case MemRegion::ObjCIvarRegionKind:
Ted Kremenek96979342011-08-12 20:02:48 +0000832 os << "an instance variable of type " << TVR->getValueType().getAsString();
Jordy Rose19c5dd12010-07-27 01:37:31 +0000833 return true;
834 default:
835 return false;
836 }
837}
838
Jordy Rosed325ffb2010-07-08 23:57:29 +0000839//===----------------------------------------------------------------------===//
Ted Kremenek9c149532010-12-01 21:57:22 +0000840// evaluation of individual function calls.
Jordy Rosed325ffb2010-07-08 23:57:29 +0000841//===----------------------------------------------------------------------===//
842
Lenny Maioranib8b875b2011-03-31 21:36:53 +0000843void CStringChecker::evalCopyCommon(CheckerContext &C,
844 const CallExpr *CE,
Ted Kremenek8bef8232012-01-26 21:29:00 +0000845 ProgramStateRef state,
Jordy Rosed325ffb2010-07-08 23:57:29 +0000846 const Expr *Size, const Expr *Dest,
Lenny Maioranib8b875b2011-03-31 21:36:53 +0000847 const Expr *Source, bool Restricted,
848 bool IsMempcpy) const {
Jordy Rose9e49d9f2011-06-20 02:06:40 +0000849 CurrentFunctionDescription = "memory copy function";
850
Jordy Rosed325ffb2010-07-08 23:57:29 +0000851 // See if the size argument is zero.
Ted Kremenek5eca4822012-01-06 22:09:28 +0000852 const LocationContext *LCtx = C.getLocationContext();
853 SVal sizeVal = state->getSVal(Size, LCtx);
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000854 QualType sizeTy = Size->getType();
Jordy Rosed325ffb2010-07-08 23:57:29 +0000855
Ted Kremenek8bef8232012-01-26 21:29:00 +0000856 ProgramStateRef stateZeroSize, stateNonZeroSize;
Jordy Rose1e022412011-06-16 05:51:02 +0000857 llvm::tie(stateZeroSize, stateNonZeroSize) =
858 assumeZero(C, state, sizeVal, sizeTy);
Jordy Rosed325ffb2010-07-08 23:57:29 +0000859
Lenny Maioranib8b875b2011-03-31 21:36:53 +0000860 // Get the value of the Dest.
Ted Kremenek5eca4822012-01-06 22:09:28 +0000861 SVal destVal = state->getSVal(Dest, LCtx);
Lenny Maioranib8b875b2011-03-31 21:36:53 +0000862
863 // If the size is zero, there won't be any actual memory access, so
864 // just bind the return value to the destination buffer and return.
865 if (stateZeroSize) {
Ted Kremenek5eca4822012-01-06 22:09:28 +0000866 stateZeroSize = stateZeroSize->BindExpr(CE, LCtx, destVal);
Anna Zaks0bd6b112011-10-26 21:06:34 +0000867 C.addTransition(stateZeroSize);
Lenny Maioranib8b875b2011-03-31 21:36:53 +0000868 }
Jordy Rosed325ffb2010-07-08 23:57:29 +0000869
870 // If the size can be nonzero, we have to check the other arguments.
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000871 if (stateNonZeroSize) {
Jordy Rose22d27172011-06-04 00:04:22 +0000872 state = stateNonZeroSize;
Lenny Maioranib8b875b2011-03-31 21:36:53 +0000873
874 // Ensure the destination is not null. If it is NULL there will be a
875 // NULL pointer dereference.
876 state = checkNonNull(C, state, Dest, destVal);
877 if (!state)
878 return;
879
880 // Get the value of the Src.
Ted Kremenek5eca4822012-01-06 22:09:28 +0000881 SVal srcVal = state->getSVal(Source, LCtx);
Lenny Maioranib8b875b2011-03-31 21:36:53 +0000882
883 // Ensure the source is not null. If it is NULL there will be a
884 // NULL pointer dereference.
885 state = checkNonNull(C, state, Source, srcVal);
886 if (!state)
887 return;
888
Jordy Rose7182b962011-06-04 01:50:25 +0000889 // Ensure the accesses are valid and that the buffers do not overlap.
Jordy Rose9e49d9f2011-06-20 02:06:40 +0000890 const char * const writeWarning =
891 "Memory copy function overflows destination buffer";
Jordy Rosee64f3112010-08-16 07:51:42 +0000892 state = CheckBufferAccess(C, state, Size, Dest, Source,
Jordy Rose9e49d9f2011-06-20 02:06:40 +0000893 writeWarning, /* sourceWarning = */ NULL);
Jordy Rosed325ffb2010-07-08 23:57:29 +0000894 if (Restricted)
895 state = CheckOverlap(C, state, Size, Dest, Source);
Jordy Rosee64f3112010-08-16 07:51:42 +0000896
Jordy Rose7182b962011-06-04 01:50:25 +0000897 if (!state)
898 return;
Lenny Maioranib8b875b2011-03-31 21:36:53 +0000899
Jordy Rose7182b962011-06-04 01:50:25 +0000900 // If this is mempcpy, get the byte after the last byte copied and
901 // bind the expr.
902 if (IsMempcpy) {
903 loc::MemRegionVal *destRegVal = dyn_cast<loc::MemRegionVal>(&destVal);
904 assert(destRegVal && "Destination should be a known MemRegionVal here");
905
906 // Get the length to copy.
907 NonLoc *lenValNonLoc = dyn_cast<NonLoc>(&sizeVal);
908
909 if (lenValNonLoc) {
910 // Get the byte after the last byte copied.
911 SVal lastElement = C.getSValBuilder().evalBinOpLN(state, BO_Add,
912 *destRegVal,
913 *lenValNonLoc,
914 Dest->getType());
915
916 // The byte after the last byte copied is the return value.
Ted Kremenek5eca4822012-01-06 22:09:28 +0000917 state = state->BindExpr(CE, LCtx, lastElement);
Jordy Rose3f8bb2f2011-06-04 01:47:27 +0000918 } else {
Jordy Rose7182b962011-06-04 01:50:25 +0000919 // If we don't know how much we copied, we can at least
920 // conjure a return value for later.
Anna Zaks5d0ea6d2011-10-04 20:43:05 +0000921 unsigned Count = C.getCurrentBlockCount();
Jordy Rose7182b962011-06-04 01:50:25 +0000922 SVal result =
923 C.getSValBuilder().getConjuredSymbolVal(NULL, CE, Count);
Ted Kremenek5eca4822012-01-06 22:09:28 +0000924 state = state->BindExpr(CE, LCtx, result);
Lenny Maioranib8b875b2011-03-31 21:36:53 +0000925 }
926
Jordy Rose7182b962011-06-04 01:50:25 +0000927 } else {
928 // All other copies return the destination buffer.
929 // (Well, bcopy() has a void return type, but this won't hurt.)
Ted Kremenek5eca4822012-01-06 22:09:28 +0000930 state = state->BindExpr(CE, LCtx, destVal);
Jordy Rosee64f3112010-08-16 07:51:42 +0000931 }
Jordy Rose7182b962011-06-04 01:50:25 +0000932
933 // Invalidate the destination.
934 // FIXME: Even if we can't perfectly model the copy, we should see if we
935 // can use LazyCompoundVals to copy the source values into the destination.
936 // This would probably remove any existing bindings past the end of the
937 // copied region, but that's still an improvement over blank invalidation.
Ted Kremenek5eca4822012-01-06 22:09:28 +0000938 state = InvalidateBuffer(C, state, Dest,
939 state->getSVal(Dest, C.getLocationContext()));
Anna Zaks0bd6b112011-10-26 21:06:34 +0000940 C.addTransition(state);
Jordy Rosed325ffb2010-07-08 23:57:29 +0000941 }
942}
943
944
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +0000945void CStringChecker::evalMemcpy(CheckerContext &C, const CallExpr *CE) const {
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000946 // void *memcpy(void *restrict dst, const void *restrict src, size_t n);
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000947 // The return value is the address of the destination buffer.
Jordy Rosed325ffb2010-07-08 23:57:29 +0000948 const Expr *Dest = CE->getArg(0);
Ted Kremenek8bef8232012-01-26 21:29:00 +0000949 ProgramStateRef state = C.getState();
Jordy Rose3f8bb2f2011-06-04 01:47:27 +0000950
Lenny Maioranib8b875b2011-03-31 21:36:53 +0000951 evalCopyCommon(C, CE, state, CE->getArg(2), Dest, CE->getArg(1), true);
952}
953
954void CStringChecker::evalMempcpy(CheckerContext &C, const CallExpr *CE) const {
955 // void *mempcpy(void *restrict dst, const void *restrict src, size_t n);
956 // The return value is a pointer to the byte following the last written byte.
957 const Expr *Dest = CE->getArg(0);
Ted Kremenek8bef8232012-01-26 21:29:00 +0000958 ProgramStateRef state = C.getState();
Lenny Maioranib8b875b2011-03-31 21:36:53 +0000959
960 evalCopyCommon(C, CE, state, CE->getArg(2), Dest, CE->getArg(1), true, true);
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000961}
962
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +0000963void CStringChecker::evalMemmove(CheckerContext &C, const CallExpr *CE) const {
Jordy Rosed325ffb2010-07-08 23:57:29 +0000964 // void *memmove(void *dst, const void *src, size_t n);
965 // The return value is the address of the destination buffer.
966 const Expr *Dest = CE->getArg(0);
Ted Kremenek8bef8232012-01-26 21:29:00 +0000967 ProgramStateRef state = C.getState();
Jordy Rose3f8bb2f2011-06-04 01:47:27 +0000968
Lenny Maioranib8b875b2011-03-31 21:36:53 +0000969 evalCopyCommon(C, CE, state, CE->getArg(2), Dest, CE->getArg(1));
Jordy Rosed325ffb2010-07-08 23:57:29 +0000970}
971
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +0000972void CStringChecker::evalBcopy(CheckerContext &C, const CallExpr *CE) const {
Jordy Rosed325ffb2010-07-08 23:57:29 +0000973 // void bcopy(const void *src, void *dst, size_t n);
Lenny Maioranib8b875b2011-03-31 21:36:53 +0000974 evalCopyCommon(C, CE, C.getState(),
975 CE->getArg(2), CE->getArg(1), CE->getArg(0));
Jordy Rosed325ffb2010-07-08 23:57:29 +0000976}
977
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +0000978void CStringChecker::evalMemcmp(CheckerContext &C, const CallExpr *CE) const {
Jordy Rosebc56d1f2010-07-07 08:15:01 +0000979 // int memcmp(const void *s1, const void *s2, size_t n);
Jordy Rose9e49d9f2011-06-20 02:06:40 +0000980 CurrentFunctionDescription = "memory comparison function";
981
Jordy Rosebc56d1f2010-07-07 08:15:01 +0000982 const Expr *Left = CE->getArg(0);
983 const Expr *Right = CE->getArg(1);
984 const Expr *Size = CE->getArg(2);
985
Ted Kremenek8bef8232012-01-26 21:29:00 +0000986 ProgramStateRef state = C.getState();
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000987 SValBuilder &svalBuilder = C.getSValBuilder();
Jordy Rosebc56d1f2010-07-07 08:15:01 +0000988
Jordy Rosed325ffb2010-07-08 23:57:29 +0000989 // See if the size argument is zero.
Ted Kremenek5eca4822012-01-06 22:09:28 +0000990 const LocationContext *LCtx = C.getLocationContext();
991 SVal sizeVal = state->getSVal(Size, LCtx);
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000992 QualType sizeTy = Size->getType();
Jordy Rosebc56d1f2010-07-07 08:15:01 +0000993
Ted Kremenek8bef8232012-01-26 21:29:00 +0000994 ProgramStateRef stateZeroSize, stateNonZeroSize;
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000995 llvm::tie(stateZeroSize, stateNonZeroSize) =
996 assumeZero(C, state, sizeVal, sizeTy);
Jordy Rosebc56d1f2010-07-07 08:15:01 +0000997
Jordy Rosed325ffb2010-07-08 23:57:29 +0000998 // If the size can be zero, the result will be 0 in that case, and we don't
999 // have to check either of the buffers.
Ted Kremenekc8413fd2010-12-02 07:49:45 +00001000 if (stateZeroSize) {
1001 state = stateZeroSize;
Ted Kremenek5eca4822012-01-06 22:09:28 +00001002 state = state->BindExpr(CE, LCtx,
1003 svalBuilder.makeZeroVal(CE->getType()));
Anna Zaks0bd6b112011-10-26 21:06:34 +00001004 C.addTransition(state);
Jordy Rosebc56d1f2010-07-07 08:15:01 +00001005 }
1006
Jordy Rosed325ffb2010-07-08 23:57:29 +00001007 // If the size can be nonzero, we have to check the other arguments.
Ted Kremenekc8413fd2010-12-02 07:49:45 +00001008 if (stateNonZeroSize) {
1009 state = stateNonZeroSize;
Jordy Rosed325ffb2010-07-08 23:57:29 +00001010 // If we know the two buffers are the same, we know the result is 0.
1011 // First, get the two buffers' addresses. Another checker will have already
1012 // made sure they're not undefined.
Ted Kremenek5eca4822012-01-06 22:09:28 +00001013 DefinedOrUnknownSVal LV =
1014 cast<DefinedOrUnknownSVal>(state->getSVal(Left, LCtx));
1015 DefinedOrUnknownSVal RV =
1016 cast<DefinedOrUnknownSVal>(state->getSVal(Right, LCtx));
Jordy Rosebc56d1f2010-07-07 08:15:01 +00001017
Jordy Rosed325ffb2010-07-08 23:57:29 +00001018 // See if they are the same.
Ted Kremenekc8413fd2010-12-02 07:49:45 +00001019 DefinedOrUnknownSVal SameBuf = svalBuilder.evalEQ(state, LV, RV);
Ted Kremenek8bef8232012-01-26 21:29:00 +00001020 ProgramStateRef StSameBuf, StNotSameBuf;
Ted Kremenek28f47b92010-12-01 22:16:56 +00001021 llvm::tie(StSameBuf, StNotSameBuf) = state->assume(SameBuf);
Jordy Rosed325ffb2010-07-08 23:57:29 +00001022
Jordy Rose1e022412011-06-16 05:51:02 +00001023 // If the two arguments might be the same buffer, we know the result is 0,
Jordy Rosed325ffb2010-07-08 23:57:29 +00001024 // and we only need to check one size.
1025 if (StSameBuf) {
1026 state = StSameBuf;
1027 state = CheckBufferAccess(C, state, Size, Left);
1028 if (state) {
Ted Kremenek5eca4822012-01-06 22:09:28 +00001029 state = StSameBuf->BindExpr(CE, LCtx,
1030 svalBuilder.makeZeroVal(CE->getType()));
Anna Zaks0bd6b112011-10-26 21:06:34 +00001031 C.addTransition(state);
Jordy Rosed325ffb2010-07-08 23:57:29 +00001032 }
1033 }
1034
1035 // If the two arguments might be different buffers, we have to check the
1036 // size of both of them.
1037 if (StNotSameBuf) {
1038 state = StNotSameBuf;
1039 state = CheckBufferAccess(C, state, Size, Left, Right);
1040 if (state) {
1041 // The return value is the comparison result, which we don't know.
Anna Zaks5d0ea6d2011-10-04 20:43:05 +00001042 unsigned Count = C.getCurrentBlockCount();
Ted Kremenekc8413fd2010-12-02 07:49:45 +00001043 SVal CmpV = svalBuilder.getConjuredSymbolVal(NULL, CE, Count);
Ted Kremenek5eca4822012-01-06 22:09:28 +00001044 state = state->BindExpr(CE, LCtx, CmpV);
Anna Zaks0bd6b112011-10-26 21:06:34 +00001045 C.addTransition(state);
Jordy Rosed325ffb2010-07-08 23:57:29 +00001046 }
1047 }
1048 }
Jordy Rosebc56d1f2010-07-07 08:15:01 +00001049}
1050
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +00001051void CStringChecker::evalstrLength(CheckerContext &C,
1052 const CallExpr *CE) const {
Jordy Rose19c5dd12010-07-27 01:37:31 +00001053 // size_t strlen(const char *s);
Ted Kremenekbe4242c2011-02-22 04:55:05 +00001054 evalstrLengthCommon(C, CE, /* IsStrnlen = */ false);
1055}
1056
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +00001057void CStringChecker::evalstrnLength(CheckerContext &C,
1058 const CallExpr *CE) const {
Ted Kremenekbe4242c2011-02-22 04:55:05 +00001059 // size_t strnlen(const char *s, size_t maxlen);
1060 evalstrLengthCommon(C, CE, /* IsStrnlen = */ true);
1061}
1062
1063void CStringChecker::evalstrLengthCommon(CheckerContext &C, const CallExpr *CE,
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +00001064 bool IsStrnlen) const {
Jordy Rose9e49d9f2011-06-20 02:06:40 +00001065 CurrentFunctionDescription = "string length function";
Ted Kremenek8bef8232012-01-26 21:29:00 +00001066 ProgramStateRef state = C.getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +00001067 const LocationContext *LCtx = C.getLocationContext();
Jordy Rose793bff32011-06-14 01:15:31 +00001068
1069 if (IsStrnlen) {
1070 const Expr *maxlenExpr = CE->getArg(1);
Ted Kremenek5eca4822012-01-06 22:09:28 +00001071 SVal maxlenVal = state->getSVal(maxlenExpr, LCtx);
Jordy Rose793bff32011-06-14 01:15:31 +00001072
Ted Kremenek8bef8232012-01-26 21:29:00 +00001073 ProgramStateRef stateZeroSize, stateNonZeroSize;
Jordy Rose793bff32011-06-14 01:15:31 +00001074 llvm::tie(stateZeroSize, stateNonZeroSize) =
1075 assumeZero(C, state, maxlenVal, maxlenExpr->getType());
1076
1077 // If the size can be zero, the result will be 0 in that case, and we don't
1078 // have to check the string itself.
1079 if (stateZeroSize) {
1080 SVal zero = C.getSValBuilder().makeZeroVal(CE->getType());
Ted Kremenek5eca4822012-01-06 22:09:28 +00001081 stateZeroSize = stateZeroSize->BindExpr(CE, LCtx, zero);
Anna Zaks0bd6b112011-10-26 21:06:34 +00001082 C.addTransition(stateZeroSize);
Jordy Rose793bff32011-06-14 01:15:31 +00001083 }
1084
1085 // If the size is GUARANTEED to be zero, we're done!
1086 if (!stateNonZeroSize)
1087 return;
1088
1089 // Otherwise, record the assumption that the size is nonzero.
1090 state = stateNonZeroSize;
1091 }
1092
1093 // Check that the string argument is non-null.
Jordy Rose19c5dd12010-07-27 01:37:31 +00001094 const Expr *Arg = CE->getArg(0);
Ted Kremenek5eca4822012-01-06 22:09:28 +00001095 SVal ArgVal = state->getSVal(Arg, LCtx);
Jordy Rose19c5dd12010-07-27 01:37:31 +00001096
Ted Kremenekc8413fd2010-12-02 07:49:45 +00001097 state = checkNonNull(C, state, Arg, ArgVal);
Jordy Rose19c5dd12010-07-27 01:37:31 +00001098
Jordy Rosebd32bee2011-06-14 01:26:48 +00001099 if (!state)
1100 return;
Jordy Rosea5261542010-08-14 21:02:52 +00001101
Jordy Rosebd32bee2011-06-14 01:26:48 +00001102 SVal strLength = getCStringLength(C, state, Arg, ArgVal);
Jordy Rosea5261542010-08-14 21:02:52 +00001103
Jordy Rosebd32bee2011-06-14 01:26:48 +00001104 // If the argument isn't a valid C string, there's no valid state to
1105 // transition to.
1106 if (strLength.isUndef())
1107 return;
Jordy Rose793bff32011-06-14 01:15:31 +00001108
Jordy Rosebd32bee2011-06-14 01:26:48 +00001109 DefinedOrUnknownSVal result = UnknownVal();
Jordy Rose793bff32011-06-14 01:15:31 +00001110
Jordy Rosebd32bee2011-06-14 01:26:48 +00001111 // If the check is for strnlen() then bind the return value to no more than
1112 // the maxlen value.
1113 if (IsStrnlen) {
Jordy Roseee2fde12011-06-16 05:56:50 +00001114 QualType cmpTy = C.getSValBuilder().getConditionType();
Jordy Rose793bff32011-06-14 01:15:31 +00001115
Jordy Rosebd32bee2011-06-14 01:26:48 +00001116 // It's a little unfortunate to be getting this again,
1117 // but it's not that expensive...
1118 const Expr *maxlenExpr = CE->getArg(1);
Ted Kremenek5eca4822012-01-06 22:09:28 +00001119 SVal maxlenVal = state->getSVal(maxlenExpr, LCtx);
Ted Kremenekbe4242c2011-02-22 04:55:05 +00001120
Jordy Rosebd32bee2011-06-14 01:26:48 +00001121 NonLoc *strLengthNL = dyn_cast<NonLoc>(&strLength);
1122 NonLoc *maxlenValNL = dyn_cast<NonLoc>(&maxlenVal);
Ted Kremenekbe4242c2011-02-22 04:55:05 +00001123
Jordy Rosebd32bee2011-06-14 01:26:48 +00001124 if (strLengthNL && maxlenValNL) {
Ted Kremenek8bef8232012-01-26 21:29:00 +00001125 ProgramStateRef stateStringTooLong, stateStringNotTooLong;
Jordy Rose793bff32011-06-14 01:15:31 +00001126
Jordy Rosebd32bee2011-06-14 01:26:48 +00001127 // Check if the strLength is greater than the maxlen.
1128 llvm::tie(stateStringTooLong, stateStringNotTooLong) =
1129 state->assume(cast<DefinedOrUnknownSVal>
1130 (C.getSValBuilder().evalBinOpNN(state, BO_GT,
1131 *strLengthNL,
1132 *maxlenValNL,
1133 cmpTy)));
Jordy Rose793bff32011-06-14 01:15:31 +00001134
Jordy Rosebd32bee2011-06-14 01:26:48 +00001135 if (stateStringTooLong && !stateStringNotTooLong) {
1136 // If the string is longer than maxlen, return maxlen.
1137 result = *maxlenValNL;
1138 } else if (stateStringNotTooLong && !stateStringTooLong) {
1139 // If the string is shorter than maxlen, return its length.
1140 result = *strLengthNL;
Ted Kremenekbe4242c2011-02-22 04:55:05 +00001141 }
1142 }
1143
Jordy Rosebd32bee2011-06-14 01:26:48 +00001144 if (result.isUnknown()) {
1145 // If we don't have enough information for a comparison, there's
1146 // no guarantee the full string length will actually be returned.
1147 // All we know is the return value is the min of the string length
1148 // and the limit. This is better than nothing.
Anna Zaks5d0ea6d2011-10-04 20:43:05 +00001149 unsigned Count = C.getCurrentBlockCount();
Jordy Rosebd32bee2011-06-14 01:26:48 +00001150 result = C.getSValBuilder().getConjuredSymbolVal(NULL, CE, Count);
1151 NonLoc *resultNL = cast<NonLoc>(&result);
1152
1153 if (strLengthNL) {
1154 state = state->assume(cast<DefinedOrUnknownSVal>
1155 (C.getSValBuilder().evalBinOpNN(state, BO_LE,
1156 *resultNL,
1157 *strLengthNL,
1158 cmpTy)), true);
1159 }
1160
1161 if (maxlenValNL) {
1162 state = state->assume(cast<DefinedOrUnknownSVal>
1163 (C.getSValBuilder().evalBinOpNN(state, BO_LE,
1164 *resultNL,
1165 *maxlenValNL,
1166 cmpTy)), true);
1167 }
1168 }
1169
1170 } else {
1171 // This is a plain strlen(), not strnlen().
1172 result = cast<DefinedOrUnknownSVal>(strLength);
1173
1174 // If we don't know the length of the string, conjure a return
1175 // value, so it can be used in constraints, at least.
1176 if (result.isUnknown()) {
Anna Zaks5d0ea6d2011-10-04 20:43:05 +00001177 unsigned Count = C.getCurrentBlockCount();
Jordy Rosebd32bee2011-06-14 01:26:48 +00001178 result = C.getSValBuilder().getConjuredSymbolVal(NULL, CE, Count);
1179 }
Jordy Rose19c5dd12010-07-27 01:37:31 +00001180 }
Jordy Rosebd32bee2011-06-14 01:26:48 +00001181
1182 // Bind the return value.
1183 assert(!result.isUnknown() && "Should have conjured a value by now");
Ted Kremenek5eca4822012-01-06 22:09:28 +00001184 state = state->BindExpr(CE, LCtx, result);
Anna Zaks0bd6b112011-10-26 21:06:34 +00001185 C.addTransition(state);
Jordy Rose19c5dd12010-07-27 01:37:31 +00001186}
1187
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +00001188void CStringChecker::evalStrcpy(CheckerContext &C, const CallExpr *CE) const {
Jordy Rosee64f3112010-08-16 07:51:42 +00001189 // char *strcpy(char *restrict dst, const char *restrict src);
Lenny Maiorani067bbd02011-04-09 15:12:58 +00001190 evalStrcpyCommon(C, CE,
1191 /* returnEnd = */ false,
1192 /* isBounded = */ false,
1193 /* isAppending = */ false);
Ted Kremenek0ef473f2011-02-22 04:58:34 +00001194}
1195
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +00001196void CStringChecker::evalStrncpy(CheckerContext &C, const CallExpr *CE) const {
Jordy Rosec1525862011-06-04 00:05:23 +00001197 // char *strncpy(char *restrict dst, const char *restrict src, size_t n);
Lenny Maiorani067bbd02011-04-09 15:12:58 +00001198 evalStrcpyCommon(C, CE,
1199 /* returnEnd = */ false,
1200 /* isBounded = */ true,
1201 /* isAppending = */ false);
Jordy Rosee64f3112010-08-16 07:51:42 +00001202}
1203
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +00001204void CStringChecker::evalStpcpy(CheckerContext &C, const CallExpr *CE) const {
Jordy Rosee64f3112010-08-16 07:51:42 +00001205 // char *stpcpy(char *restrict dst, const char *restrict src);
Lenny Maiorani067bbd02011-04-09 15:12:58 +00001206 evalStrcpyCommon(C, CE,
1207 /* returnEnd = */ true,
1208 /* isBounded = */ false,
1209 /* isAppending = */ false);
1210}
1211
1212void CStringChecker::evalStrcat(CheckerContext &C, const CallExpr *CE) const {
1213 //char *strcat(char *restrict s1, const char *restrict s2);
1214 evalStrcpyCommon(C, CE,
1215 /* returnEnd = */ false,
1216 /* isBounded = */ false,
1217 /* isAppending = */ true);
1218}
1219
1220void CStringChecker::evalStrncat(CheckerContext &C, const CallExpr *CE) const {
1221 //char *strncat(char *restrict s1, const char *restrict s2, size_t n);
1222 evalStrcpyCommon(C, CE,
1223 /* returnEnd = */ false,
1224 /* isBounded = */ true,
1225 /* isAppending = */ true);
Jordy Rosee64f3112010-08-16 07:51:42 +00001226}
1227
Ted Kremenek9c149532010-12-01 21:57:22 +00001228void CStringChecker::evalStrcpyCommon(CheckerContext &C, const CallExpr *CE,
Lenny Maiorani067bbd02011-04-09 15:12:58 +00001229 bool returnEnd, bool isBounded,
1230 bool isAppending) const {
Jordy Rose9e49d9f2011-06-20 02:06:40 +00001231 CurrentFunctionDescription = "string copy function";
Ted Kremenek8bef8232012-01-26 21:29:00 +00001232 ProgramStateRef state = C.getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +00001233 const LocationContext *LCtx = C.getLocationContext();
Jordy Rosee64f3112010-08-16 07:51:42 +00001234
Lenny Maiorani067bbd02011-04-09 15:12:58 +00001235 // Check that the destination is non-null.
Jordy Rosee64f3112010-08-16 07:51:42 +00001236 const Expr *Dst = CE->getArg(0);
Ted Kremenek5eca4822012-01-06 22:09:28 +00001237 SVal DstVal = state->getSVal(Dst, LCtx);
Jordy Rosee64f3112010-08-16 07:51:42 +00001238
Ted Kremenekc8413fd2010-12-02 07:49:45 +00001239 state = checkNonNull(C, state, Dst, DstVal);
Jordy Rosee64f3112010-08-16 07:51:42 +00001240 if (!state)
1241 return;
1242
1243 // Check that the source is non-null.
Ted Kremenekc8413fd2010-12-02 07:49:45 +00001244 const Expr *srcExpr = CE->getArg(1);
Ted Kremenek5eca4822012-01-06 22:09:28 +00001245 SVal srcVal = state->getSVal(srcExpr, LCtx);
Ted Kremenekc8413fd2010-12-02 07:49:45 +00001246 state = checkNonNull(C, state, srcExpr, srcVal);
Jordy Rosee64f3112010-08-16 07:51:42 +00001247 if (!state)
1248 return;
1249
1250 // Get the string length of the source.
Ted Kremenekc8413fd2010-12-02 07:49:45 +00001251 SVal strLength = getCStringLength(C, state, srcExpr, srcVal);
Jordy Rosee64f3112010-08-16 07:51:42 +00001252
1253 // If the source isn't a valid C string, give up.
Ted Kremenekc8413fd2010-12-02 07:49:45 +00001254 if (strLength.isUndef())
Jordy Rosee64f3112010-08-16 07:51:42 +00001255 return;
1256
Jordy Rosed5af0e12011-06-15 05:52:56 +00001257 SValBuilder &svalBuilder = C.getSValBuilder();
1258 QualType cmpTy = svalBuilder.getConditionType();
Jordy Rose8912aae2011-06-20 21:55:40 +00001259 QualType sizeTy = svalBuilder.getContext().getSizeType();
Jordy Rosed5af0e12011-06-15 05:52:56 +00001260
Jordy Rose8912aae2011-06-20 21:55:40 +00001261 // These two values allow checking two kinds of errors:
1262 // - actual overflows caused by a source that doesn't fit in the destination
1263 // - potential overflows caused by a bound that could exceed the destination
Jordy Rosed5af0e12011-06-15 05:52:56 +00001264 SVal amountCopied = UnknownVal();
Jordy Rose8912aae2011-06-20 21:55:40 +00001265 SVal maxLastElementIndex = UnknownVal();
1266 const char *boundWarning = NULL;
Jordy Rosed5af0e12011-06-15 05:52:56 +00001267
Lenny Maiorani067bbd02011-04-09 15:12:58 +00001268 // If the function is strncpy, strncat, etc... it is bounded.
1269 if (isBounded) {
1270 // Get the max number of characters to copy.
Ted Kremenek0ef473f2011-02-22 04:58:34 +00001271 const Expr *lenExpr = CE->getArg(2);
Ted Kremenek5eca4822012-01-06 22:09:28 +00001272 SVal lenVal = state->getSVal(lenExpr, LCtx);
Ted Kremenek0ef473f2011-02-22 04:58:34 +00001273
Jordy Rosed5af0e12011-06-15 05:52:56 +00001274 // Protect against misdeclared strncpy().
Jordy Rose8912aae2011-06-20 21:55:40 +00001275 lenVal = svalBuilder.evalCast(lenVal, sizeTy, lenExpr->getType());
Jordy Rosed5af0e12011-06-15 05:52:56 +00001276
Ted Kremenek0ef473f2011-02-22 04:58:34 +00001277 NonLoc *strLengthNL = dyn_cast<NonLoc>(&strLength);
1278 NonLoc *lenValNL = dyn_cast<NonLoc>(&lenVal);
1279
Jordy Rosed5af0e12011-06-15 05:52:56 +00001280 // If we know both values, we might be able to figure out how much
1281 // we're copying.
1282 if (strLengthNL && lenValNL) {
Ted Kremenek8bef8232012-01-26 21:29:00 +00001283 ProgramStateRef stateSourceTooLong, stateSourceNotTooLong;
Ted Kremenek0ef473f2011-02-22 04:58:34 +00001284
Jordy Rosed5af0e12011-06-15 05:52:56 +00001285 // Check if the max number to copy is less than the length of the src.
Jordy Rose8912aae2011-06-20 21:55:40 +00001286 // If the bound is equal to the source length, strncpy won't null-
1287 // terminate the result!
Jordy Rosed5af0e12011-06-15 05:52:56 +00001288 llvm::tie(stateSourceTooLong, stateSourceNotTooLong) =
1289 state->assume(cast<DefinedOrUnknownSVal>
Jordy Rose8912aae2011-06-20 21:55:40 +00001290 (svalBuilder.evalBinOpNN(state, BO_GE, *strLengthNL,
Jordy Rosed5af0e12011-06-15 05:52:56 +00001291 *lenValNL, cmpTy)));
1292
1293 if (stateSourceTooLong && !stateSourceNotTooLong) {
1294 // Max number to copy is less than the length of the src, so the actual
1295 // strLength copied is the max number arg.
1296 state = stateSourceTooLong;
1297 amountCopied = lenVal;
1298
1299 } else if (!stateSourceTooLong && stateSourceNotTooLong) {
1300 // The source buffer entirely fits in the bound.
1301 state = stateSourceNotTooLong;
1302 amountCopied = strLength;
1303 }
1304 }
1305
Jordy Rose5e5f1502011-06-20 03:49:16 +00001306 // We still want to know if the bound is known to be too large.
Jordy Rose8912aae2011-06-20 21:55:40 +00001307 if (lenValNL) {
1308 if (isAppending) {
1309 // For strncat, the check is strlen(dst) + lenVal < sizeof(dst)
1310
1311 // Get the string length of the destination. If the destination is
1312 // memory that can't have a string length, we shouldn't be copying
1313 // into it anyway.
1314 SVal dstStrLength = getCStringLength(C, state, Dst, DstVal);
1315 if (dstStrLength.isUndef())
1316 return;
1317
1318 if (NonLoc *dstStrLengthNL = dyn_cast<NonLoc>(&dstStrLength)) {
1319 maxLastElementIndex = svalBuilder.evalBinOpNN(state, BO_Add,
1320 *lenValNL,
1321 *dstStrLengthNL,
1322 sizeTy);
1323 boundWarning = "Size argument is greater than the free space in the "
1324 "destination buffer";
1325 }
1326
1327 } else {
1328 // For strncpy, this is just checking that lenVal <= sizeof(dst)
1329 // (Yes, strncpy and strncat differ in how they treat termination.
1330 // strncat ALWAYS terminates, but strncpy doesn't.)
1331 NonLoc one = cast<NonLoc>(svalBuilder.makeIntVal(1, sizeTy));
1332 maxLastElementIndex = svalBuilder.evalBinOpNN(state, BO_Sub, *lenValNL,
1333 one, sizeTy);
1334 boundWarning = "Size argument is greater than the length of the "
1335 "destination buffer";
1336 }
1337 }
Jordy Rose5e5f1502011-06-20 03:49:16 +00001338
Jordy Rosed5af0e12011-06-15 05:52:56 +00001339 // If we couldn't pin down the copy length, at least bound it.
Jordy Rose8912aae2011-06-20 21:55:40 +00001340 // FIXME: We should actually run this code path for append as well, but
1341 // right now it creates problems with constraints (since we can end up
1342 // trying to pass constraints from symbol to symbol).
1343 if (amountCopied.isUnknown() && !isAppending) {
Jordy Rosed5af0e12011-06-15 05:52:56 +00001344 // Try to get a "hypothetical" string length symbol, which we can later
1345 // set as a real value if that turns out to be the case.
1346 amountCopied = getCStringLength(C, state, lenExpr, srcVal, true);
1347 assert(!amountCopied.isUndef());
1348
1349 if (NonLoc *amountCopiedNL = dyn_cast<NonLoc>(&amountCopied)) {
1350 if (lenValNL) {
1351 // amountCopied <= lenVal
1352 SVal copiedLessThanBound = svalBuilder.evalBinOpNN(state, BO_LE,
1353 *amountCopiedNL,
1354 *lenValNL,
1355 cmpTy);
1356 state = state->assume(cast<DefinedOrUnknownSVal>(copiedLessThanBound),
1357 true);
1358 if (!state)
1359 return;
1360 }
1361
1362 if (strLengthNL) {
1363 // amountCopied <= strlen(source)
1364 SVal copiedLessThanSrc = svalBuilder.evalBinOpNN(state, BO_LE,
1365 *amountCopiedNL,
1366 *strLengthNL,
1367 cmpTy);
1368 state = state->assume(cast<DefinedOrUnknownSVal>(copiedLessThanSrc),
1369 true);
1370 if (!state)
1371 return;
1372 }
1373 }
1374 }
1375
1376 } else {
1377 // The function isn't bounded. The amount copied should match the length
1378 // of the source buffer.
1379 amountCopied = strLength;
Ted Kremenek0ef473f2011-02-22 04:58:34 +00001380 }
1381
Jordy Rosed5af0e12011-06-15 05:52:56 +00001382 assert(state);
1383
1384 // This represents the number of characters copied into the destination
1385 // buffer. (It may not actually be the strlen if the destination buffer
1386 // is not terminated.)
1387 SVal finalStrLength = UnknownVal();
1388
Lenny Maiorani067bbd02011-04-09 15:12:58 +00001389 // If this is an appending function (strcat, strncat...) then set the
1390 // string length to strlen(src) + strlen(dst) since the buffer will
1391 // ultimately contain both.
1392 if (isAppending) {
Jordy Rose1e022412011-06-16 05:51:02 +00001393 // Get the string length of the destination. If the destination is memory
1394 // that can't have a string length, we shouldn't be copying into it anyway.
Lenny Maiorani067bbd02011-04-09 15:12:58 +00001395 SVal dstStrLength = getCStringLength(C, state, Dst, DstVal);
1396 if (dstStrLength.isUndef())
1397 return;
1398
Jordy Rosed5af0e12011-06-15 05:52:56 +00001399 NonLoc *srcStrLengthNL = dyn_cast<NonLoc>(&amountCopied);
Lenny Maiorani067bbd02011-04-09 15:12:58 +00001400 NonLoc *dstStrLengthNL = dyn_cast<NonLoc>(&dstStrLength);
1401
Jordy Rosed5af0e12011-06-15 05:52:56 +00001402 // If we know both string lengths, we might know the final string length.
1403 if (srcStrLengthNL && dstStrLengthNL) {
1404 // Make sure the two lengths together don't overflow a size_t.
1405 state = checkAdditionOverflow(C, state, *srcStrLengthNL, *dstStrLengthNL);
1406 if (!state)
1407 return;
Lenny Maiorani067bbd02011-04-09 15:12:58 +00001408
Jordy Rosed5af0e12011-06-15 05:52:56 +00001409 finalStrLength = svalBuilder.evalBinOpNN(state, BO_Add, *srcStrLengthNL,
1410 *dstStrLengthNL, sizeTy);
1411 }
Lenny Maiorani067bbd02011-04-09 15:12:58 +00001412
Jordy Rosed5af0e12011-06-15 05:52:56 +00001413 // If we couldn't get a single value for the final string length,
1414 // we can at least bound it by the individual lengths.
1415 if (finalStrLength.isUnknown()) {
1416 // Try to get a "hypothetical" string length symbol, which we can later
1417 // set as a real value if that turns out to be the case.
1418 finalStrLength = getCStringLength(C, state, CE, DstVal, true);
1419 assert(!finalStrLength.isUndef());
1420
1421 if (NonLoc *finalStrLengthNL = dyn_cast<NonLoc>(&finalStrLength)) {
1422 if (srcStrLengthNL) {
1423 // finalStrLength >= srcStrLength
1424 SVal sourceInResult = svalBuilder.evalBinOpNN(state, BO_GE,
1425 *finalStrLengthNL,
1426 *srcStrLengthNL,
1427 cmpTy);
1428 state = state->assume(cast<DefinedOrUnknownSVal>(sourceInResult),
1429 true);
1430 if (!state)
1431 return;
1432 }
1433
1434 if (dstStrLengthNL) {
1435 // finalStrLength >= dstStrLength
1436 SVal destInResult = svalBuilder.evalBinOpNN(state, BO_GE,
1437 *finalStrLengthNL,
1438 *dstStrLengthNL,
1439 cmpTy);
1440 state = state->assume(cast<DefinedOrUnknownSVal>(destInResult),
1441 true);
1442 if (!state)
1443 return;
1444 }
1445 }
1446 }
1447
1448 } else {
1449 // Otherwise, this is a copy-over function (strcpy, strncpy, ...), and
1450 // the final string length will match the input string length.
1451 finalStrLength = amountCopied;
Lenny Maiorani067bbd02011-04-09 15:12:58 +00001452 }
1453
Jordy Rosed5af0e12011-06-15 05:52:56 +00001454 // The final result of the function will either be a pointer past the last
1455 // copied element, or a pointer to the start of the destination buffer.
Ted Kremenekc8413fd2010-12-02 07:49:45 +00001456 SVal Result = (returnEnd ? UnknownVal() : DstVal);
Jordy Rosee64f3112010-08-16 07:51:42 +00001457
Jordy Rosed5af0e12011-06-15 05:52:56 +00001458 assert(state);
1459
Jordy Rosee64f3112010-08-16 07:51:42 +00001460 // If the destination is a MemRegion, try to check for a buffer overflow and
1461 // record the new string length.
Ted Kremenekc8413fd2010-12-02 07:49:45 +00001462 if (loc::MemRegionVal *dstRegVal = dyn_cast<loc::MemRegionVal>(&DstVal)) {
Jordy Rose8912aae2011-06-20 21:55:40 +00001463 QualType ptrTy = Dst->getType();
1464
1465 // If we have an exact value on a bounded copy, use that to check for
1466 // overflows, rather than our estimate about how much is actually copied.
1467 if (boundWarning) {
1468 if (NonLoc *maxLastNL = dyn_cast<NonLoc>(&maxLastElementIndex)) {
1469 SVal maxLastElement = svalBuilder.evalBinOpLN(state, BO_Add, *dstRegVal,
1470 *maxLastNL, ptrTy);
1471 state = CheckLocation(C, state, CE->getArg(2), maxLastElement,
1472 boundWarning);
1473 if (!state)
1474 return;
1475 }
1476 }
1477
1478 // Then, if the final length is known...
Jordy Rosed5af0e12011-06-15 05:52:56 +00001479 if (NonLoc *knownStrLength = dyn_cast<NonLoc>(&finalStrLength)) {
1480 SVal lastElement = svalBuilder.evalBinOpLN(state, BO_Add, *dstRegVal,
Jordy Rose8912aae2011-06-20 21:55:40 +00001481 *knownStrLength, ptrTy);
Jordy Rosee64f3112010-08-16 07:51:42 +00001482
Jordy Rose8912aae2011-06-20 21:55:40 +00001483 // ...and we haven't checked the bound, we'll check the actual copy.
1484 if (!boundWarning) {
1485 const char * const warningMsg =
1486 "String copy function overflows destination buffer";
1487 state = CheckLocation(C, state, Dst, lastElement, warningMsg);
1488 if (!state)
1489 return;
1490 }
Jordy Rosee64f3112010-08-16 07:51:42 +00001491
1492 // If this is a stpcpy-style copy, the last element is the return value.
Ted Kremenekc8413fd2010-12-02 07:49:45 +00001493 if (returnEnd)
1494 Result = lastElement;
Jordy Rosee64f3112010-08-16 07:51:42 +00001495 }
1496
1497 // Invalidate the destination. This must happen before we set the C string
1498 // length because invalidation will clear the length.
1499 // FIXME: Even if we can't perfectly model the copy, we should see if we
1500 // can use LazyCompoundVals to copy the source values into the destination.
1501 // This would probably remove any existing bindings past the end of the
1502 // string, but that's still an improvement over blank invalidation.
Ted Kremenekc8413fd2010-12-02 07:49:45 +00001503 state = InvalidateBuffer(C, state, Dst, *dstRegVal);
Jordy Rosee64f3112010-08-16 07:51:42 +00001504
Jordy Rose5e5f1502011-06-20 03:49:16 +00001505 // Set the C string length of the destination, if we know it.
Jordy Rose8912aae2011-06-20 21:55:40 +00001506 if (isBounded && !isAppending) {
1507 // strncpy is annoying in that it doesn't guarantee to null-terminate
1508 // the result string. If the original string didn't fit entirely inside
1509 // the bound (including the null-terminator), we don't know how long the
1510 // result is.
1511 if (amountCopied != strLength)
1512 finalStrLength = UnknownVal();
1513 }
1514 state = setCStringLength(state, dstRegVal->getRegion(), finalStrLength);
Jordy Rosee64f3112010-08-16 07:51:42 +00001515 }
1516
Jordy Rosed5af0e12011-06-15 05:52:56 +00001517 assert(state);
1518
Jordy Rosee64f3112010-08-16 07:51:42 +00001519 // If this is a stpcpy-style copy, but we were unable to check for a buffer
1520 // overflow, we still need a result. Conjure a return value.
Ted Kremenekc8413fd2010-12-02 07:49:45 +00001521 if (returnEnd && Result.isUnknown()) {
Anna Zaks5d0ea6d2011-10-04 20:43:05 +00001522 unsigned Count = C.getCurrentBlockCount();
Jordy Rosed5af0e12011-06-15 05:52:56 +00001523 Result = svalBuilder.getConjuredSymbolVal(NULL, CE, Count);
Jordy Rosee64f3112010-08-16 07:51:42 +00001524 }
1525
1526 // Set the return value.
Ted Kremenek5eca4822012-01-06 22:09:28 +00001527 state = state->BindExpr(CE, LCtx, Result);
Anna Zaks0bd6b112011-10-26 21:06:34 +00001528 C.addTransition(state);
Jordy Rosee64f3112010-08-16 07:51:42 +00001529}
1530
Lenny Maiorani318dd922011-04-12 17:08:43 +00001531void CStringChecker::evalStrcmp(CheckerContext &C, const CallExpr *CE) const {
Jordy Roseadc42d42011-06-16 07:13:34 +00001532 //int strcmp(const char *s1, const char *s2);
Lenny Maioranibd1d16a2011-04-28 15:09:11 +00001533 evalStrcmpCommon(C, CE, /* isBounded = */ false, /* ignoreCase = */ false);
Lenny Maiorani357f6ee2011-04-25 22:21:00 +00001534}
Lenny Maiorani318dd922011-04-12 17:08:43 +00001535
Lenny Maiorani357f6ee2011-04-25 22:21:00 +00001536void CStringChecker::evalStrncmp(CheckerContext &C, const CallExpr *CE) const {
Jordy Roseadc42d42011-06-16 07:13:34 +00001537 //int strncmp(const char *s1, const char *s2, size_t n);
Lenny Maioranibd1d16a2011-04-28 15:09:11 +00001538 evalStrcmpCommon(C, CE, /* isBounded = */ true, /* ignoreCase = */ false);
1539}
1540
1541void CStringChecker::evalStrcasecmp(CheckerContext &C,
1542 const CallExpr *CE) const {
Jordy Roseadc42d42011-06-16 07:13:34 +00001543 //int strcasecmp(const char *s1, const char *s2);
Lenny Maioranibd1d16a2011-04-28 15:09:11 +00001544 evalStrcmpCommon(C, CE, /* isBounded = */ false, /* ignoreCase = */ true);
Lenny Maiorani357f6ee2011-04-25 22:21:00 +00001545}
1546
Lenny Maiorani454fd2d2011-05-02 19:05:49 +00001547void CStringChecker::evalStrncasecmp(CheckerContext &C,
1548 const CallExpr *CE) const {
Jordy Roseadc42d42011-06-16 07:13:34 +00001549 //int strncasecmp(const char *s1, const char *s2, size_t n);
Lenny Maiorani454fd2d2011-05-02 19:05:49 +00001550 evalStrcmpCommon(C, CE, /* isBounded = */ true, /* ignoreCase = */ true);
1551}
1552
Lenny Maiorani357f6ee2011-04-25 22:21:00 +00001553void CStringChecker::evalStrcmpCommon(CheckerContext &C, const CallExpr *CE,
Lenny Maioranibd1d16a2011-04-28 15:09:11 +00001554 bool isBounded, bool ignoreCase) const {
Jordy Rose9e49d9f2011-06-20 02:06:40 +00001555 CurrentFunctionDescription = "string comparison function";
Ted Kremenek8bef8232012-01-26 21:29:00 +00001556 ProgramStateRef state = C.getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +00001557 const LocationContext *LCtx = C.getLocationContext();
Lenny Maiorani318dd922011-04-12 17:08:43 +00001558
1559 // Check that the first string is non-null
1560 const Expr *s1 = CE->getArg(0);
Ted Kremenek5eca4822012-01-06 22:09:28 +00001561 SVal s1Val = state->getSVal(s1, LCtx);
Lenny Maiorani318dd922011-04-12 17:08:43 +00001562 state = checkNonNull(C, state, s1, s1Val);
1563 if (!state)
1564 return;
1565
1566 // Check that the second string is non-null.
1567 const Expr *s2 = CE->getArg(1);
Ted Kremenek5eca4822012-01-06 22:09:28 +00001568 SVal s2Val = state->getSVal(s2, LCtx);
Lenny Maiorani318dd922011-04-12 17:08:43 +00001569 state = checkNonNull(C, state, s2, s2Val);
1570 if (!state)
1571 return;
1572
1573 // Get the string length of the first string or give up.
1574 SVal s1Length = getCStringLength(C, state, s1, s1Val);
1575 if (s1Length.isUndef())
1576 return;
1577
1578 // Get the string length of the second string or give up.
1579 SVal s2Length = getCStringLength(C, state, s2, s2Val);
1580 if (s2Length.isUndef())
1581 return;
1582
Jordy Roseadc42d42011-06-16 07:13:34 +00001583 // If we know the two buffers are the same, we know the result is 0.
1584 // First, get the two buffers' addresses. Another checker will have already
1585 // made sure they're not undefined.
1586 DefinedOrUnknownSVal LV = cast<DefinedOrUnknownSVal>(s1Val);
1587 DefinedOrUnknownSVal RV = cast<DefinedOrUnknownSVal>(s2Val);
Lenny Maiorani318dd922011-04-12 17:08:43 +00001588
Jordy Roseadc42d42011-06-16 07:13:34 +00001589 // See if they are the same.
Lenny Maiorani318dd922011-04-12 17:08:43 +00001590 SValBuilder &svalBuilder = C.getSValBuilder();
Jordy Roseadc42d42011-06-16 07:13:34 +00001591 DefinedOrUnknownSVal SameBuf = svalBuilder.evalEQ(state, LV, RV);
Ted Kremenek8bef8232012-01-26 21:29:00 +00001592 ProgramStateRef StSameBuf, StNotSameBuf;
Jordy Roseadc42d42011-06-16 07:13:34 +00001593 llvm::tie(StSameBuf, StNotSameBuf) = state->assume(SameBuf);
Lenny Maiorani318dd922011-04-12 17:08:43 +00001594
Jordy Roseadc42d42011-06-16 07:13:34 +00001595 // If the two arguments might be the same buffer, we know the result is 0,
1596 // and we only need to check one size.
1597 if (StSameBuf) {
Ted Kremenek5eca4822012-01-06 22:09:28 +00001598 StSameBuf = StSameBuf->BindExpr(CE, LCtx,
1599 svalBuilder.makeZeroVal(CE->getType()));
Anna Zaks0bd6b112011-10-26 21:06:34 +00001600 C.addTransition(StSameBuf);
Jordy Roseadc42d42011-06-16 07:13:34 +00001601
1602 // If the two arguments are GUARANTEED to be the same, we're done!
1603 if (!StNotSameBuf)
1604 return;
1605 }
1606
1607 assert(StNotSameBuf);
1608 state = StNotSameBuf;
1609
1610 // At this point we can go about comparing the two buffers.
1611 // For now, we only do this if they're both known string literals.
1612
1613 // Attempt to extract string literals from both expressions.
1614 const StringLiteral *s1StrLiteral = getCStringLiteral(C, state, s1, s1Val);
1615 const StringLiteral *s2StrLiteral = getCStringLiteral(C, state, s2, s2Val);
1616 bool canComputeResult = false;
1617
1618 if (s1StrLiteral && s2StrLiteral) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001619 StringRef s1StrRef = s1StrLiteral->getString();
1620 StringRef s2StrRef = s2StrLiteral->getString();
Jordy Roseadc42d42011-06-16 07:13:34 +00001621
1622 if (isBounded) {
1623 // Get the max number of characters to compare.
1624 const Expr *lenExpr = CE->getArg(2);
Ted Kremenek5eca4822012-01-06 22:09:28 +00001625 SVal lenVal = state->getSVal(lenExpr, LCtx);
Jordy Roseadc42d42011-06-16 07:13:34 +00001626
1627 // If the length is known, we can get the right substrings.
1628 if (const llvm::APSInt *len = svalBuilder.getKnownValue(state, lenVal)) {
1629 // Create substrings of each to compare the prefix.
1630 s1StrRef = s1StrRef.substr(0, (size_t)len->getZExtValue());
1631 s2StrRef = s2StrRef.substr(0, (size_t)len->getZExtValue());
1632 canComputeResult = true;
1633 }
1634 } else {
1635 // This is a normal, unbounded strcmp.
1636 canComputeResult = true;
1637 }
1638
1639 if (canComputeResult) {
1640 // Real strcmp stops at null characters.
1641 size_t s1Term = s1StrRef.find('\0');
Chris Lattner5f9e2722011-07-23 10:55:15 +00001642 if (s1Term != StringRef::npos)
Jordy Roseadc42d42011-06-16 07:13:34 +00001643 s1StrRef = s1StrRef.substr(0, s1Term);
1644
1645 size_t s2Term = s2StrRef.find('\0');
Chris Lattner5f9e2722011-07-23 10:55:15 +00001646 if (s2Term != StringRef::npos)
Jordy Roseadc42d42011-06-16 07:13:34 +00001647 s2StrRef = s2StrRef.substr(0, s2Term);
1648
1649 // Use StringRef's comparison methods to compute the actual result.
1650 int result;
1651
1652 if (ignoreCase) {
1653 // Compare string 1 to string 2 the same way strcasecmp() does.
1654 result = s1StrRef.compare_lower(s2StrRef);
1655 } else {
1656 // Compare string 1 to string 2 the same way strcmp() does.
1657 result = s1StrRef.compare(s2StrRef);
1658 }
1659
1660 // Build the SVal of the comparison and bind the return value.
1661 SVal resultVal = svalBuilder.makeIntVal(result, CE->getType());
Ted Kremenek5eca4822012-01-06 22:09:28 +00001662 state = state->BindExpr(CE, LCtx, resultVal);
Jordy Roseadc42d42011-06-16 07:13:34 +00001663 }
1664 }
1665
1666 if (!canComputeResult) {
1667 // Conjure a symbolic value. It's the best we can do.
Anna Zaks5d0ea6d2011-10-04 20:43:05 +00001668 unsigned Count = C.getCurrentBlockCount();
Jordy Roseadc42d42011-06-16 07:13:34 +00001669 SVal resultVal = svalBuilder.getConjuredSymbolVal(NULL, CE, Count);
Ted Kremenek5eca4822012-01-06 22:09:28 +00001670 state = state->BindExpr(CE, LCtx, resultVal);
Jordy Roseadc42d42011-06-16 07:13:34 +00001671 }
1672
1673 // Record this as a possible path.
Anna Zaks0bd6b112011-10-26 21:06:34 +00001674 C.addTransition(state);
Lenny Maiorani318dd922011-04-12 17:08:43 +00001675}
1676
Jordy Rosed325ffb2010-07-08 23:57:29 +00001677//===----------------------------------------------------------------------===//
Jordy Rosea5261542010-08-14 21:02:52 +00001678// The driver method, and other Checker callbacks.
Jordy Rosed325ffb2010-07-08 23:57:29 +00001679//===----------------------------------------------------------------------===//
Jordy Roseccbf7ee2010-07-06 23:11:01 +00001680
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +00001681bool CStringChecker::evalCall(const CallExpr *CE, CheckerContext &C) const {
Anna Zaksb805c8f2011-12-01 05:57:37 +00001682 StringRef Name = C.getCalleeName(CE);
1683 if (Name.empty())
Jordy Roseccbf7ee2010-07-06 23:11:01 +00001684 return false;
Jordy Roseccbf7ee2010-07-06 23:11:01 +00001685 if (Name.startswith("__builtin_"))
1686 Name = Name.substr(10);
1687
Ted Kremenek9c149532010-12-01 21:57:22 +00001688 FnCheck evalFunction = llvm::StringSwitch<FnCheck>(Name)
1689 .Cases("memcpy", "__memcpy_chk", &CStringChecker::evalMemcpy)
Jordy Rosebe460d82011-06-03 23:42:56 +00001690 .Cases("mempcpy", "__mempcpy_chk", &CStringChecker::evalMempcpy)
Ted Kremenek9c149532010-12-01 21:57:22 +00001691 .Cases("memcmp", "bcmp", &CStringChecker::evalMemcmp)
1692 .Cases("memmove", "__memmove_chk", &CStringChecker::evalMemmove)
1693 .Cases("strcpy", "__strcpy_chk", &CStringChecker::evalStrcpy)
Jordy Rose5e5f1502011-06-20 03:49:16 +00001694 .Cases("strncpy", "__strncpy_chk", &CStringChecker::evalStrncpy)
Ted Kremenek9c149532010-12-01 21:57:22 +00001695 .Cases("stpcpy", "__stpcpy_chk", &CStringChecker::evalStpcpy)
Lenny Maiorani067bbd02011-04-09 15:12:58 +00001696 .Cases("strcat", "__strcat_chk", &CStringChecker::evalStrcat)
1697 .Cases("strncat", "__strncat_chk", &CStringChecker::evalStrncat)
Ted Kremenekc8413fd2010-12-02 07:49:45 +00001698 .Case("strlen", &CStringChecker::evalstrLength)
Ted Kremenekbe4242c2011-02-22 04:55:05 +00001699 .Case("strnlen", &CStringChecker::evalstrnLength)
Lenny Maiorani318dd922011-04-12 17:08:43 +00001700 .Case("strcmp", &CStringChecker::evalStrcmp)
Lenny Maiorani357f6ee2011-04-25 22:21:00 +00001701 .Case("strncmp", &CStringChecker::evalStrncmp)
Lenny Maioranibd1d16a2011-04-28 15:09:11 +00001702 .Case("strcasecmp", &CStringChecker::evalStrcasecmp)
Lenny Maiorani454fd2d2011-05-02 19:05:49 +00001703 .Case("strncasecmp", &CStringChecker::evalStrncasecmp)
Ted Kremenek9c149532010-12-01 21:57:22 +00001704 .Case("bcopy", &CStringChecker::evalBcopy)
Jordy Roseccbf7ee2010-07-06 23:11:01 +00001705 .Default(NULL);
1706
Jordy Rosed325ffb2010-07-08 23:57:29 +00001707 // If the callee isn't a string function, let another checker handle it.
Ted Kremenek9c149532010-12-01 21:57:22 +00001708 if (!evalFunction)
Jordy Roseccbf7ee2010-07-06 23:11:01 +00001709 return false;
1710
Jordy Rose9e49d9f2011-06-20 02:06:40 +00001711 // Make sure each function sets its own description.
1712 // (But don't bother in a release build.)
1713 assert(!(CurrentFunctionDescription = NULL));
1714
Jordy Rosed325ffb2010-07-08 23:57:29 +00001715 // Check and evaluate the call.
Ted Kremenek9c149532010-12-01 21:57:22 +00001716 (this->*evalFunction)(C, CE);
Jordy Roseccbf7ee2010-07-06 23:11:01 +00001717 return true;
1718}
Jordy Rosea5261542010-08-14 21:02:52 +00001719
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +00001720void CStringChecker::checkPreStmt(const DeclStmt *DS, CheckerContext &C) const {
Jordy Rosea5261542010-08-14 21:02:52 +00001721 // Record string length for char a[] = "abc";
Ted Kremenek8bef8232012-01-26 21:29:00 +00001722 ProgramStateRef state = C.getState();
Jordy Rosea5261542010-08-14 21:02:52 +00001723
1724 for (DeclStmt::const_decl_iterator I = DS->decl_begin(), E = DS->decl_end();
1725 I != E; ++I) {
1726 const VarDecl *D = dyn_cast<VarDecl>(*I);
1727 if (!D)
1728 continue;
1729
1730 // FIXME: Handle array fields of structs.
1731 if (!D->getType()->isArrayType())
1732 continue;
1733
1734 const Expr *Init = D->getInit();
1735 if (!Init)
1736 continue;
1737 if (!isa<StringLiteral>(Init))
1738 continue;
1739
Anna Zaks39ac1872011-10-26 21:06:44 +00001740 Loc VarLoc = state->getLValue(D, C.getLocationContext());
Jordy Rosea5261542010-08-14 21:02:52 +00001741 const MemRegion *MR = VarLoc.getAsRegion();
1742 if (!MR)
1743 continue;
1744
Ted Kremenek5eca4822012-01-06 22:09:28 +00001745 SVal StrVal = state->getSVal(Init, C.getLocationContext());
Jordy Rosea5261542010-08-14 21:02:52 +00001746 assert(StrVal.isValid() && "Initializer string is unknown or undefined");
Ted Kremenekc8413fd2010-12-02 07:49:45 +00001747 DefinedOrUnknownSVal strLength
1748 = cast<DefinedOrUnknownSVal>(getCStringLength(C, state, Init, StrVal));
Jordy Rosea5261542010-08-14 21:02:52 +00001749
Ted Kremenekc8413fd2010-12-02 07:49:45 +00001750 state = state->set<CStringLength>(MR, strLength);
Jordy Rosea5261542010-08-14 21:02:52 +00001751 }
1752
Anna Zaks0bd6b112011-10-26 21:06:34 +00001753 C.addTransition(state);
Jordy Rosea5261542010-08-14 21:02:52 +00001754}
1755
Ted Kremenek8bef8232012-01-26 21:29:00 +00001756bool CStringChecker::wantsRegionChangeUpdate(ProgramStateRef state) const {
Jordy Rosea5261542010-08-14 21:02:52 +00001757 CStringLength::EntryMap Entries = state->get<CStringLength>();
1758 return !Entries.isEmpty();
1759}
1760
Ted Kremenek8bef8232012-01-26 21:29:00 +00001761ProgramStateRef
1762CStringChecker::checkRegionChanges(ProgramStateRef state,
Ted Kremenek35bdbf42011-05-02 19:42:42 +00001763 const StoreManager::InvalidatedSymbols *,
Jordy Rose537716a2011-08-27 22:51:26 +00001764 ArrayRef<const MemRegion *> ExplicitRegions,
1765 ArrayRef<const MemRegion *> Regions) const {
Jordy Rosea5261542010-08-14 21:02:52 +00001766 CStringLength::EntryMap Entries = state->get<CStringLength>();
1767 if (Entries.isEmpty())
1768 return state;
1769
1770 llvm::SmallPtrSet<const MemRegion *, 8> Invalidated;
1771 llvm::SmallPtrSet<const MemRegion *, 32> SuperRegions;
1772
1773 // First build sets for the changed regions and their super-regions.
Jordy Rose537716a2011-08-27 22:51:26 +00001774 for (ArrayRef<const MemRegion *>::iterator
1775 I = Regions.begin(), E = Regions.end(); I != E; ++I) {
1776 const MemRegion *MR = *I;
Jordy Rosea5261542010-08-14 21:02:52 +00001777 Invalidated.insert(MR);
1778
1779 SuperRegions.insert(MR);
1780 while (const SubRegion *SR = dyn_cast<SubRegion>(MR)) {
1781 MR = SR->getSuperRegion();
1782 SuperRegions.insert(MR);
1783 }
1784 }
1785
1786 CStringLength::EntryMap::Factory &F = state->get_context<CStringLength>();
1787
1788 // Then loop over the entries in the current state.
1789 for (CStringLength::EntryMap::iterator I = Entries.begin(),
1790 E = Entries.end(); I != E; ++I) {
1791 const MemRegion *MR = I.getKey();
1792
1793 // Is this entry for a super-region of a changed region?
1794 if (SuperRegions.count(MR)) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00001795 Entries = F.remove(Entries, MR);
Jordy Rosea5261542010-08-14 21:02:52 +00001796 continue;
1797 }
1798
1799 // Is this entry for a sub-region of a changed region?
1800 const MemRegion *Super = MR;
1801 while (const SubRegion *SR = dyn_cast<SubRegion>(Super)) {
1802 Super = SR->getSuperRegion();
1803 if (Invalidated.count(Super)) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00001804 Entries = F.remove(Entries, MR);
Jordy Rosea5261542010-08-14 21:02:52 +00001805 break;
1806 }
1807 }
1808 }
1809
1810 return state->set<CStringLength>(Entries);
1811}
1812
Ted Kremenek8bef8232012-01-26 21:29:00 +00001813void CStringChecker::checkLiveSymbols(ProgramStateRef state,
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +00001814 SymbolReaper &SR) const {
Jordy Rosea5261542010-08-14 21:02:52 +00001815 // Mark all symbols in our string length map as valid.
1816 CStringLength::EntryMap Entries = state->get<CStringLength>();
1817
1818 for (CStringLength::EntryMap::iterator I = Entries.begin(), E = Entries.end();
1819 I != E; ++I) {
1820 SVal Len = I.getData();
Jordy Rosed5af0e12011-06-15 05:52:56 +00001821
Anna Zaks1d1d5152011-12-06 23:12:33 +00001822 for (SymExpr::symbol_iterator si = Len.symbol_begin(),
1823 se = Len.symbol_end(); si != se; ++si)
Jordy Rosed5af0e12011-06-15 05:52:56 +00001824 SR.markInUse(*si);
Jordy Rosea5261542010-08-14 21:02:52 +00001825 }
1826}
1827
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +00001828void CStringChecker::checkDeadSymbols(SymbolReaper &SR,
1829 CheckerContext &C) const {
Jordy Rosea5261542010-08-14 21:02:52 +00001830 if (!SR.hasDeadSymbols())
1831 return;
1832
Ted Kremenek8bef8232012-01-26 21:29:00 +00001833 ProgramStateRef state = C.getState();
Jordy Rosea5261542010-08-14 21:02:52 +00001834 CStringLength::EntryMap Entries = state->get<CStringLength>();
1835 if (Entries.isEmpty())
1836 return;
1837
1838 CStringLength::EntryMap::Factory &F = state->get_context<CStringLength>();
1839 for (CStringLength::EntryMap::iterator I = Entries.begin(), E = Entries.end();
1840 I != E; ++I) {
1841 SVal Len = I.getData();
1842 if (SymbolRef Sym = Len.getAsSymbol()) {
1843 if (SR.isDead(Sym))
Ted Kremenek3baf6722010-11-24 00:54:37 +00001844 Entries = F.remove(Entries, I.getKey());
Jordy Rosea5261542010-08-14 21:02:52 +00001845 }
1846 }
1847
1848 state = state->set<CStringLength>(Entries);
Anna Zaks0bd6b112011-10-26 21:06:34 +00001849 C.addTransition(state);
Jordy Rosea5261542010-08-14 21:02:52 +00001850}
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +00001851
1852void ento::registerCStringChecker(CheckerManager &mgr) {
1853 mgr.registerChecker<CStringChecker>();
1854}