blob: 7518b96c63b56cc05863c3fc3d56786f5065c641 [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 Kramer8fe83e12012-02-04 13:45:25 +000021#include "llvm/ADT/SmallString.h"
Benjamin Kramer00bd44d2012-02-04 12:31:12 +000022#include "llvm/ADT/STLExtras.h"
Jordy Roseccbf7ee2010-07-06 23:11:01 +000023#include "llvm/ADT/StringSwitch.h"
24
25using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000026using namespace ento;
Jordy Roseccbf7ee2010-07-06 23:11:01 +000027
28namespace {
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000029class CStringChecker : public Checker< eval::Call,
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +000030 check::PreStmt<DeclStmt>,
31 check::LiveSymbols,
32 check::DeadSymbols,
33 check::RegionChanges
34 > {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +000035 mutable OwningPtr<BugType> BT_Null, BT_Bounds,
Jordy Rosed5af0e12011-06-15 05:52:56 +000036 BT_Overlap, BT_NotCString,
37 BT_AdditionOverflow;
Jordy Rose9e49d9f2011-06-20 02:06:40 +000038 mutable const char *CurrentFunctionDescription;
39
Jordy Roseccbf7ee2010-07-06 23:11:01 +000040public:
Jordy Roseccbf7ee2010-07-06 23:11:01 +000041 static void *getTag() { static int tag; return &tag; }
42
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +000043 bool evalCall(const CallExpr *CE, CheckerContext &C) const;
44 void checkPreStmt(const DeclStmt *DS, CheckerContext &C) const;
Ted Kremenek8bef8232012-01-26 21:29:00 +000045 void checkLiveSymbols(ProgramStateRef state, SymbolReaper &SR) const;
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +000046 void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const;
Ted Kremenek8bef8232012-01-26 21:29:00 +000047 bool wantsRegionChangeUpdate(ProgramStateRef state) const;
Jordy Rosea5261542010-08-14 21:02:52 +000048
Ted Kremenek8bef8232012-01-26 21:29:00 +000049 ProgramStateRef
50 checkRegionChanges(ProgramStateRef state,
Ted Kremenek18c66fd2011-08-15 22:09:50 +000051 const StoreManager::InvalidatedSymbols *,
Jordy Rose537716a2011-08-27 22:51:26 +000052 ArrayRef<const MemRegion *> ExplicitRegions,
53 ArrayRef<const MemRegion *> Regions) const;
Jordy Roseccbf7ee2010-07-06 23:11:01 +000054
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +000055 typedef void (CStringChecker::*FnCheck)(CheckerContext &,
56 const CallExpr *) const;
Jordy Roseccbf7ee2010-07-06 23:11:01 +000057
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +000058 void evalMemcpy(CheckerContext &C, const CallExpr *CE) const;
Lenny Maioranib8b875b2011-03-31 21:36:53 +000059 void evalMempcpy(CheckerContext &C, const CallExpr *CE) const;
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +000060 void evalMemmove(CheckerContext &C, const CallExpr *CE) const;
61 void evalBcopy(CheckerContext &C, const CallExpr *CE) const;
Lenny Maioranib8b875b2011-03-31 21:36:53 +000062 void evalCopyCommon(CheckerContext &C, const CallExpr *CE,
Ted Kremenek8bef8232012-01-26 21:29:00 +000063 ProgramStateRef state,
Ted Kremenek18c66fd2011-08-15 22:09:50 +000064 const Expr *Size,
65 const Expr *Source,
66 const Expr *Dest,
Lenny Maioranib8b875b2011-03-31 21:36:53 +000067 bool Restricted = false,
68 bool IsMempcpy = false) const;
Jordy Rosed325ffb2010-07-08 23:57:29 +000069
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +000070 void evalMemcmp(CheckerContext &C, const CallExpr *CE) const;
Jordy Roseccbf7ee2010-07-06 23:11:01 +000071
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +000072 void evalstrLength(CheckerContext &C, const CallExpr *CE) const;
73 void evalstrnLength(CheckerContext &C, const CallExpr *CE) const;
Ted Kremenek18c66fd2011-08-15 22:09:50 +000074 void evalstrLengthCommon(CheckerContext &C,
75 const CallExpr *CE,
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +000076 bool IsStrnlen = false) const;
Jordy Rose19c5dd12010-07-27 01:37:31 +000077
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +000078 void evalStrcpy(CheckerContext &C, const CallExpr *CE) const;
79 void evalStrncpy(CheckerContext &C, const CallExpr *CE) const;
80 void evalStpcpy(CheckerContext &C, const CallExpr *CE) const;
Ted Kremenek18c66fd2011-08-15 22:09:50 +000081 void evalStrcpyCommon(CheckerContext &C,
82 const CallExpr *CE,
83 bool returnEnd,
84 bool isBounded,
85 bool isAppending) const;
Lenny Maiorani067bbd02011-04-09 15:12:58 +000086
87 void evalStrcat(CheckerContext &C, const CallExpr *CE) const;
88 void evalStrncat(CheckerContext &C, const CallExpr *CE) const;
Jordy Rosee64f3112010-08-16 07:51:42 +000089
Lenny Maiorani318dd922011-04-12 17:08:43 +000090 void evalStrcmp(CheckerContext &C, const CallExpr *CE) const;
Lenny Maiorani357f6ee2011-04-25 22:21:00 +000091 void evalStrncmp(CheckerContext &C, const CallExpr *CE) const;
Lenny Maioranibd1d16a2011-04-28 15:09:11 +000092 void evalStrcasecmp(CheckerContext &C, const CallExpr *CE) const;
Lenny Maiorani454fd2d2011-05-02 19:05:49 +000093 void evalStrncasecmp(CheckerContext &C, const CallExpr *CE) const;
Ted Kremenek18c66fd2011-08-15 22:09:50 +000094 void evalStrcmpCommon(CheckerContext &C,
95 const CallExpr *CE,
96 bool isBounded = false,
97 bool ignoreCase = false) const;
Lenny Maiorani318dd922011-04-12 17:08:43 +000098
Jordy Roseccbf7ee2010-07-06 23:11:01 +000099 // Utility methods
Ted Kremenek8bef8232012-01-26 21:29:00 +0000100 std::pair<ProgramStateRef , ProgramStateRef >
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +0000101 static assumeZero(CheckerContext &C,
Ted Kremenek8bef8232012-01-26 21:29:00 +0000102 ProgramStateRef state, SVal V, QualType Ty);
Jordy Rosed325ffb2010-07-08 23:57:29 +0000103
Ted Kremenek8bef8232012-01-26 21:29:00 +0000104 static ProgramStateRef setCStringLength(ProgramStateRef state,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000105 const MemRegion *MR,
106 SVal strLength);
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +0000107 static SVal getCStringLengthForRegion(CheckerContext &C,
Ted Kremenek8bef8232012-01-26 21:29:00 +0000108 ProgramStateRef &state,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000109 const Expr *Ex,
110 const MemRegion *MR,
Jordy Rosed5af0e12011-06-15 05:52:56 +0000111 bool hypothetical);
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000112 SVal getCStringLength(CheckerContext &C,
Ted Kremenek8bef8232012-01-26 21:29:00 +0000113 ProgramStateRef &state,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000114 const Expr *Ex,
115 SVal Buf,
Jordy Rosed5af0e12011-06-15 05:52:56 +0000116 bool hypothetical = false) const;
Jordy Rose19c5dd12010-07-27 01:37:31 +0000117
Lenny Maiorani318dd922011-04-12 17:08:43 +0000118 const StringLiteral *getCStringLiteral(CheckerContext &C,
Ted Kremenek8bef8232012-01-26 21:29:00 +0000119 ProgramStateRef &state,
Lenny Maiorani318dd922011-04-12 17:08:43 +0000120 const Expr *expr,
121 SVal val) const;
122
Ted Kremenek8bef8232012-01-26 21:29:00 +0000123 static ProgramStateRef InvalidateBuffer(CheckerContext &C,
124 ProgramStateRef state,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000125 const Expr *Ex, SVal V);
Jordy Rosee64f3112010-08-16 07:51:42 +0000126
Ted Kremenek9c378f72011-08-12 23:37:29 +0000127 static bool SummarizeRegion(raw_ostream &os, ASTContext &Ctx,
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +0000128 const MemRegion *MR);
Jordy Rose19c5dd12010-07-27 01:37:31 +0000129
130 // Re-usable checks
Ted Kremenek8bef8232012-01-26 21:29:00 +0000131 ProgramStateRef checkNonNull(CheckerContext &C,
132 ProgramStateRef state,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000133 const Expr *S,
134 SVal l) const;
Ted Kremenek8bef8232012-01-26 21:29:00 +0000135 ProgramStateRef CheckLocation(CheckerContext &C,
136 ProgramStateRef state,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000137 const Expr *S,
138 SVal l,
139 const char *message = NULL) const;
Ted Kremenek8bef8232012-01-26 21:29:00 +0000140 ProgramStateRef CheckBufferAccess(CheckerContext &C,
141 ProgramStateRef state,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000142 const Expr *Size,
143 const Expr *FirstBuf,
144 const Expr *SecondBuf,
145 const char *firstMessage = NULL,
146 const char *secondMessage = NULL,
147 bool WarnAboutSize = false) const;
148
Ted Kremenek8bef8232012-01-26 21:29:00 +0000149 ProgramStateRef CheckBufferAccess(CheckerContext &C,
150 ProgramStateRef state,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000151 const Expr *Size,
152 const Expr *Buf,
153 const char *message = NULL,
154 bool WarnAboutSize = false) const {
Jordy Rose9e49d9f2011-06-20 02:06:40 +0000155 // This is a convenience override.
Jordy Rose5e5f1502011-06-20 03:49:16 +0000156 return CheckBufferAccess(C, state, Size, Buf, NULL, message, NULL,
157 WarnAboutSize);
Jordy Rose9e49d9f2011-06-20 02:06:40 +0000158 }
Ted Kremenek8bef8232012-01-26 21:29:00 +0000159 ProgramStateRef CheckOverlap(CheckerContext &C,
160 ProgramStateRef state,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000161 const Expr *Size,
162 const Expr *First,
163 const Expr *Second) const;
164 void emitOverlapBug(CheckerContext &C,
Ted Kremenek8bef8232012-01-26 21:29:00 +0000165 ProgramStateRef state,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000166 const Stmt *First,
167 const Stmt *Second) const;
168
Ted Kremenek8bef8232012-01-26 21:29:00 +0000169 ProgramStateRef checkAdditionOverflow(CheckerContext &C,
170 ProgramStateRef state,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000171 NonLoc left,
172 NonLoc right) const;
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000173};
Jordy Rosea5261542010-08-14 21:02:52 +0000174
175class CStringLength {
176public:
177 typedef llvm::ImmutableMap<const MemRegion *, SVal> EntryMap;
178};
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000179} //end anonymous namespace
180
Jordy Rosea5261542010-08-14 21:02:52 +0000181namespace clang {
Ted Kremenek9ef65372010-12-23 07:20:52 +0000182namespace ento {
Jordy Rosea5261542010-08-14 21:02:52 +0000183 template <>
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000184 struct ProgramStateTrait<CStringLength>
185 : public ProgramStatePartialTrait<CStringLength::EntryMap> {
Jordy Rosea5261542010-08-14 21:02:52 +0000186 static void *GDMIndex() { return CStringChecker::getTag(); }
187 };
188}
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +0000189}
Jordy Rosea5261542010-08-14 21:02:52 +0000190
Jordy Rosed325ffb2010-07-08 23:57:29 +0000191//===----------------------------------------------------------------------===//
192// Individual checks and utility methods.
193//===----------------------------------------------------------------------===//
194
Ted Kremenek8bef8232012-01-26 21:29:00 +0000195std::pair<ProgramStateRef , ProgramStateRef >
196CStringChecker::assumeZero(CheckerContext &C, ProgramStateRef state, SVal V,
Jordy Rosed325ffb2010-07-08 23:57:29 +0000197 QualType Ty) {
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000198 DefinedSVal *val = dyn_cast<DefinedSVal>(&V);
199 if (!val)
Ted Kremenek8bef8232012-01-26 21:29:00 +0000200 return std::pair<ProgramStateRef , ProgramStateRef >(state, state);
Jordy Rosea6b808c2010-07-07 07:48:06 +0000201
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000202 SValBuilder &svalBuilder = C.getSValBuilder();
203 DefinedOrUnknownSVal zero = svalBuilder.makeZeroVal(Ty);
204 return state->assume(svalBuilder.evalEQ(state, *val, zero));
Jordy Rosed325ffb2010-07-08 23:57:29 +0000205}
Jordy Rosea6b808c2010-07-07 07:48:06 +0000206
Ted Kremenek8bef8232012-01-26 21:29:00 +0000207ProgramStateRef CStringChecker::checkNonNull(CheckerContext &C,
208 ProgramStateRef state,
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +0000209 const Expr *S, SVal l) const {
Jordy Rosed325ffb2010-07-08 23:57:29 +0000210 // If a previous check has failed, propagate the failure.
211 if (!state)
212 return NULL;
213
Ted Kremenek8bef8232012-01-26 21:29:00 +0000214 ProgramStateRef stateNull, stateNonNull;
Ted Kremenek28f47b92010-12-01 22:16:56 +0000215 llvm::tie(stateNull, stateNonNull) = assumeZero(C, state, l, S->getType());
Jordy Rosed325ffb2010-07-08 23:57:29 +0000216
217 if (stateNull && !stateNonNull) {
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000218 ExplodedNode *N = C.generateSink(stateNull);
Jordy Rosea6b808c2010-07-07 07:48:06 +0000219 if (!N)
220 return NULL;
221
Jordy Rosed325ffb2010-07-08 23:57:29 +0000222 if (!BT_Null)
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +0000223 BT_Null.reset(new BuiltinBug("API",
224 "Null pointer argument in call to byte string function"));
Jordy Rosea6b808c2010-07-07 07:48:06 +0000225
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000226 SmallString<80> buf;
Jordy Rose9e49d9f2011-06-20 02:06:40 +0000227 llvm::raw_svector_ostream os(buf);
228 assert(CurrentFunctionDescription);
229 os << "Null pointer argument in call to " << CurrentFunctionDescription;
230
Jordy Rosea6b808c2010-07-07 07:48:06 +0000231 // Generate a report for this bug.
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +0000232 BuiltinBug *BT = static_cast<BuiltinBug*>(BT_Null.get());
Anna Zakse172e8b2011-08-17 23:00:25 +0000233 BugReport *report = new BugReport(*BT, os.str(), N);
Jordy Rosea6b808c2010-07-07 07:48:06 +0000234
235 report->addRange(S->getSourceRange());
Anna Zaks50bbc162011-08-19 22:33:38 +0000236 report->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N, S));
Jordy Rosea6b808c2010-07-07 07:48:06 +0000237 C.EmitReport(report);
238 return NULL;
239 }
240
241 // From here on, assume that the value is non-null.
Jordy Rosed325ffb2010-07-08 23:57:29 +0000242 assert(stateNonNull);
243 return stateNonNull;
Jordy Rosea6b808c2010-07-07 07:48:06 +0000244}
245
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000246// FIXME: This was originally copied from ArrayBoundChecker.cpp. Refactor?
Ted Kremenek8bef8232012-01-26 21:29:00 +0000247ProgramStateRef CStringChecker::CheckLocation(CheckerContext &C,
248 ProgramStateRef state,
Jordy Rosee64f3112010-08-16 07:51:42 +0000249 const Expr *S, SVal l,
Jordy Rose9e49d9f2011-06-20 02:06:40 +0000250 const char *warningMsg) const {
Jordy Rosed325ffb2010-07-08 23:57:29 +0000251 // If a previous check has failed, propagate the failure.
252 if (!state)
253 return NULL;
254
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000255 // Check for out of bound array element access.
256 const MemRegion *R = l.getAsRegion();
257 if (!R)
258 return state;
259
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000260 const ElementRegion *ER = dyn_cast<ElementRegion>(R);
261 if (!ER)
262 return state;
263
Zhongxing Xu018220c2010-08-11 06:10:55 +0000264 assert(ER->getValueType() == C.getASTContext().CharTy &&
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000265 "CheckLocation should only be called with char* ElementRegions");
266
267 // Get the size of the array.
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000268 const SubRegion *superReg = cast<SubRegion>(ER->getSuperRegion());
269 SValBuilder &svalBuilder = C.getSValBuilder();
Jordy Rose1e022412011-06-16 05:51:02 +0000270 SVal Extent =
271 svalBuilder.convertToArrayIndex(superReg->getExtent(svalBuilder));
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000272 DefinedOrUnknownSVal Size = cast<DefinedOrUnknownSVal>(Extent);
273
274 // Get the index of the accessed element.
Gabor Greif89b06582010-09-09 10:51:37 +0000275 DefinedOrUnknownSVal Idx = cast<DefinedOrUnknownSVal>(ER->getIndex());
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000276
Ted Kremenek8bef8232012-01-26 21:29:00 +0000277 ProgramStateRef StInBound = state->assumeInBound(Idx, Size, true);
278 ProgramStateRef StOutBound = state->assumeInBound(Idx, Size, false);
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000279 if (StOutBound && !StInBound) {
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000280 ExplodedNode *N = C.generateSink(StOutBound);
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000281 if (!N)
282 return NULL;
283
Jordy Rose9e49d9f2011-06-20 02:06:40 +0000284 if (!BT_Bounds) {
285 BT_Bounds.reset(new BuiltinBug("Out-of-bound array access",
286 "Byte string function accesses out-of-bound array element"));
287 }
288 BuiltinBug *BT = static_cast<BuiltinBug*>(BT_Bounds.get());
289
290 // Generate a report for this bug.
Anna Zakse172e8b2011-08-17 23:00:25 +0000291 BugReport *report;
Jordy Rose9e49d9f2011-06-20 02:06:40 +0000292 if (warningMsg) {
Anna Zakse172e8b2011-08-17 23:00:25 +0000293 report = new BugReport(*BT, warningMsg, N);
Jordy Rosee64f3112010-08-16 07:51:42 +0000294 } else {
Jordy Rose9e49d9f2011-06-20 02:06:40 +0000295 assert(CurrentFunctionDescription);
296 assert(CurrentFunctionDescription[0] != '\0');
297
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000298 SmallString<80> buf;
Jordy Rose9e49d9f2011-06-20 02:06:40 +0000299 llvm::raw_svector_ostream os(buf);
300 os << (char)toupper(CurrentFunctionDescription[0])
301 << &CurrentFunctionDescription[1]
302 << " accesses out-of-bound array element";
Anna Zakse172e8b2011-08-17 23:00:25 +0000303 report = new BugReport(*BT, os.str(), N);
Jordy Rosee64f3112010-08-16 07:51:42 +0000304 }
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000305
306 // FIXME: It would be nice to eventually make this diagnostic more clear,
307 // e.g., by referencing the original declaration or by saying *why* this
308 // reference is outside the range.
309
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000310 report->addRange(S->getSourceRange());
311 C.EmitReport(report);
312 return NULL;
313 }
314
315 // Array bound check succeeded. From this point forward the array bound
316 // should always succeed.
317 return StInBound;
318}
319
Ted Kremenek8bef8232012-01-26 21:29:00 +0000320ProgramStateRef CStringChecker::CheckBufferAccess(CheckerContext &C,
321 ProgramStateRef state,
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000322 const Expr *Size,
323 const Expr *FirstBuf,
Jordy Rosee64f3112010-08-16 07:51:42 +0000324 const Expr *SecondBuf,
Jordy Rose9e49d9f2011-06-20 02:06:40 +0000325 const char *firstMessage,
Jordy Rose5e5f1502011-06-20 03:49:16 +0000326 const char *secondMessage,
327 bool WarnAboutSize) const {
Jordy Rosed325ffb2010-07-08 23:57:29 +0000328 // If a previous check has failed, propagate the failure.
329 if (!state)
330 return NULL;
331
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000332 SValBuilder &svalBuilder = C.getSValBuilder();
Jordy Rose1e022412011-06-16 05:51:02 +0000333 ASTContext &Ctx = svalBuilder.getContext();
Ted Kremenek5eca4822012-01-06 22:09:28 +0000334 const LocationContext *LCtx = C.getLocationContext();
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000335
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000336 QualType sizeTy = Size->getType();
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000337 QualType PtrTy = Ctx.getPointerType(Ctx.CharTy);
338
Jordy Rosea6b808c2010-07-07 07:48:06 +0000339 // Check that the first buffer is non-null.
Ted Kremenek5eca4822012-01-06 22:09:28 +0000340 SVal BufVal = state->getSVal(FirstBuf, LCtx);
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000341 state = checkNonNull(C, state, FirstBuf, BufVal);
Jordy Rosea6b808c2010-07-07 07:48:06 +0000342 if (!state)
343 return NULL;
344
Jordy Rosed325ffb2010-07-08 23:57:29 +0000345 // Get the access length and make sure it is known.
Jordy Rose9e49d9f2011-06-20 02:06:40 +0000346 // FIXME: This assumes the caller has already checked that the access length
347 // is positive. And that it's unsigned.
Ted Kremenek5eca4822012-01-06 22:09:28 +0000348 SVal LengthVal = state->getSVal(Size, LCtx);
Jordy Rosed325ffb2010-07-08 23:57:29 +0000349 NonLoc *Length = dyn_cast<NonLoc>(&LengthVal);
350 if (!Length)
351 return state;
352
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000353 // Compute the offset of the last element to be accessed: size-1.
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000354 NonLoc One = cast<NonLoc>(svalBuilder.makeIntVal(1, sizeTy));
355 NonLoc LastOffset = cast<NonLoc>(svalBuilder.evalBinOpNN(state, BO_Sub,
356 *Length, One, sizeTy));
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000357
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000358 // Check that the first buffer is sufficiently long.
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000359 SVal BufStart = svalBuilder.evalCast(BufVal, PtrTy, FirstBuf->getType());
Jordy Roseb6a40262010-08-05 23:11:30 +0000360 if (Loc *BufLoc = dyn_cast<Loc>(&BufStart)) {
Jordy Rose5e5f1502011-06-20 03:49:16 +0000361 const Expr *warningExpr = (WarnAboutSize ? Size : FirstBuf);
362
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000363 SVal BufEnd = svalBuilder.evalBinOpLN(state, BO_Add, *BufLoc,
364 LastOffset, PtrTy);
Jordy Rose5e5f1502011-06-20 03:49:16 +0000365 state = CheckLocation(C, state, warningExpr, BufEnd, firstMessage);
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000366
Jordy Roseb6a40262010-08-05 23:11:30 +0000367 // If the buffer isn't large enough, abort.
368 if (!state)
369 return NULL;
370 }
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000371
372 // If there's a second buffer, check it as well.
373 if (SecondBuf) {
Ted Kremenek5eca4822012-01-06 22:09:28 +0000374 BufVal = state->getSVal(SecondBuf, LCtx);
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000375 state = checkNonNull(C, state, SecondBuf, BufVal);
Jordy Rosea6b808c2010-07-07 07:48:06 +0000376 if (!state)
377 return NULL;
378
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000379 BufStart = svalBuilder.evalCast(BufVal, PtrTy, SecondBuf->getType());
Jordy Roseb6a40262010-08-05 23:11:30 +0000380 if (Loc *BufLoc = dyn_cast<Loc>(&BufStart)) {
Jordy Rose5e5f1502011-06-20 03:49:16 +0000381 const Expr *warningExpr = (WarnAboutSize ? Size : SecondBuf);
382
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000383 SVal BufEnd = svalBuilder.evalBinOpLN(state, BO_Add, *BufLoc,
384 LastOffset, PtrTy);
Jordy Rose5e5f1502011-06-20 03:49:16 +0000385 state = CheckLocation(C, state, warningExpr, BufEnd, secondMessage);
Jordy Roseb6a40262010-08-05 23:11:30 +0000386 }
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000387 }
388
389 // Large enough or not, return this state!
390 return state;
391}
392
Ted Kremenek8bef8232012-01-26 21:29:00 +0000393ProgramStateRef CStringChecker::CheckOverlap(CheckerContext &C,
394 ProgramStateRef state,
Jordy Rosed325ffb2010-07-08 23:57:29 +0000395 const Expr *Size,
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000396 const Expr *First,
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +0000397 const Expr *Second) const {
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000398 // Do a simple check for overlap: if the two arguments are from the same
399 // buffer, see if the end of the first is greater than the start of the second
400 // or vice versa.
401
Jordy Rosed325ffb2010-07-08 23:57:29 +0000402 // If a previous check has failed, propagate the failure.
403 if (!state)
404 return NULL;
405
Ted Kremenek8bef8232012-01-26 21:29:00 +0000406 ProgramStateRef stateTrue, stateFalse;
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000407
408 // Get the buffer values and make sure they're known locations.
Ted Kremenek5eca4822012-01-06 22:09:28 +0000409 const LocationContext *LCtx = C.getLocationContext();
410 SVal firstVal = state->getSVal(First, LCtx);
411 SVal secondVal = state->getSVal(Second, LCtx);
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000412
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000413 Loc *firstLoc = dyn_cast<Loc>(&firstVal);
414 if (!firstLoc)
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000415 return state;
416
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000417 Loc *secondLoc = dyn_cast<Loc>(&secondVal);
418 if (!secondLoc)
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000419 return state;
420
421 // Are the two values the same?
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000422 SValBuilder &svalBuilder = C.getSValBuilder();
423 llvm::tie(stateTrue, stateFalse) =
424 state->assume(svalBuilder.evalEQ(state, *firstLoc, *secondLoc));
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000425
426 if (stateTrue && !stateFalse) {
427 // If the values are known to be equal, that's automatically an overlap.
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000428 emitOverlapBug(C, stateTrue, First, Second);
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000429 return NULL;
430 }
431
Ted Kremenek28f47b92010-12-01 22:16:56 +0000432 // assume the two expressions are not equal.
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000433 assert(stateFalse);
434 state = stateFalse;
435
436 // Which value comes first?
Jordy Roseee2fde12011-06-16 05:56:50 +0000437 QualType cmpTy = svalBuilder.getConditionType();
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000438 SVal reverse = svalBuilder.evalBinOpLL(state, BO_GT,
439 *firstLoc, *secondLoc, cmpTy);
440 DefinedOrUnknownSVal *reverseTest = dyn_cast<DefinedOrUnknownSVal>(&reverse);
441 if (!reverseTest)
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000442 return state;
443
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000444 llvm::tie(stateTrue, stateFalse) = state->assume(*reverseTest);
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000445 if (stateTrue) {
446 if (stateFalse) {
447 // If we don't know which one comes first, we can't perform this test.
448 return state;
449 } else {
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000450 // Switch the values so that firstVal is before secondVal.
451 Loc *tmpLoc = firstLoc;
452 firstLoc = secondLoc;
453 secondLoc = tmpLoc;
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000454
455 // Switch the Exprs as well, so that they still correspond.
456 const Expr *tmpExpr = First;
457 First = Second;
458 Second = tmpExpr;
459 }
460 }
461
462 // Get the length, and make sure it too is known.
Ted Kremenek5eca4822012-01-06 22:09:28 +0000463 SVal LengthVal = state->getSVal(Size, LCtx);
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000464 NonLoc *Length = dyn_cast<NonLoc>(&LengthVal);
465 if (!Length)
466 return state;
467
468 // Convert the first buffer's start address to char*.
469 // Bail out if the cast fails.
Jordy Roseee2fde12011-06-16 05:56:50 +0000470 ASTContext &Ctx = svalBuilder.getContext();
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000471 QualType CharPtrTy = Ctx.getPointerType(Ctx.CharTy);
Jordy Rose1e022412011-06-16 05:51:02 +0000472 SVal FirstStart = svalBuilder.evalCast(*firstLoc, CharPtrTy,
473 First->getType());
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000474 Loc *FirstStartLoc = dyn_cast<Loc>(&FirstStart);
475 if (!FirstStartLoc)
476 return state;
477
478 // Compute the end of the first buffer. Bail out if THAT fails.
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000479 SVal FirstEnd = svalBuilder.evalBinOpLN(state, BO_Add,
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000480 *FirstStartLoc, *Length, CharPtrTy);
481 Loc *FirstEndLoc = dyn_cast<Loc>(&FirstEnd);
482 if (!FirstEndLoc)
483 return state;
484
485 // Is the end of the first buffer past the start of the second buffer?
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000486 SVal Overlap = svalBuilder.evalBinOpLL(state, BO_GT,
487 *FirstEndLoc, *secondLoc, cmpTy);
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000488 DefinedOrUnknownSVal *OverlapTest = dyn_cast<DefinedOrUnknownSVal>(&Overlap);
489 if (!OverlapTest)
490 return state;
491
Ted Kremenek28f47b92010-12-01 22:16:56 +0000492 llvm::tie(stateTrue, stateFalse) = state->assume(*OverlapTest);
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000493
494 if (stateTrue && !stateFalse) {
495 // Overlap!
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000496 emitOverlapBug(C, stateTrue, First, Second);
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000497 return NULL;
498 }
499
Ted Kremenek28f47b92010-12-01 22:16:56 +0000500 // assume the two expressions don't overlap.
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000501 assert(stateFalse);
502 return stateFalse;
503}
504
Ted Kremenek8bef8232012-01-26 21:29:00 +0000505void CStringChecker::emitOverlapBug(CheckerContext &C, ProgramStateRef state,
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +0000506 const Stmt *First, const Stmt *Second) const {
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000507 ExplodedNode *N = C.generateSink(state);
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000508 if (!N)
509 return;
510
511 if (!BT_Overlap)
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +0000512 BT_Overlap.reset(new BugType("Unix API", "Improper arguments"));
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000513
514 // Generate a report for this bug.
Anna Zakse172e8b2011-08-17 23:00:25 +0000515 BugReport *report =
516 new BugReport(*BT_Overlap,
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000517 "Arguments must not be overlapping buffers", N);
518 report->addRange(First->getSourceRange());
519 report->addRange(Second->getSourceRange());
520
521 C.EmitReport(report);
522}
523
Ted Kremenek8bef8232012-01-26 21:29:00 +0000524ProgramStateRef CStringChecker::checkAdditionOverflow(CheckerContext &C,
525 ProgramStateRef state,
Jordy Rosed5af0e12011-06-15 05:52:56 +0000526 NonLoc left,
527 NonLoc right) const {
528 // If a previous check has failed, propagate the failure.
529 if (!state)
530 return NULL;
531
532 SValBuilder &svalBuilder = C.getSValBuilder();
533 BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
534
535 QualType sizeTy = svalBuilder.getContext().getSizeType();
536 const llvm::APSInt &maxValInt = BVF.getMaxValue(sizeTy);
537 NonLoc maxVal = svalBuilder.makeIntVal(maxValInt);
538
Anna Zakse3d250e2011-12-11 18:43:40 +0000539 SVal maxMinusRight;
540 if (isa<nonloc::ConcreteInt>(right)) {
541 maxMinusRight = svalBuilder.evalBinOpNN(state, BO_Sub, maxVal, right,
542 sizeTy);
543 } else {
Jordy Rosed5af0e12011-06-15 05:52:56 +0000544 // Try switching the operands. (The order of these two assignments is
545 // important!)
546 maxMinusRight = svalBuilder.evalBinOpNN(state, BO_Sub, maxVal, left,
547 sizeTy);
548 left = right;
549 }
550
551 if (NonLoc *maxMinusRightNL = dyn_cast<NonLoc>(&maxMinusRight)) {
552 QualType cmpTy = svalBuilder.getConditionType();
553 // If left > max - right, we have an overflow.
554 SVal willOverflow = svalBuilder.evalBinOpNN(state, BO_GT, left,
555 *maxMinusRightNL, cmpTy);
556
Ted Kremenek8bef8232012-01-26 21:29:00 +0000557 ProgramStateRef stateOverflow, stateOkay;
Jordy Rosed5af0e12011-06-15 05:52:56 +0000558 llvm::tie(stateOverflow, stateOkay) =
559 state->assume(cast<DefinedOrUnknownSVal>(willOverflow));
560
561 if (stateOverflow && !stateOkay) {
562 // We have an overflow. Emit a bug report.
563 ExplodedNode *N = C.generateSink(stateOverflow);
564 if (!N)
565 return NULL;
566
567 if (!BT_AdditionOverflow)
568 BT_AdditionOverflow.reset(new BuiltinBug("API",
569 "Sum of expressions causes overflow"));
570
Jordy Rosed5af0e12011-06-15 05:52:56 +0000571 // This isn't a great error message, but this should never occur in real
572 // code anyway -- you'd have to create a buffer longer than a size_t can
573 // represent, which is sort of a contradiction.
Jordy Rose8cc24912011-06-20 03:51:53 +0000574 const char *warning =
575 "This expression will create a string whose length is too big to "
576 "be represented as a size_t";
Jordy Rosed5af0e12011-06-15 05:52:56 +0000577
578 // Generate a report for this bug.
Jordy Rose8cc24912011-06-20 03:51:53 +0000579 BugReport *report = new BugReport(*BT_AdditionOverflow, warning, N);
Jordy Rosed5af0e12011-06-15 05:52:56 +0000580 C.EmitReport(report);
581
582 return NULL;
583 }
584
585 // From now on, assume an overflow didn't occur.
586 assert(stateOkay);
587 state = stateOkay;
588 }
589
590 return state;
591}
592
Ted Kremenek8bef8232012-01-26 21:29:00 +0000593ProgramStateRef CStringChecker::setCStringLength(ProgramStateRef state,
Jordy Rosee64f3112010-08-16 07:51:42 +0000594 const MemRegion *MR,
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000595 SVal strLength) {
596 assert(!strLength.isUndef() && "Attempt to set an undefined string length");
Jordy Rosee64f3112010-08-16 07:51:42 +0000597
598 MR = MR->StripCasts();
599
600 switch (MR->getKind()) {
601 case MemRegion::StringRegionKind:
602 // FIXME: This can happen if we strcpy() into a string region. This is
603 // undefined [C99 6.4.5p6], but we should still warn about it.
604 return state;
605
606 case MemRegion::SymbolicRegionKind:
607 case MemRegion::AllocaRegionKind:
608 case MemRegion::VarRegionKind:
609 case MemRegion::FieldRegionKind:
610 case MemRegion::ObjCIvarRegionKind:
Jordy Rose210c05b2011-06-15 05:14:03 +0000611 // These are the types we can currently track string lengths for.
612 break;
Jordy Rosee64f3112010-08-16 07:51:42 +0000613
614 case MemRegion::ElementRegionKind:
615 // FIXME: Handle element regions by upper-bounding the parent region's
616 // string length.
617 return state;
618
619 default:
620 // Other regions (mostly non-data) can't have a reliable C string length.
621 // For now, just ignore the change.
622 // FIXME: These are rare but not impossible. We should output some kind of
623 // warning for things like strcpy((char[]){'a', 0}, "b");
624 return state;
625 }
Jordy Rose210c05b2011-06-15 05:14:03 +0000626
627 if (strLength.isUnknown())
628 return state->remove<CStringLength>(MR);
629
630 return state->set<CStringLength>(MR, strLength);
Jordy Rosee64f3112010-08-16 07:51:42 +0000631}
632
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000633SVal CStringChecker::getCStringLengthForRegion(CheckerContext &C,
Ted Kremenek8bef8232012-01-26 21:29:00 +0000634 ProgramStateRef &state,
Jordy Rosea5261542010-08-14 21:02:52 +0000635 const Expr *Ex,
Jordy Rosed5af0e12011-06-15 05:52:56 +0000636 const MemRegion *MR,
637 bool hypothetical) {
638 if (!hypothetical) {
639 // If there's a recorded length, go ahead and return it.
640 const SVal *Recorded = state->get<CStringLength>(MR);
641 if (Recorded)
642 return *Recorded;
643 }
Jordy Rosea5261542010-08-14 21:02:52 +0000644
645 // Otherwise, get a new symbol and update the state.
Anna Zaks5d0ea6d2011-10-04 20:43:05 +0000646 unsigned Count = C.getCurrentBlockCount();
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000647 SValBuilder &svalBuilder = C.getSValBuilder();
648 QualType sizeTy = svalBuilder.getContext().getSizeType();
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +0000649 SVal strLength = svalBuilder.getMetadataSymbolVal(CStringChecker::getTag(),
650 MR, Ex, sizeTy, Count);
Jordy Rosed5af0e12011-06-15 05:52:56 +0000651
652 if (!hypothetical)
653 state = state->set<CStringLength>(MR, strLength);
654
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000655 return strLength;
Jordy Rosea5261542010-08-14 21:02:52 +0000656}
657
Ted Kremenek8bef8232012-01-26 21:29:00 +0000658SVal CStringChecker::getCStringLength(CheckerContext &C, ProgramStateRef &state,
Jordy Rosed5af0e12011-06-15 05:52:56 +0000659 const Expr *Ex, SVal Buf,
660 bool hypothetical) const {
Jordy Rose19c5dd12010-07-27 01:37:31 +0000661 const MemRegion *MR = Buf.getAsRegion();
662 if (!MR) {
663 // If we can't get a region, see if it's something we /know/ isn't a
664 // C string. In the context of locations, the only time we can issue such
665 // a warning is for labels.
666 if (loc::GotoLabel *Label = dyn_cast<loc::GotoLabel>(&Buf)) {
Anna Zaks0bd6b112011-10-26 21:06:34 +0000667 if (ExplodedNode *N = C.addTransition(state)) {
Jordy Rose19c5dd12010-07-27 01:37:31 +0000668 if (!BT_NotCString)
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +0000669 BT_NotCString.reset(new BuiltinBug("API",
670 "Argument is not a null-terminated string."));
Jordy Rose19c5dd12010-07-27 01:37:31 +0000671
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000672 SmallString<120> buf;
Jordy Rose19c5dd12010-07-27 01:37:31 +0000673 llvm::raw_svector_ostream os(buf);
Jordy Rose9e49d9f2011-06-20 02:06:40 +0000674 assert(CurrentFunctionDescription);
675 os << "Argument to " << CurrentFunctionDescription
676 << " is the address of the label '" << Label->getLabel()->getName()
Jordy Rose19c5dd12010-07-27 01:37:31 +0000677 << "', which is not a null-terminated string";
678
679 // Generate a report for this bug.
Anna Zakse172e8b2011-08-17 23:00:25 +0000680 BugReport *report = new BugReport(*BT_NotCString,
Jordy Rose19c5dd12010-07-27 01:37:31 +0000681 os.str(), N);
682
683 report->addRange(Ex->getSourceRange());
684 C.EmitReport(report);
685 }
686
687 return UndefinedVal();
688 }
689
Jordy Rosea5261542010-08-14 21:02:52 +0000690 // If it's not a region and not a label, give up.
691 return UnknownVal();
Jordy Rose19c5dd12010-07-27 01:37:31 +0000692 }
693
Jordy Rosea5261542010-08-14 21:02:52 +0000694 // If we have a region, strip casts from it and see if we can figure out
695 // its length. For anything we can't figure out, just return UnknownVal.
696 MR = MR->StripCasts();
697
698 switch (MR->getKind()) {
699 case MemRegion::StringRegionKind: {
700 // Modifying the contents of string regions is undefined [C99 6.4.5p6],
701 // so we can assume that the byte length is the correct C string length.
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000702 SValBuilder &svalBuilder = C.getSValBuilder();
703 QualType sizeTy = svalBuilder.getContext().getSizeType();
704 const StringLiteral *strLit = cast<StringRegion>(MR)->getStringLiteral();
705 return svalBuilder.makeIntVal(strLit->getByteLength(), sizeTy);
Jordy Rosea5261542010-08-14 21:02:52 +0000706 }
707 case MemRegion::SymbolicRegionKind:
708 case MemRegion::AllocaRegionKind:
709 case MemRegion::VarRegionKind:
710 case MemRegion::FieldRegionKind:
711 case MemRegion::ObjCIvarRegionKind:
Jordy Rosed5af0e12011-06-15 05:52:56 +0000712 return getCStringLengthForRegion(C, state, Ex, MR, hypothetical);
Jordy Rosea5261542010-08-14 21:02:52 +0000713 case MemRegion::CompoundLiteralRegionKind:
714 // FIXME: Can we track this? Is it necessary?
715 return UnknownVal();
716 case MemRegion::ElementRegionKind:
717 // FIXME: How can we handle this? It's not good enough to subtract the
718 // offset from the base string length; consider "123\x00567" and &a[5].
719 return UnknownVal();
720 default:
721 // Other regions (mostly non-data) can't have a reliable C string length.
722 // In this case, an error is emitted and UndefinedVal is returned.
723 // The caller should always be prepared to handle this case.
Anna Zaks0bd6b112011-10-26 21:06:34 +0000724 if (ExplodedNode *N = C.addTransition(state)) {
Jordy Rosea5261542010-08-14 21:02:52 +0000725 if (!BT_NotCString)
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +0000726 BT_NotCString.reset(new BuiltinBug("API",
727 "Argument is not a null-terminated string."));
Jordy Rosea5261542010-08-14 21:02:52 +0000728
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000729 SmallString<120> buf;
Jordy Rosea5261542010-08-14 21:02:52 +0000730 llvm::raw_svector_ostream os(buf);
731
Jordy Rose9e49d9f2011-06-20 02:06:40 +0000732 assert(CurrentFunctionDescription);
733 os << "Argument to " << CurrentFunctionDescription << " is ";
Jordy Rosea5261542010-08-14 21:02:52 +0000734
735 if (SummarizeRegion(os, C.getASTContext(), MR))
736 os << ", which is not a null-terminated string";
737 else
738 os << "not a null-terminated string";
739
740 // Generate a report for this bug.
Anna Zakse172e8b2011-08-17 23:00:25 +0000741 BugReport *report = new BugReport(*BT_NotCString,
Jordy Rosea5261542010-08-14 21:02:52 +0000742 os.str(), N);
743
744 report->addRange(Ex->getSourceRange());
745 C.EmitReport(report);
746 }
747
748 return UndefinedVal();
749 }
Jordy Rose19c5dd12010-07-27 01:37:31 +0000750}
751
Lenny Maiorani318dd922011-04-12 17:08:43 +0000752const StringLiteral *CStringChecker::getCStringLiteral(CheckerContext &C,
Ted Kremenek8bef8232012-01-26 21:29:00 +0000753 ProgramStateRef &state, const Expr *expr, SVal val) const {
Lenny Maiorani318dd922011-04-12 17:08:43 +0000754
755 // Get the memory region pointed to by the val.
756 const MemRegion *bufRegion = val.getAsRegion();
757 if (!bufRegion)
758 return NULL;
759
760 // Strip casts off the memory region.
761 bufRegion = bufRegion->StripCasts();
762
763 // Cast the memory region to a string region.
764 const StringRegion *strRegion= dyn_cast<StringRegion>(bufRegion);
765 if (!strRegion)
766 return NULL;
767
768 // Return the actual string in the string region.
769 return strRegion->getStringLiteral();
770}
771
Ted Kremenek8bef8232012-01-26 21:29:00 +0000772ProgramStateRef CStringChecker::InvalidateBuffer(CheckerContext &C,
773 ProgramStateRef state,
Jordy Rosee64f3112010-08-16 07:51:42 +0000774 const Expr *E, SVal V) {
775 Loc *L = dyn_cast<Loc>(&V);
776 if (!L)
777 return state;
778
779 // FIXME: This is a simplified version of what's in CFRefCount.cpp -- it makes
780 // some assumptions about the value that CFRefCount can't. Even so, it should
781 // probably be refactored.
782 if (loc::MemRegionVal* MR = dyn_cast<loc::MemRegionVal>(L)) {
783 const MemRegion *R = MR->getRegion()->StripCasts();
784
785 // Are we dealing with an ElementRegion? If so, we should be invalidating
786 // the super-region.
787 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
788 R = ER->getSuperRegion();
789 // FIXME: What about layers of ElementRegions?
790 }
791
792 // Invalidate this region.
Anna Zaks5d0ea6d2011-10-04 20:43:05 +0000793 unsigned Count = C.getCurrentBlockCount();
Jordy Rose537716a2011-08-27 22:51:26 +0000794 return state->invalidateRegions(R, E, Count);
Jordy Rosee64f3112010-08-16 07:51:42 +0000795 }
796
797 // If we have a non-region value by chance, just remove the binding.
798 // FIXME: is this necessary or correct? This handles the non-Region
799 // cases. Is it ever valid to store to these?
800 return state->unbindLoc(*L);
801}
802
Ted Kremenek9c378f72011-08-12 23:37:29 +0000803bool CStringChecker::SummarizeRegion(raw_ostream &os, ASTContext &Ctx,
Jordy Rose19c5dd12010-07-27 01:37:31 +0000804 const MemRegion *MR) {
Ted Kremenek96979342011-08-12 20:02:48 +0000805 const TypedValueRegion *TVR = dyn_cast<TypedValueRegion>(MR);
Jordy Rose19c5dd12010-07-27 01:37:31 +0000806
Jordy Rose096aef92011-08-12 21:41:07 +0000807 switch (MR->getKind()) {
Jordy Rose19c5dd12010-07-27 01:37:31 +0000808 case MemRegion::FunctionTextRegionKind: {
Jordy Rose096aef92011-08-12 21:41:07 +0000809 const FunctionDecl *FD = cast<FunctionTextRegion>(MR)->getDecl();
Jordy Rose19c5dd12010-07-27 01:37:31 +0000810 if (FD)
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000811 os << "the address of the function '" << *FD << '\'';
Jordy Rose19c5dd12010-07-27 01:37:31 +0000812 else
813 os << "the address of a function";
814 return true;
815 }
816 case MemRegion::BlockTextRegionKind:
817 os << "block text";
818 return true;
819 case MemRegion::BlockDataRegionKind:
820 os << "a block";
821 return true;
822 case MemRegion::CXXThisRegionKind:
Zhongxing Xu02fe28c2010-11-26 08:52:48 +0000823 case MemRegion::CXXTempObjectRegionKind:
Ted Kremenek96979342011-08-12 20:02:48 +0000824 os << "a C++ temp object of type " << TVR->getValueType().getAsString();
Jordy Rose19c5dd12010-07-27 01:37:31 +0000825 return true;
826 case MemRegion::VarRegionKind:
Ted Kremenek96979342011-08-12 20:02:48 +0000827 os << "a variable of type" << TVR->getValueType().getAsString();
Jordy Rose19c5dd12010-07-27 01:37:31 +0000828 return true;
829 case MemRegion::FieldRegionKind:
Ted Kremenek96979342011-08-12 20:02:48 +0000830 os << "a field of type " << TVR->getValueType().getAsString();
Jordy Rose19c5dd12010-07-27 01:37:31 +0000831 return true;
832 case MemRegion::ObjCIvarRegionKind:
Ted Kremenek96979342011-08-12 20:02:48 +0000833 os << "an instance variable of type " << TVR->getValueType().getAsString();
Jordy Rose19c5dd12010-07-27 01:37:31 +0000834 return true;
835 default:
836 return false;
837 }
838}
839
Jordy Rosed325ffb2010-07-08 23:57:29 +0000840//===----------------------------------------------------------------------===//
Ted Kremenek9c149532010-12-01 21:57:22 +0000841// evaluation of individual function calls.
Jordy Rosed325ffb2010-07-08 23:57:29 +0000842//===----------------------------------------------------------------------===//
843
Lenny Maioranib8b875b2011-03-31 21:36:53 +0000844void CStringChecker::evalCopyCommon(CheckerContext &C,
845 const CallExpr *CE,
Ted Kremenek8bef8232012-01-26 21:29:00 +0000846 ProgramStateRef state,
Jordy Rosed325ffb2010-07-08 23:57:29 +0000847 const Expr *Size, const Expr *Dest,
Lenny Maioranib8b875b2011-03-31 21:36:53 +0000848 const Expr *Source, bool Restricted,
849 bool IsMempcpy) const {
Jordy Rose9e49d9f2011-06-20 02:06:40 +0000850 CurrentFunctionDescription = "memory copy function";
851
Jordy Rosed325ffb2010-07-08 23:57:29 +0000852 // See if the size argument is zero.
Ted Kremenek5eca4822012-01-06 22:09:28 +0000853 const LocationContext *LCtx = C.getLocationContext();
854 SVal sizeVal = state->getSVal(Size, LCtx);
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000855 QualType sizeTy = Size->getType();
Jordy Rosed325ffb2010-07-08 23:57:29 +0000856
Ted Kremenek8bef8232012-01-26 21:29:00 +0000857 ProgramStateRef stateZeroSize, stateNonZeroSize;
Jordy Rose1e022412011-06-16 05:51:02 +0000858 llvm::tie(stateZeroSize, stateNonZeroSize) =
859 assumeZero(C, state, sizeVal, sizeTy);
Jordy Rosed325ffb2010-07-08 23:57:29 +0000860
Lenny Maioranib8b875b2011-03-31 21:36:53 +0000861 // Get the value of the Dest.
Ted Kremenek5eca4822012-01-06 22:09:28 +0000862 SVal destVal = state->getSVal(Dest, LCtx);
Lenny Maioranib8b875b2011-03-31 21:36:53 +0000863
864 // If the size is zero, there won't be any actual memory access, so
865 // just bind the return value to the destination buffer and return.
866 if (stateZeroSize) {
Ted Kremenek5eca4822012-01-06 22:09:28 +0000867 stateZeroSize = stateZeroSize->BindExpr(CE, LCtx, destVal);
Anna Zaks0bd6b112011-10-26 21:06:34 +0000868 C.addTransition(stateZeroSize);
Lenny Maioranib8b875b2011-03-31 21:36:53 +0000869 }
Jordy Rosed325ffb2010-07-08 23:57:29 +0000870
871 // If the size can be nonzero, we have to check the other arguments.
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000872 if (stateNonZeroSize) {
Jordy Rose22d27172011-06-04 00:04:22 +0000873 state = stateNonZeroSize;
Lenny Maioranib8b875b2011-03-31 21:36:53 +0000874
875 // Ensure the destination is not null. If it is NULL there will be a
876 // NULL pointer dereference.
877 state = checkNonNull(C, state, Dest, destVal);
878 if (!state)
879 return;
880
881 // Get the value of the Src.
Ted Kremenek5eca4822012-01-06 22:09:28 +0000882 SVal srcVal = state->getSVal(Source, LCtx);
Lenny Maioranib8b875b2011-03-31 21:36:53 +0000883
884 // Ensure the source is not null. If it is NULL there will be a
885 // NULL pointer dereference.
886 state = checkNonNull(C, state, Source, srcVal);
887 if (!state)
888 return;
889
Jordy Rose7182b962011-06-04 01:50:25 +0000890 // Ensure the accesses are valid and that the buffers do not overlap.
Jordy Rose9e49d9f2011-06-20 02:06:40 +0000891 const char * const writeWarning =
892 "Memory copy function overflows destination buffer";
Jordy Rosee64f3112010-08-16 07:51:42 +0000893 state = CheckBufferAccess(C, state, Size, Dest, Source,
Jordy Rose9e49d9f2011-06-20 02:06:40 +0000894 writeWarning, /* sourceWarning = */ NULL);
Jordy Rosed325ffb2010-07-08 23:57:29 +0000895 if (Restricted)
896 state = CheckOverlap(C, state, Size, Dest, Source);
Jordy Rosee64f3112010-08-16 07:51:42 +0000897
Jordy Rose7182b962011-06-04 01:50:25 +0000898 if (!state)
899 return;
Lenny Maioranib8b875b2011-03-31 21:36:53 +0000900
Jordy Rose7182b962011-06-04 01:50:25 +0000901 // If this is mempcpy, get the byte after the last byte copied and
902 // bind the expr.
903 if (IsMempcpy) {
904 loc::MemRegionVal *destRegVal = dyn_cast<loc::MemRegionVal>(&destVal);
905 assert(destRegVal && "Destination should be a known MemRegionVal here");
906
907 // Get the length to copy.
908 NonLoc *lenValNonLoc = dyn_cast<NonLoc>(&sizeVal);
909
910 if (lenValNonLoc) {
911 // Get the byte after the last byte copied.
912 SVal lastElement = C.getSValBuilder().evalBinOpLN(state, BO_Add,
913 *destRegVal,
914 *lenValNonLoc,
915 Dest->getType());
916
917 // The byte after the last byte copied is the return value.
Ted Kremenek5eca4822012-01-06 22:09:28 +0000918 state = state->BindExpr(CE, LCtx, lastElement);
Jordy Rose3f8bb2f2011-06-04 01:47:27 +0000919 } else {
Jordy Rose7182b962011-06-04 01:50:25 +0000920 // If we don't know how much we copied, we can at least
921 // conjure a return value for later.
Anna Zaks5d0ea6d2011-10-04 20:43:05 +0000922 unsigned Count = C.getCurrentBlockCount();
Jordy Rose7182b962011-06-04 01:50:25 +0000923 SVal result =
924 C.getSValBuilder().getConjuredSymbolVal(NULL, CE, Count);
Ted Kremenek5eca4822012-01-06 22:09:28 +0000925 state = state->BindExpr(CE, LCtx, result);
Lenny Maioranib8b875b2011-03-31 21:36:53 +0000926 }
927
Jordy Rose7182b962011-06-04 01:50:25 +0000928 } else {
929 // All other copies return the destination buffer.
930 // (Well, bcopy() has a void return type, but this won't hurt.)
Ted Kremenek5eca4822012-01-06 22:09:28 +0000931 state = state->BindExpr(CE, LCtx, destVal);
Jordy Rosee64f3112010-08-16 07:51:42 +0000932 }
Jordy Rose7182b962011-06-04 01:50:25 +0000933
934 // Invalidate the destination.
935 // FIXME: Even if we can't perfectly model the copy, we should see if we
936 // can use LazyCompoundVals to copy the source values into the destination.
937 // This would probably remove any existing bindings past the end of the
938 // copied region, but that's still an improvement over blank invalidation.
Ted Kremenek5eca4822012-01-06 22:09:28 +0000939 state = InvalidateBuffer(C, state, Dest,
940 state->getSVal(Dest, C.getLocationContext()));
Anna Zaks0bd6b112011-10-26 21:06:34 +0000941 C.addTransition(state);
Jordy Rosed325ffb2010-07-08 23:57:29 +0000942 }
943}
944
945
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +0000946void CStringChecker::evalMemcpy(CheckerContext &C, const CallExpr *CE) const {
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000947 // void *memcpy(void *restrict dst, const void *restrict src, size_t n);
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000948 // The return value is the address of the destination buffer.
Jordy Rosed325ffb2010-07-08 23:57:29 +0000949 const Expr *Dest = CE->getArg(0);
Ted Kremenek8bef8232012-01-26 21:29:00 +0000950 ProgramStateRef state = C.getState();
Jordy Rose3f8bb2f2011-06-04 01:47:27 +0000951
Lenny Maioranib8b875b2011-03-31 21:36:53 +0000952 evalCopyCommon(C, CE, state, CE->getArg(2), Dest, CE->getArg(1), true);
953}
954
955void CStringChecker::evalMempcpy(CheckerContext &C, const CallExpr *CE) const {
956 // void *mempcpy(void *restrict dst, const void *restrict src, size_t n);
957 // The return value is a pointer to the byte following the last written byte.
958 const Expr *Dest = CE->getArg(0);
Ted Kremenek8bef8232012-01-26 21:29:00 +0000959 ProgramStateRef state = C.getState();
Lenny Maioranib8b875b2011-03-31 21:36:53 +0000960
961 evalCopyCommon(C, CE, state, CE->getArg(2), Dest, CE->getArg(1), true, true);
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000962}
963
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +0000964void CStringChecker::evalMemmove(CheckerContext &C, const CallExpr *CE) const {
Jordy Rosed325ffb2010-07-08 23:57:29 +0000965 // void *memmove(void *dst, const void *src, size_t n);
966 // The return value is the address of the destination buffer.
967 const Expr *Dest = CE->getArg(0);
Ted Kremenek8bef8232012-01-26 21:29:00 +0000968 ProgramStateRef state = C.getState();
Jordy Rose3f8bb2f2011-06-04 01:47:27 +0000969
Lenny Maioranib8b875b2011-03-31 21:36:53 +0000970 evalCopyCommon(C, CE, state, CE->getArg(2), Dest, CE->getArg(1));
Jordy Rosed325ffb2010-07-08 23:57:29 +0000971}
972
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +0000973void CStringChecker::evalBcopy(CheckerContext &C, const CallExpr *CE) const {
Jordy Rosed325ffb2010-07-08 23:57:29 +0000974 // void bcopy(const void *src, void *dst, size_t n);
Lenny Maioranib8b875b2011-03-31 21:36:53 +0000975 evalCopyCommon(C, CE, C.getState(),
976 CE->getArg(2), CE->getArg(1), CE->getArg(0));
Jordy Rosed325ffb2010-07-08 23:57:29 +0000977}
978
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +0000979void CStringChecker::evalMemcmp(CheckerContext &C, const CallExpr *CE) const {
Jordy Rosebc56d1f2010-07-07 08:15:01 +0000980 // int memcmp(const void *s1, const void *s2, size_t n);
Jordy Rose9e49d9f2011-06-20 02:06:40 +0000981 CurrentFunctionDescription = "memory comparison function";
982
Jordy Rosebc56d1f2010-07-07 08:15:01 +0000983 const Expr *Left = CE->getArg(0);
984 const Expr *Right = CE->getArg(1);
985 const Expr *Size = CE->getArg(2);
986
Ted Kremenek8bef8232012-01-26 21:29:00 +0000987 ProgramStateRef state = C.getState();
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000988 SValBuilder &svalBuilder = C.getSValBuilder();
Jordy Rosebc56d1f2010-07-07 08:15:01 +0000989
Jordy Rosed325ffb2010-07-08 23:57:29 +0000990 // See if the size argument is zero.
Ted Kremenek5eca4822012-01-06 22:09:28 +0000991 const LocationContext *LCtx = C.getLocationContext();
992 SVal sizeVal = state->getSVal(Size, LCtx);
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000993 QualType sizeTy = Size->getType();
Jordy Rosebc56d1f2010-07-07 08:15:01 +0000994
Ted Kremenek8bef8232012-01-26 21:29:00 +0000995 ProgramStateRef stateZeroSize, stateNonZeroSize;
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000996 llvm::tie(stateZeroSize, stateNonZeroSize) =
997 assumeZero(C, state, sizeVal, sizeTy);
Jordy Rosebc56d1f2010-07-07 08:15:01 +0000998
Jordy Rosed325ffb2010-07-08 23:57:29 +0000999 // If the size can be zero, the result will be 0 in that case, and we don't
1000 // have to check either of the buffers.
Ted Kremenekc8413fd2010-12-02 07:49:45 +00001001 if (stateZeroSize) {
1002 state = stateZeroSize;
Ted Kremenek5eca4822012-01-06 22:09:28 +00001003 state = state->BindExpr(CE, LCtx,
1004 svalBuilder.makeZeroVal(CE->getType()));
Anna Zaks0bd6b112011-10-26 21:06:34 +00001005 C.addTransition(state);
Jordy Rosebc56d1f2010-07-07 08:15:01 +00001006 }
1007
Jordy Rosed325ffb2010-07-08 23:57:29 +00001008 // If the size can be nonzero, we have to check the other arguments.
Ted Kremenekc8413fd2010-12-02 07:49:45 +00001009 if (stateNonZeroSize) {
1010 state = stateNonZeroSize;
Jordy Rosed325ffb2010-07-08 23:57:29 +00001011 // If we know the two buffers are the same, we know the result is 0.
1012 // First, get the two buffers' addresses. Another checker will have already
1013 // made sure they're not undefined.
Ted Kremenek5eca4822012-01-06 22:09:28 +00001014 DefinedOrUnknownSVal LV =
1015 cast<DefinedOrUnknownSVal>(state->getSVal(Left, LCtx));
1016 DefinedOrUnknownSVal RV =
1017 cast<DefinedOrUnknownSVal>(state->getSVal(Right, LCtx));
Jordy Rosebc56d1f2010-07-07 08:15:01 +00001018
Jordy Rosed325ffb2010-07-08 23:57:29 +00001019 // See if they are the same.
Ted Kremenekc8413fd2010-12-02 07:49:45 +00001020 DefinedOrUnknownSVal SameBuf = svalBuilder.evalEQ(state, LV, RV);
Ted Kremenek8bef8232012-01-26 21:29:00 +00001021 ProgramStateRef StSameBuf, StNotSameBuf;
Ted Kremenek28f47b92010-12-01 22:16:56 +00001022 llvm::tie(StSameBuf, StNotSameBuf) = state->assume(SameBuf);
Jordy Rosed325ffb2010-07-08 23:57:29 +00001023
Jordy Rose1e022412011-06-16 05:51:02 +00001024 // If the two arguments might be the same buffer, we know the result is 0,
Jordy Rosed325ffb2010-07-08 23:57:29 +00001025 // and we only need to check one size.
1026 if (StSameBuf) {
1027 state = StSameBuf;
1028 state = CheckBufferAccess(C, state, Size, Left);
1029 if (state) {
Ted Kremenek5eca4822012-01-06 22:09:28 +00001030 state = StSameBuf->BindExpr(CE, LCtx,
1031 svalBuilder.makeZeroVal(CE->getType()));
Anna Zaks0bd6b112011-10-26 21:06:34 +00001032 C.addTransition(state);
Jordy Rosed325ffb2010-07-08 23:57:29 +00001033 }
1034 }
1035
1036 // If the two arguments might be different buffers, we have to check the
1037 // size of both of them.
1038 if (StNotSameBuf) {
1039 state = StNotSameBuf;
1040 state = CheckBufferAccess(C, state, Size, Left, Right);
1041 if (state) {
1042 // The return value is the comparison result, which we don't know.
Anna Zaks5d0ea6d2011-10-04 20:43:05 +00001043 unsigned Count = C.getCurrentBlockCount();
Ted Kremenekc8413fd2010-12-02 07:49:45 +00001044 SVal CmpV = svalBuilder.getConjuredSymbolVal(NULL, CE, Count);
Ted Kremenek5eca4822012-01-06 22:09:28 +00001045 state = state->BindExpr(CE, LCtx, CmpV);
Anna Zaks0bd6b112011-10-26 21:06:34 +00001046 C.addTransition(state);
Jordy Rosed325ffb2010-07-08 23:57:29 +00001047 }
1048 }
1049 }
Jordy Rosebc56d1f2010-07-07 08:15:01 +00001050}
1051
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +00001052void CStringChecker::evalstrLength(CheckerContext &C,
1053 const CallExpr *CE) const {
Jordy Rose19c5dd12010-07-27 01:37:31 +00001054 // size_t strlen(const char *s);
Ted Kremenekbe4242c2011-02-22 04:55:05 +00001055 evalstrLengthCommon(C, CE, /* IsStrnlen = */ false);
1056}
1057
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +00001058void CStringChecker::evalstrnLength(CheckerContext &C,
1059 const CallExpr *CE) const {
Ted Kremenekbe4242c2011-02-22 04:55:05 +00001060 // size_t strnlen(const char *s, size_t maxlen);
1061 evalstrLengthCommon(C, CE, /* IsStrnlen = */ true);
1062}
1063
1064void CStringChecker::evalstrLengthCommon(CheckerContext &C, const CallExpr *CE,
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +00001065 bool IsStrnlen) const {
Jordy Rose9e49d9f2011-06-20 02:06:40 +00001066 CurrentFunctionDescription = "string length function";
Ted Kremenek8bef8232012-01-26 21:29:00 +00001067 ProgramStateRef state = C.getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +00001068 const LocationContext *LCtx = C.getLocationContext();
Jordy Rose793bff32011-06-14 01:15:31 +00001069
1070 if (IsStrnlen) {
1071 const Expr *maxlenExpr = CE->getArg(1);
Ted Kremenek5eca4822012-01-06 22:09:28 +00001072 SVal maxlenVal = state->getSVal(maxlenExpr, LCtx);
Jordy Rose793bff32011-06-14 01:15:31 +00001073
Ted Kremenek8bef8232012-01-26 21:29:00 +00001074 ProgramStateRef stateZeroSize, stateNonZeroSize;
Jordy Rose793bff32011-06-14 01:15:31 +00001075 llvm::tie(stateZeroSize, stateNonZeroSize) =
1076 assumeZero(C, state, maxlenVal, maxlenExpr->getType());
1077
1078 // If the size can be zero, the result will be 0 in that case, and we don't
1079 // have to check the string itself.
1080 if (stateZeroSize) {
1081 SVal zero = C.getSValBuilder().makeZeroVal(CE->getType());
Ted Kremenek5eca4822012-01-06 22:09:28 +00001082 stateZeroSize = stateZeroSize->BindExpr(CE, LCtx, zero);
Anna Zaks0bd6b112011-10-26 21:06:34 +00001083 C.addTransition(stateZeroSize);
Jordy Rose793bff32011-06-14 01:15:31 +00001084 }
1085
1086 // If the size is GUARANTEED to be zero, we're done!
1087 if (!stateNonZeroSize)
1088 return;
1089
1090 // Otherwise, record the assumption that the size is nonzero.
1091 state = stateNonZeroSize;
1092 }
1093
1094 // Check that the string argument is non-null.
Jordy Rose19c5dd12010-07-27 01:37:31 +00001095 const Expr *Arg = CE->getArg(0);
Ted Kremenek5eca4822012-01-06 22:09:28 +00001096 SVal ArgVal = state->getSVal(Arg, LCtx);
Jordy Rose19c5dd12010-07-27 01:37:31 +00001097
Ted Kremenekc8413fd2010-12-02 07:49:45 +00001098 state = checkNonNull(C, state, Arg, ArgVal);
Jordy Rose19c5dd12010-07-27 01:37:31 +00001099
Jordy Rosebd32bee2011-06-14 01:26:48 +00001100 if (!state)
1101 return;
Jordy Rosea5261542010-08-14 21:02:52 +00001102
Jordy Rosebd32bee2011-06-14 01:26:48 +00001103 SVal strLength = getCStringLength(C, state, Arg, ArgVal);
Jordy Rosea5261542010-08-14 21:02:52 +00001104
Jordy Rosebd32bee2011-06-14 01:26:48 +00001105 // If the argument isn't a valid C string, there's no valid state to
1106 // transition to.
1107 if (strLength.isUndef())
1108 return;
Jordy Rose793bff32011-06-14 01:15:31 +00001109
Jordy Rosebd32bee2011-06-14 01:26:48 +00001110 DefinedOrUnknownSVal result = UnknownVal();
Jordy Rose793bff32011-06-14 01:15:31 +00001111
Jordy Rosebd32bee2011-06-14 01:26:48 +00001112 // If the check is for strnlen() then bind the return value to no more than
1113 // the maxlen value.
1114 if (IsStrnlen) {
Jordy Roseee2fde12011-06-16 05:56:50 +00001115 QualType cmpTy = C.getSValBuilder().getConditionType();
Jordy Rose793bff32011-06-14 01:15:31 +00001116
Jordy Rosebd32bee2011-06-14 01:26:48 +00001117 // It's a little unfortunate to be getting this again,
1118 // but it's not that expensive...
1119 const Expr *maxlenExpr = CE->getArg(1);
Ted Kremenek5eca4822012-01-06 22:09:28 +00001120 SVal maxlenVal = state->getSVal(maxlenExpr, LCtx);
Ted Kremenekbe4242c2011-02-22 04:55:05 +00001121
Jordy Rosebd32bee2011-06-14 01:26:48 +00001122 NonLoc *strLengthNL = dyn_cast<NonLoc>(&strLength);
1123 NonLoc *maxlenValNL = dyn_cast<NonLoc>(&maxlenVal);
Ted Kremenekbe4242c2011-02-22 04:55:05 +00001124
Jordy Rosebd32bee2011-06-14 01:26:48 +00001125 if (strLengthNL && maxlenValNL) {
Ted Kremenek8bef8232012-01-26 21:29:00 +00001126 ProgramStateRef stateStringTooLong, stateStringNotTooLong;
Jordy Rose793bff32011-06-14 01:15:31 +00001127
Jordy Rosebd32bee2011-06-14 01:26:48 +00001128 // Check if the strLength is greater than the maxlen.
1129 llvm::tie(stateStringTooLong, stateStringNotTooLong) =
1130 state->assume(cast<DefinedOrUnknownSVal>
1131 (C.getSValBuilder().evalBinOpNN(state, BO_GT,
1132 *strLengthNL,
1133 *maxlenValNL,
1134 cmpTy)));
Jordy Rose793bff32011-06-14 01:15:31 +00001135
Jordy Rosebd32bee2011-06-14 01:26:48 +00001136 if (stateStringTooLong && !stateStringNotTooLong) {
1137 // If the string is longer than maxlen, return maxlen.
1138 result = *maxlenValNL;
1139 } else if (stateStringNotTooLong && !stateStringTooLong) {
1140 // If the string is shorter than maxlen, return its length.
1141 result = *strLengthNL;
Ted Kremenekbe4242c2011-02-22 04:55:05 +00001142 }
1143 }
1144
Jordy Rosebd32bee2011-06-14 01:26:48 +00001145 if (result.isUnknown()) {
1146 // If we don't have enough information for a comparison, there's
1147 // no guarantee the full string length will actually be returned.
1148 // All we know is the return value is the min of the string length
1149 // and the limit. This is better than nothing.
Anna Zaks5d0ea6d2011-10-04 20:43:05 +00001150 unsigned Count = C.getCurrentBlockCount();
Jordy Rosebd32bee2011-06-14 01:26:48 +00001151 result = C.getSValBuilder().getConjuredSymbolVal(NULL, CE, Count);
1152 NonLoc *resultNL = cast<NonLoc>(&result);
1153
1154 if (strLengthNL) {
1155 state = state->assume(cast<DefinedOrUnknownSVal>
1156 (C.getSValBuilder().evalBinOpNN(state, BO_LE,
1157 *resultNL,
1158 *strLengthNL,
1159 cmpTy)), true);
1160 }
1161
1162 if (maxlenValNL) {
1163 state = state->assume(cast<DefinedOrUnknownSVal>
1164 (C.getSValBuilder().evalBinOpNN(state, BO_LE,
1165 *resultNL,
1166 *maxlenValNL,
1167 cmpTy)), true);
1168 }
1169 }
1170
1171 } else {
1172 // This is a plain strlen(), not strnlen().
1173 result = cast<DefinedOrUnknownSVal>(strLength);
1174
1175 // If we don't know the length of the string, conjure a return
1176 // value, so it can be used in constraints, at least.
1177 if (result.isUnknown()) {
Anna Zaks5d0ea6d2011-10-04 20:43:05 +00001178 unsigned Count = C.getCurrentBlockCount();
Jordy Rosebd32bee2011-06-14 01:26:48 +00001179 result = C.getSValBuilder().getConjuredSymbolVal(NULL, CE, Count);
1180 }
Jordy Rose19c5dd12010-07-27 01:37:31 +00001181 }
Jordy Rosebd32bee2011-06-14 01:26:48 +00001182
1183 // Bind the return value.
1184 assert(!result.isUnknown() && "Should have conjured a value by now");
Ted Kremenek5eca4822012-01-06 22:09:28 +00001185 state = state->BindExpr(CE, LCtx, result);
Anna Zaks0bd6b112011-10-26 21:06:34 +00001186 C.addTransition(state);
Jordy Rose19c5dd12010-07-27 01:37:31 +00001187}
1188
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +00001189void CStringChecker::evalStrcpy(CheckerContext &C, const CallExpr *CE) const {
Jordy Rosee64f3112010-08-16 07:51:42 +00001190 // char *strcpy(char *restrict dst, const char *restrict src);
Lenny Maiorani067bbd02011-04-09 15:12:58 +00001191 evalStrcpyCommon(C, CE,
1192 /* returnEnd = */ false,
1193 /* isBounded = */ false,
1194 /* isAppending = */ false);
Ted Kremenek0ef473f2011-02-22 04:58:34 +00001195}
1196
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +00001197void CStringChecker::evalStrncpy(CheckerContext &C, const CallExpr *CE) const {
Jordy Rosec1525862011-06-04 00:05:23 +00001198 // char *strncpy(char *restrict dst, const char *restrict src, size_t n);
Lenny Maiorani067bbd02011-04-09 15:12:58 +00001199 evalStrcpyCommon(C, CE,
1200 /* returnEnd = */ false,
1201 /* isBounded = */ true,
1202 /* isAppending = */ false);
Jordy Rosee64f3112010-08-16 07:51:42 +00001203}
1204
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +00001205void CStringChecker::evalStpcpy(CheckerContext &C, const CallExpr *CE) const {
Jordy Rosee64f3112010-08-16 07:51:42 +00001206 // char *stpcpy(char *restrict dst, const char *restrict src);
Lenny Maiorani067bbd02011-04-09 15:12:58 +00001207 evalStrcpyCommon(C, CE,
1208 /* returnEnd = */ true,
1209 /* isBounded = */ false,
1210 /* isAppending = */ false);
1211}
1212
1213void CStringChecker::evalStrcat(CheckerContext &C, const CallExpr *CE) const {
1214 //char *strcat(char *restrict s1, const char *restrict s2);
1215 evalStrcpyCommon(C, CE,
1216 /* returnEnd = */ false,
1217 /* isBounded = */ false,
1218 /* isAppending = */ true);
1219}
1220
1221void CStringChecker::evalStrncat(CheckerContext &C, const CallExpr *CE) const {
1222 //char *strncat(char *restrict s1, const char *restrict s2, size_t n);
1223 evalStrcpyCommon(C, CE,
1224 /* returnEnd = */ false,
1225 /* isBounded = */ true,
1226 /* isAppending = */ true);
Jordy Rosee64f3112010-08-16 07:51:42 +00001227}
1228
Ted Kremenek9c149532010-12-01 21:57:22 +00001229void CStringChecker::evalStrcpyCommon(CheckerContext &C, const CallExpr *CE,
Lenny Maiorani067bbd02011-04-09 15:12:58 +00001230 bool returnEnd, bool isBounded,
1231 bool isAppending) const {
Jordy Rose9e49d9f2011-06-20 02:06:40 +00001232 CurrentFunctionDescription = "string copy function";
Ted Kremenek8bef8232012-01-26 21:29:00 +00001233 ProgramStateRef state = C.getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +00001234 const LocationContext *LCtx = C.getLocationContext();
Jordy Rosee64f3112010-08-16 07:51:42 +00001235
Lenny Maiorani067bbd02011-04-09 15:12:58 +00001236 // Check that the destination is non-null.
Jordy Rosee64f3112010-08-16 07:51:42 +00001237 const Expr *Dst = CE->getArg(0);
Ted Kremenek5eca4822012-01-06 22:09:28 +00001238 SVal DstVal = state->getSVal(Dst, LCtx);
Jordy Rosee64f3112010-08-16 07:51:42 +00001239
Ted Kremenekc8413fd2010-12-02 07:49:45 +00001240 state = checkNonNull(C, state, Dst, DstVal);
Jordy Rosee64f3112010-08-16 07:51:42 +00001241 if (!state)
1242 return;
1243
1244 // Check that the source is non-null.
Ted Kremenekc8413fd2010-12-02 07:49:45 +00001245 const Expr *srcExpr = CE->getArg(1);
Ted Kremenek5eca4822012-01-06 22:09:28 +00001246 SVal srcVal = state->getSVal(srcExpr, LCtx);
Ted Kremenekc8413fd2010-12-02 07:49:45 +00001247 state = checkNonNull(C, state, srcExpr, srcVal);
Jordy Rosee64f3112010-08-16 07:51:42 +00001248 if (!state)
1249 return;
1250
1251 // Get the string length of the source.
Ted Kremenekc8413fd2010-12-02 07:49:45 +00001252 SVal strLength = getCStringLength(C, state, srcExpr, srcVal);
Jordy Rosee64f3112010-08-16 07:51:42 +00001253
1254 // If the source isn't a valid C string, give up.
Ted Kremenekc8413fd2010-12-02 07:49:45 +00001255 if (strLength.isUndef())
Jordy Rosee64f3112010-08-16 07:51:42 +00001256 return;
1257
Jordy Rosed5af0e12011-06-15 05:52:56 +00001258 SValBuilder &svalBuilder = C.getSValBuilder();
1259 QualType cmpTy = svalBuilder.getConditionType();
Jordy Rose8912aae2011-06-20 21:55:40 +00001260 QualType sizeTy = svalBuilder.getContext().getSizeType();
Jordy Rosed5af0e12011-06-15 05:52:56 +00001261
Jordy Rose8912aae2011-06-20 21:55:40 +00001262 // These two values allow checking two kinds of errors:
1263 // - actual overflows caused by a source that doesn't fit in the destination
1264 // - potential overflows caused by a bound that could exceed the destination
Jordy Rosed5af0e12011-06-15 05:52:56 +00001265 SVal amountCopied = UnknownVal();
Jordy Rose8912aae2011-06-20 21:55:40 +00001266 SVal maxLastElementIndex = UnknownVal();
1267 const char *boundWarning = NULL;
Jordy Rosed5af0e12011-06-15 05:52:56 +00001268
Lenny Maiorani067bbd02011-04-09 15:12:58 +00001269 // If the function is strncpy, strncat, etc... it is bounded.
1270 if (isBounded) {
1271 // Get the max number of characters to copy.
Ted Kremenek0ef473f2011-02-22 04:58:34 +00001272 const Expr *lenExpr = CE->getArg(2);
Ted Kremenek5eca4822012-01-06 22:09:28 +00001273 SVal lenVal = state->getSVal(lenExpr, LCtx);
Ted Kremenek0ef473f2011-02-22 04:58:34 +00001274
Jordy Rosed5af0e12011-06-15 05:52:56 +00001275 // Protect against misdeclared strncpy().
Jordy Rose8912aae2011-06-20 21:55:40 +00001276 lenVal = svalBuilder.evalCast(lenVal, sizeTy, lenExpr->getType());
Jordy Rosed5af0e12011-06-15 05:52:56 +00001277
Ted Kremenek0ef473f2011-02-22 04:58:34 +00001278 NonLoc *strLengthNL = dyn_cast<NonLoc>(&strLength);
1279 NonLoc *lenValNL = dyn_cast<NonLoc>(&lenVal);
1280
Jordy Rosed5af0e12011-06-15 05:52:56 +00001281 // If we know both values, we might be able to figure out how much
1282 // we're copying.
1283 if (strLengthNL && lenValNL) {
Ted Kremenek8bef8232012-01-26 21:29:00 +00001284 ProgramStateRef stateSourceTooLong, stateSourceNotTooLong;
Ted Kremenek0ef473f2011-02-22 04:58:34 +00001285
Jordy Rosed5af0e12011-06-15 05:52:56 +00001286 // Check if the max number to copy is less than the length of the src.
Jordy Rose8912aae2011-06-20 21:55:40 +00001287 // If the bound is equal to the source length, strncpy won't null-
1288 // terminate the result!
Jordy Rosed5af0e12011-06-15 05:52:56 +00001289 llvm::tie(stateSourceTooLong, stateSourceNotTooLong) =
1290 state->assume(cast<DefinedOrUnknownSVal>
Jordy Rose8912aae2011-06-20 21:55:40 +00001291 (svalBuilder.evalBinOpNN(state, BO_GE, *strLengthNL,
Jordy Rosed5af0e12011-06-15 05:52:56 +00001292 *lenValNL, cmpTy)));
1293
1294 if (stateSourceTooLong && !stateSourceNotTooLong) {
1295 // Max number to copy is less than the length of the src, so the actual
1296 // strLength copied is the max number arg.
1297 state = stateSourceTooLong;
1298 amountCopied = lenVal;
1299
1300 } else if (!stateSourceTooLong && stateSourceNotTooLong) {
1301 // The source buffer entirely fits in the bound.
1302 state = stateSourceNotTooLong;
1303 amountCopied = strLength;
1304 }
1305 }
1306
Jordy Rose5e5f1502011-06-20 03:49:16 +00001307 // We still want to know if the bound is known to be too large.
Jordy Rose8912aae2011-06-20 21:55:40 +00001308 if (lenValNL) {
1309 if (isAppending) {
1310 // For strncat, the check is strlen(dst) + lenVal < sizeof(dst)
1311
1312 // Get the string length of the destination. If the destination is
1313 // memory that can't have a string length, we shouldn't be copying
1314 // into it anyway.
1315 SVal dstStrLength = getCStringLength(C, state, Dst, DstVal);
1316 if (dstStrLength.isUndef())
1317 return;
1318
1319 if (NonLoc *dstStrLengthNL = dyn_cast<NonLoc>(&dstStrLength)) {
1320 maxLastElementIndex = svalBuilder.evalBinOpNN(state, BO_Add,
1321 *lenValNL,
1322 *dstStrLengthNL,
1323 sizeTy);
1324 boundWarning = "Size argument is greater than the free space in the "
1325 "destination buffer";
1326 }
1327
1328 } else {
1329 // For strncpy, this is just checking that lenVal <= sizeof(dst)
1330 // (Yes, strncpy and strncat differ in how they treat termination.
1331 // strncat ALWAYS terminates, but strncpy doesn't.)
1332 NonLoc one = cast<NonLoc>(svalBuilder.makeIntVal(1, sizeTy));
1333 maxLastElementIndex = svalBuilder.evalBinOpNN(state, BO_Sub, *lenValNL,
1334 one, sizeTy);
1335 boundWarning = "Size argument is greater than the length of the "
1336 "destination buffer";
1337 }
1338 }
Jordy Rose5e5f1502011-06-20 03:49:16 +00001339
Jordy Rosed5af0e12011-06-15 05:52:56 +00001340 // If we couldn't pin down the copy length, at least bound it.
Jordy Rose8912aae2011-06-20 21:55:40 +00001341 // FIXME: We should actually run this code path for append as well, but
1342 // right now it creates problems with constraints (since we can end up
1343 // trying to pass constraints from symbol to symbol).
1344 if (amountCopied.isUnknown() && !isAppending) {
Jordy Rosed5af0e12011-06-15 05:52:56 +00001345 // Try to get a "hypothetical" string length symbol, which we can later
1346 // set as a real value if that turns out to be the case.
1347 amountCopied = getCStringLength(C, state, lenExpr, srcVal, true);
1348 assert(!amountCopied.isUndef());
1349
1350 if (NonLoc *amountCopiedNL = dyn_cast<NonLoc>(&amountCopied)) {
1351 if (lenValNL) {
1352 // amountCopied <= lenVal
1353 SVal copiedLessThanBound = svalBuilder.evalBinOpNN(state, BO_LE,
1354 *amountCopiedNL,
1355 *lenValNL,
1356 cmpTy);
1357 state = state->assume(cast<DefinedOrUnknownSVal>(copiedLessThanBound),
1358 true);
1359 if (!state)
1360 return;
1361 }
1362
1363 if (strLengthNL) {
1364 // amountCopied <= strlen(source)
1365 SVal copiedLessThanSrc = svalBuilder.evalBinOpNN(state, BO_LE,
1366 *amountCopiedNL,
1367 *strLengthNL,
1368 cmpTy);
1369 state = state->assume(cast<DefinedOrUnknownSVal>(copiedLessThanSrc),
1370 true);
1371 if (!state)
1372 return;
1373 }
1374 }
1375 }
1376
1377 } else {
1378 // The function isn't bounded. The amount copied should match the length
1379 // of the source buffer.
1380 amountCopied = strLength;
Ted Kremenek0ef473f2011-02-22 04:58:34 +00001381 }
1382
Jordy Rosed5af0e12011-06-15 05:52:56 +00001383 assert(state);
1384
1385 // This represents the number of characters copied into the destination
1386 // buffer. (It may not actually be the strlen if the destination buffer
1387 // is not terminated.)
1388 SVal finalStrLength = UnknownVal();
1389
Lenny Maiorani067bbd02011-04-09 15:12:58 +00001390 // If this is an appending function (strcat, strncat...) then set the
1391 // string length to strlen(src) + strlen(dst) since the buffer will
1392 // ultimately contain both.
1393 if (isAppending) {
Jordy Rose1e022412011-06-16 05:51:02 +00001394 // Get the string length of the destination. If the destination is memory
1395 // that can't have a string length, we shouldn't be copying into it anyway.
Lenny Maiorani067bbd02011-04-09 15:12:58 +00001396 SVal dstStrLength = getCStringLength(C, state, Dst, DstVal);
1397 if (dstStrLength.isUndef())
1398 return;
1399
Jordy Rosed5af0e12011-06-15 05:52:56 +00001400 NonLoc *srcStrLengthNL = dyn_cast<NonLoc>(&amountCopied);
Lenny Maiorani067bbd02011-04-09 15:12:58 +00001401 NonLoc *dstStrLengthNL = dyn_cast<NonLoc>(&dstStrLength);
1402
Jordy Rosed5af0e12011-06-15 05:52:56 +00001403 // If we know both string lengths, we might know the final string length.
1404 if (srcStrLengthNL && dstStrLengthNL) {
1405 // Make sure the two lengths together don't overflow a size_t.
1406 state = checkAdditionOverflow(C, state, *srcStrLengthNL, *dstStrLengthNL);
1407 if (!state)
1408 return;
Lenny Maiorani067bbd02011-04-09 15:12:58 +00001409
Jordy Rosed5af0e12011-06-15 05:52:56 +00001410 finalStrLength = svalBuilder.evalBinOpNN(state, BO_Add, *srcStrLengthNL,
1411 *dstStrLengthNL, sizeTy);
1412 }
Lenny Maiorani067bbd02011-04-09 15:12:58 +00001413
Jordy Rosed5af0e12011-06-15 05:52:56 +00001414 // If we couldn't get a single value for the final string length,
1415 // we can at least bound it by the individual lengths.
1416 if (finalStrLength.isUnknown()) {
1417 // Try to get a "hypothetical" string length symbol, which we can later
1418 // set as a real value if that turns out to be the case.
1419 finalStrLength = getCStringLength(C, state, CE, DstVal, true);
1420 assert(!finalStrLength.isUndef());
1421
1422 if (NonLoc *finalStrLengthNL = dyn_cast<NonLoc>(&finalStrLength)) {
1423 if (srcStrLengthNL) {
1424 // finalStrLength >= srcStrLength
1425 SVal sourceInResult = svalBuilder.evalBinOpNN(state, BO_GE,
1426 *finalStrLengthNL,
1427 *srcStrLengthNL,
1428 cmpTy);
1429 state = state->assume(cast<DefinedOrUnknownSVal>(sourceInResult),
1430 true);
1431 if (!state)
1432 return;
1433 }
1434
1435 if (dstStrLengthNL) {
1436 // finalStrLength >= dstStrLength
1437 SVal destInResult = svalBuilder.evalBinOpNN(state, BO_GE,
1438 *finalStrLengthNL,
1439 *dstStrLengthNL,
1440 cmpTy);
1441 state = state->assume(cast<DefinedOrUnknownSVal>(destInResult),
1442 true);
1443 if (!state)
1444 return;
1445 }
1446 }
1447 }
1448
1449 } else {
1450 // Otherwise, this is a copy-over function (strcpy, strncpy, ...), and
1451 // the final string length will match the input string length.
1452 finalStrLength = amountCopied;
Lenny Maiorani067bbd02011-04-09 15:12:58 +00001453 }
1454
Jordy Rosed5af0e12011-06-15 05:52:56 +00001455 // The final result of the function will either be a pointer past the last
1456 // copied element, or a pointer to the start of the destination buffer.
Ted Kremenekc8413fd2010-12-02 07:49:45 +00001457 SVal Result = (returnEnd ? UnknownVal() : DstVal);
Jordy Rosee64f3112010-08-16 07:51:42 +00001458
Jordy Rosed5af0e12011-06-15 05:52:56 +00001459 assert(state);
1460
Jordy Rosee64f3112010-08-16 07:51:42 +00001461 // If the destination is a MemRegion, try to check for a buffer overflow and
1462 // record the new string length.
Ted Kremenekc8413fd2010-12-02 07:49:45 +00001463 if (loc::MemRegionVal *dstRegVal = dyn_cast<loc::MemRegionVal>(&DstVal)) {
Jordy Rose8912aae2011-06-20 21:55:40 +00001464 QualType ptrTy = Dst->getType();
1465
1466 // If we have an exact value on a bounded copy, use that to check for
1467 // overflows, rather than our estimate about how much is actually copied.
1468 if (boundWarning) {
1469 if (NonLoc *maxLastNL = dyn_cast<NonLoc>(&maxLastElementIndex)) {
1470 SVal maxLastElement = svalBuilder.evalBinOpLN(state, BO_Add, *dstRegVal,
1471 *maxLastNL, ptrTy);
1472 state = CheckLocation(C, state, CE->getArg(2), maxLastElement,
1473 boundWarning);
1474 if (!state)
1475 return;
1476 }
1477 }
1478
1479 // Then, if the final length is known...
Jordy Rosed5af0e12011-06-15 05:52:56 +00001480 if (NonLoc *knownStrLength = dyn_cast<NonLoc>(&finalStrLength)) {
1481 SVal lastElement = svalBuilder.evalBinOpLN(state, BO_Add, *dstRegVal,
Jordy Rose8912aae2011-06-20 21:55:40 +00001482 *knownStrLength, ptrTy);
Jordy Rosee64f3112010-08-16 07:51:42 +00001483
Jordy Rose8912aae2011-06-20 21:55:40 +00001484 // ...and we haven't checked the bound, we'll check the actual copy.
1485 if (!boundWarning) {
1486 const char * const warningMsg =
1487 "String copy function overflows destination buffer";
1488 state = CheckLocation(C, state, Dst, lastElement, warningMsg);
1489 if (!state)
1490 return;
1491 }
Jordy Rosee64f3112010-08-16 07:51:42 +00001492
1493 // If this is a stpcpy-style copy, the last element is the return value.
Ted Kremenekc8413fd2010-12-02 07:49:45 +00001494 if (returnEnd)
1495 Result = lastElement;
Jordy Rosee64f3112010-08-16 07:51:42 +00001496 }
1497
1498 // Invalidate the destination. This must happen before we set the C string
1499 // length because invalidation will clear the length.
1500 // FIXME: Even if we can't perfectly model the copy, we should see if we
1501 // can use LazyCompoundVals to copy the source values into the destination.
1502 // This would probably remove any existing bindings past the end of the
1503 // string, but that's still an improvement over blank invalidation.
Ted Kremenekc8413fd2010-12-02 07:49:45 +00001504 state = InvalidateBuffer(C, state, Dst, *dstRegVal);
Jordy Rosee64f3112010-08-16 07:51:42 +00001505
Jordy Rose5e5f1502011-06-20 03:49:16 +00001506 // Set the C string length of the destination, if we know it.
Jordy Rose8912aae2011-06-20 21:55:40 +00001507 if (isBounded && !isAppending) {
1508 // strncpy is annoying in that it doesn't guarantee to null-terminate
1509 // the result string. If the original string didn't fit entirely inside
1510 // the bound (including the null-terminator), we don't know how long the
1511 // result is.
1512 if (amountCopied != strLength)
1513 finalStrLength = UnknownVal();
1514 }
1515 state = setCStringLength(state, dstRegVal->getRegion(), finalStrLength);
Jordy Rosee64f3112010-08-16 07:51:42 +00001516 }
1517
Jordy Rosed5af0e12011-06-15 05:52:56 +00001518 assert(state);
1519
Jordy Rosee64f3112010-08-16 07:51:42 +00001520 // If this is a stpcpy-style copy, but we were unable to check for a buffer
1521 // overflow, we still need a result. Conjure a return value.
Ted Kremenekc8413fd2010-12-02 07:49:45 +00001522 if (returnEnd && Result.isUnknown()) {
Anna Zaks5d0ea6d2011-10-04 20:43:05 +00001523 unsigned Count = C.getCurrentBlockCount();
Jordy Rosed5af0e12011-06-15 05:52:56 +00001524 Result = svalBuilder.getConjuredSymbolVal(NULL, CE, Count);
Jordy Rosee64f3112010-08-16 07:51:42 +00001525 }
1526
1527 // Set the return value.
Ted Kremenek5eca4822012-01-06 22:09:28 +00001528 state = state->BindExpr(CE, LCtx, Result);
Anna Zaks0bd6b112011-10-26 21:06:34 +00001529 C.addTransition(state);
Jordy Rosee64f3112010-08-16 07:51:42 +00001530}
1531
Lenny Maiorani318dd922011-04-12 17:08:43 +00001532void CStringChecker::evalStrcmp(CheckerContext &C, const CallExpr *CE) const {
Jordy Roseadc42d42011-06-16 07:13:34 +00001533 //int strcmp(const char *s1, const char *s2);
Lenny Maioranibd1d16a2011-04-28 15:09:11 +00001534 evalStrcmpCommon(C, CE, /* isBounded = */ false, /* ignoreCase = */ false);
Lenny Maiorani357f6ee2011-04-25 22:21:00 +00001535}
Lenny Maiorani318dd922011-04-12 17:08:43 +00001536
Lenny Maiorani357f6ee2011-04-25 22:21:00 +00001537void CStringChecker::evalStrncmp(CheckerContext &C, const CallExpr *CE) const {
Jordy Roseadc42d42011-06-16 07:13:34 +00001538 //int strncmp(const char *s1, const char *s2, size_t n);
Lenny Maioranibd1d16a2011-04-28 15:09:11 +00001539 evalStrcmpCommon(C, CE, /* isBounded = */ true, /* ignoreCase = */ false);
1540}
1541
1542void CStringChecker::evalStrcasecmp(CheckerContext &C,
1543 const CallExpr *CE) const {
Jordy Roseadc42d42011-06-16 07:13:34 +00001544 //int strcasecmp(const char *s1, const char *s2);
Lenny Maioranibd1d16a2011-04-28 15:09:11 +00001545 evalStrcmpCommon(C, CE, /* isBounded = */ false, /* ignoreCase = */ true);
Lenny Maiorani357f6ee2011-04-25 22:21:00 +00001546}
1547
Lenny Maiorani454fd2d2011-05-02 19:05:49 +00001548void CStringChecker::evalStrncasecmp(CheckerContext &C,
1549 const CallExpr *CE) const {
Jordy Roseadc42d42011-06-16 07:13:34 +00001550 //int strncasecmp(const char *s1, const char *s2, size_t n);
Lenny Maiorani454fd2d2011-05-02 19:05:49 +00001551 evalStrcmpCommon(C, CE, /* isBounded = */ true, /* ignoreCase = */ true);
1552}
1553
Lenny Maiorani357f6ee2011-04-25 22:21:00 +00001554void CStringChecker::evalStrcmpCommon(CheckerContext &C, const CallExpr *CE,
Lenny Maioranibd1d16a2011-04-28 15:09:11 +00001555 bool isBounded, bool ignoreCase) const {
Jordy Rose9e49d9f2011-06-20 02:06:40 +00001556 CurrentFunctionDescription = "string comparison function";
Ted Kremenek8bef8232012-01-26 21:29:00 +00001557 ProgramStateRef state = C.getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +00001558 const LocationContext *LCtx = C.getLocationContext();
Lenny Maiorani318dd922011-04-12 17:08:43 +00001559
1560 // Check that the first string is non-null
1561 const Expr *s1 = CE->getArg(0);
Ted Kremenek5eca4822012-01-06 22:09:28 +00001562 SVal s1Val = state->getSVal(s1, LCtx);
Lenny Maiorani318dd922011-04-12 17:08:43 +00001563 state = checkNonNull(C, state, s1, s1Val);
1564 if (!state)
1565 return;
1566
1567 // Check that the second string is non-null.
1568 const Expr *s2 = CE->getArg(1);
Ted Kremenek5eca4822012-01-06 22:09:28 +00001569 SVal s2Val = state->getSVal(s2, LCtx);
Lenny Maiorani318dd922011-04-12 17:08:43 +00001570 state = checkNonNull(C, state, s2, s2Val);
1571 if (!state)
1572 return;
1573
1574 // Get the string length of the first string or give up.
1575 SVal s1Length = getCStringLength(C, state, s1, s1Val);
1576 if (s1Length.isUndef())
1577 return;
1578
1579 // Get the string length of the second string or give up.
1580 SVal s2Length = getCStringLength(C, state, s2, s2Val);
1581 if (s2Length.isUndef())
1582 return;
1583
Jordy Roseadc42d42011-06-16 07:13:34 +00001584 // If we know the two buffers are the same, we know the result is 0.
1585 // First, get the two buffers' addresses. Another checker will have already
1586 // made sure they're not undefined.
1587 DefinedOrUnknownSVal LV = cast<DefinedOrUnknownSVal>(s1Val);
1588 DefinedOrUnknownSVal RV = cast<DefinedOrUnknownSVal>(s2Val);
Lenny Maiorani318dd922011-04-12 17:08:43 +00001589
Jordy Roseadc42d42011-06-16 07:13:34 +00001590 // See if they are the same.
Lenny Maiorani318dd922011-04-12 17:08:43 +00001591 SValBuilder &svalBuilder = C.getSValBuilder();
Jordy Roseadc42d42011-06-16 07:13:34 +00001592 DefinedOrUnknownSVal SameBuf = svalBuilder.evalEQ(state, LV, RV);
Ted Kremenek8bef8232012-01-26 21:29:00 +00001593 ProgramStateRef StSameBuf, StNotSameBuf;
Jordy Roseadc42d42011-06-16 07:13:34 +00001594 llvm::tie(StSameBuf, StNotSameBuf) = state->assume(SameBuf);
Lenny Maiorani318dd922011-04-12 17:08:43 +00001595
Jordy Roseadc42d42011-06-16 07:13:34 +00001596 // If the two arguments might be the same buffer, we know the result is 0,
1597 // and we only need to check one size.
1598 if (StSameBuf) {
Ted Kremenek5eca4822012-01-06 22:09:28 +00001599 StSameBuf = StSameBuf->BindExpr(CE, LCtx,
1600 svalBuilder.makeZeroVal(CE->getType()));
Anna Zaks0bd6b112011-10-26 21:06:34 +00001601 C.addTransition(StSameBuf);
Jordy Roseadc42d42011-06-16 07:13:34 +00001602
1603 // If the two arguments are GUARANTEED to be the same, we're done!
1604 if (!StNotSameBuf)
1605 return;
1606 }
1607
1608 assert(StNotSameBuf);
1609 state = StNotSameBuf;
1610
1611 // At this point we can go about comparing the two buffers.
1612 // For now, we only do this if they're both known string literals.
1613
1614 // Attempt to extract string literals from both expressions.
1615 const StringLiteral *s1StrLiteral = getCStringLiteral(C, state, s1, s1Val);
1616 const StringLiteral *s2StrLiteral = getCStringLiteral(C, state, s2, s2Val);
1617 bool canComputeResult = false;
1618
1619 if (s1StrLiteral && s2StrLiteral) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001620 StringRef s1StrRef = s1StrLiteral->getString();
1621 StringRef s2StrRef = s2StrLiteral->getString();
Jordy Roseadc42d42011-06-16 07:13:34 +00001622
1623 if (isBounded) {
1624 // Get the max number of characters to compare.
1625 const Expr *lenExpr = CE->getArg(2);
Ted Kremenek5eca4822012-01-06 22:09:28 +00001626 SVal lenVal = state->getSVal(lenExpr, LCtx);
Jordy Roseadc42d42011-06-16 07:13:34 +00001627
1628 // If the length is known, we can get the right substrings.
1629 if (const llvm::APSInt *len = svalBuilder.getKnownValue(state, lenVal)) {
1630 // Create substrings of each to compare the prefix.
1631 s1StrRef = s1StrRef.substr(0, (size_t)len->getZExtValue());
1632 s2StrRef = s2StrRef.substr(0, (size_t)len->getZExtValue());
1633 canComputeResult = true;
1634 }
1635 } else {
1636 // This is a normal, unbounded strcmp.
1637 canComputeResult = true;
1638 }
1639
1640 if (canComputeResult) {
1641 // Real strcmp stops at null characters.
1642 size_t s1Term = s1StrRef.find('\0');
Chris Lattner5f9e2722011-07-23 10:55:15 +00001643 if (s1Term != StringRef::npos)
Jordy Roseadc42d42011-06-16 07:13:34 +00001644 s1StrRef = s1StrRef.substr(0, s1Term);
1645
1646 size_t s2Term = s2StrRef.find('\0');
Chris Lattner5f9e2722011-07-23 10:55:15 +00001647 if (s2Term != StringRef::npos)
Jordy Roseadc42d42011-06-16 07:13:34 +00001648 s2StrRef = s2StrRef.substr(0, s2Term);
1649
1650 // Use StringRef's comparison methods to compute the actual result.
1651 int result;
1652
1653 if (ignoreCase) {
1654 // Compare string 1 to string 2 the same way strcasecmp() does.
1655 result = s1StrRef.compare_lower(s2StrRef);
1656 } else {
1657 // Compare string 1 to string 2 the same way strcmp() does.
1658 result = s1StrRef.compare(s2StrRef);
1659 }
1660
1661 // Build the SVal of the comparison and bind the return value.
1662 SVal resultVal = svalBuilder.makeIntVal(result, CE->getType());
Ted Kremenek5eca4822012-01-06 22:09:28 +00001663 state = state->BindExpr(CE, LCtx, resultVal);
Jordy Roseadc42d42011-06-16 07:13:34 +00001664 }
1665 }
1666
1667 if (!canComputeResult) {
1668 // Conjure a symbolic value. It's the best we can do.
Anna Zaks5d0ea6d2011-10-04 20:43:05 +00001669 unsigned Count = C.getCurrentBlockCount();
Jordy Roseadc42d42011-06-16 07:13:34 +00001670 SVal resultVal = svalBuilder.getConjuredSymbolVal(NULL, CE, Count);
Ted Kremenek5eca4822012-01-06 22:09:28 +00001671 state = state->BindExpr(CE, LCtx, resultVal);
Jordy Roseadc42d42011-06-16 07:13:34 +00001672 }
1673
1674 // Record this as a possible path.
Anna Zaks0bd6b112011-10-26 21:06:34 +00001675 C.addTransition(state);
Lenny Maiorani318dd922011-04-12 17:08:43 +00001676}
1677
Jordy Rosed325ffb2010-07-08 23:57:29 +00001678//===----------------------------------------------------------------------===//
Jordy Rosea5261542010-08-14 21:02:52 +00001679// The driver method, and other Checker callbacks.
Jordy Rosed325ffb2010-07-08 23:57:29 +00001680//===----------------------------------------------------------------------===//
Jordy Roseccbf7ee2010-07-06 23:11:01 +00001681
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +00001682bool CStringChecker::evalCall(const CallExpr *CE, CheckerContext &C) const {
Anna Zaksb805c8f2011-12-01 05:57:37 +00001683 StringRef Name = C.getCalleeName(CE);
1684 if (Name.empty())
Jordy Roseccbf7ee2010-07-06 23:11:01 +00001685 return false;
Jordy Roseccbf7ee2010-07-06 23:11:01 +00001686 if (Name.startswith("__builtin_"))
1687 Name = Name.substr(10);
1688
Ted Kremenek9c149532010-12-01 21:57:22 +00001689 FnCheck evalFunction = llvm::StringSwitch<FnCheck>(Name)
1690 .Cases("memcpy", "__memcpy_chk", &CStringChecker::evalMemcpy)
Jordy Rosebe460d82011-06-03 23:42:56 +00001691 .Cases("mempcpy", "__mempcpy_chk", &CStringChecker::evalMempcpy)
Ted Kremenek9c149532010-12-01 21:57:22 +00001692 .Cases("memcmp", "bcmp", &CStringChecker::evalMemcmp)
1693 .Cases("memmove", "__memmove_chk", &CStringChecker::evalMemmove)
1694 .Cases("strcpy", "__strcpy_chk", &CStringChecker::evalStrcpy)
Jordy Rose5e5f1502011-06-20 03:49:16 +00001695 .Cases("strncpy", "__strncpy_chk", &CStringChecker::evalStrncpy)
Ted Kremenek9c149532010-12-01 21:57:22 +00001696 .Cases("stpcpy", "__stpcpy_chk", &CStringChecker::evalStpcpy)
Lenny Maiorani067bbd02011-04-09 15:12:58 +00001697 .Cases("strcat", "__strcat_chk", &CStringChecker::evalStrcat)
1698 .Cases("strncat", "__strncat_chk", &CStringChecker::evalStrncat)
Ted Kremenekc8413fd2010-12-02 07:49:45 +00001699 .Case("strlen", &CStringChecker::evalstrLength)
Ted Kremenekbe4242c2011-02-22 04:55:05 +00001700 .Case("strnlen", &CStringChecker::evalstrnLength)
Lenny Maiorani318dd922011-04-12 17:08:43 +00001701 .Case("strcmp", &CStringChecker::evalStrcmp)
Lenny Maiorani357f6ee2011-04-25 22:21:00 +00001702 .Case("strncmp", &CStringChecker::evalStrncmp)
Lenny Maioranibd1d16a2011-04-28 15:09:11 +00001703 .Case("strcasecmp", &CStringChecker::evalStrcasecmp)
Lenny Maiorani454fd2d2011-05-02 19:05:49 +00001704 .Case("strncasecmp", &CStringChecker::evalStrncasecmp)
Ted Kremenek9c149532010-12-01 21:57:22 +00001705 .Case("bcopy", &CStringChecker::evalBcopy)
Jordy Roseccbf7ee2010-07-06 23:11:01 +00001706 .Default(NULL);
1707
Jordy Rosed325ffb2010-07-08 23:57:29 +00001708 // If the callee isn't a string function, let another checker handle it.
Ted Kremenek9c149532010-12-01 21:57:22 +00001709 if (!evalFunction)
Jordy Roseccbf7ee2010-07-06 23:11:01 +00001710 return false;
1711
Jordy Rose9e49d9f2011-06-20 02:06:40 +00001712 // Make sure each function sets its own description.
1713 // (But don't bother in a release build.)
1714 assert(!(CurrentFunctionDescription = NULL));
1715
Jordy Rosed325ffb2010-07-08 23:57:29 +00001716 // Check and evaluate the call.
Ted Kremenek9c149532010-12-01 21:57:22 +00001717 (this->*evalFunction)(C, CE);
Jordy Roseccbf7ee2010-07-06 23:11:01 +00001718 return true;
1719}
Jordy Rosea5261542010-08-14 21:02:52 +00001720
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +00001721void CStringChecker::checkPreStmt(const DeclStmt *DS, CheckerContext &C) const {
Jordy Rosea5261542010-08-14 21:02:52 +00001722 // Record string length for char a[] = "abc";
Ted Kremenek8bef8232012-01-26 21:29:00 +00001723 ProgramStateRef state = C.getState();
Jordy Rosea5261542010-08-14 21:02:52 +00001724
1725 for (DeclStmt::const_decl_iterator I = DS->decl_begin(), E = DS->decl_end();
1726 I != E; ++I) {
1727 const VarDecl *D = dyn_cast<VarDecl>(*I);
1728 if (!D)
1729 continue;
1730
1731 // FIXME: Handle array fields of structs.
1732 if (!D->getType()->isArrayType())
1733 continue;
1734
1735 const Expr *Init = D->getInit();
1736 if (!Init)
1737 continue;
1738 if (!isa<StringLiteral>(Init))
1739 continue;
1740
Anna Zaks39ac1872011-10-26 21:06:44 +00001741 Loc VarLoc = state->getLValue(D, C.getLocationContext());
Jordy Rosea5261542010-08-14 21:02:52 +00001742 const MemRegion *MR = VarLoc.getAsRegion();
1743 if (!MR)
1744 continue;
1745
Ted Kremenek5eca4822012-01-06 22:09:28 +00001746 SVal StrVal = state->getSVal(Init, C.getLocationContext());
Jordy Rosea5261542010-08-14 21:02:52 +00001747 assert(StrVal.isValid() && "Initializer string is unknown or undefined");
Ted Kremenekc8413fd2010-12-02 07:49:45 +00001748 DefinedOrUnknownSVal strLength
1749 = cast<DefinedOrUnknownSVal>(getCStringLength(C, state, Init, StrVal));
Jordy Rosea5261542010-08-14 21:02:52 +00001750
Ted Kremenekc8413fd2010-12-02 07:49:45 +00001751 state = state->set<CStringLength>(MR, strLength);
Jordy Rosea5261542010-08-14 21:02:52 +00001752 }
1753
Anna Zaks0bd6b112011-10-26 21:06:34 +00001754 C.addTransition(state);
Jordy Rosea5261542010-08-14 21:02:52 +00001755}
1756
Ted Kremenek8bef8232012-01-26 21:29:00 +00001757bool CStringChecker::wantsRegionChangeUpdate(ProgramStateRef state) const {
Jordy Rosea5261542010-08-14 21:02:52 +00001758 CStringLength::EntryMap Entries = state->get<CStringLength>();
1759 return !Entries.isEmpty();
1760}
1761
Ted Kremenek8bef8232012-01-26 21:29:00 +00001762ProgramStateRef
1763CStringChecker::checkRegionChanges(ProgramStateRef state,
Ted Kremenek35bdbf42011-05-02 19:42:42 +00001764 const StoreManager::InvalidatedSymbols *,
Jordy Rose537716a2011-08-27 22:51:26 +00001765 ArrayRef<const MemRegion *> ExplicitRegions,
1766 ArrayRef<const MemRegion *> Regions) const {
Jordy Rosea5261542010-08-14 21:02:52 +00001767 CStringLength::EntryMap Entries = state->get<CStringLength>();
1768 if (Entries.isEmpty())
1769 return state;
1770
1771 llvm::SmallPtrSet<const MemRegion *, 8> Invalidated;
1772 llvm::SmallPtrSet<const MemRegion *, 32> SuperRegions;
1773
1774 // First build sets for the changed regions and their super-regions.
Jordy Rose537716a2011-08-27 22:51:26 +00001775 for (ArrayRef<const MemRegion *>::iterator
1776 I = Regions.begin(), E = Regions.end(); I != E; ++I) {
1777 const MemRegion *MR = *I;
Jordy Rosea5261542010-08-14 21:02:52 +00001778 Invalidated.insert(MR);
1779
1780 SuperRegions.insert(MR);
1781 while (const SubRegion *SR = dyn_cast<SubRegion>(MR)) {
1782 MR = SR->getSuperRegion();
1783 SuperRegions.insert(MR);
1784 }
1785 }
1786
1787 CStringLength::EntryMap::Factory &F = state->get_context<CStringLength>();
1788
1789 // Then loop over the entries in the current state.
1790 for (CStringLength::EntryMap::iterator I = Entries.begin(),
1791 E = Entries.end(); I != E; ++I) {
1792 const MemRegion *MR = I.getKey();
1793
1794 // Is this entry for a super-region of a changed region?
1795 if (SuperRegions.count(MR)) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00001796 Entries = F.remove(Entries, MR);
Jordy Rosea5261542010-08-14 21:02:52 +00001797 continue;
1798 }
1799
1800 // Is this entry for a sub-region of a changed region?
1801 const MemRegion *Super = MR;
1802 while (const SubRegion *SR = dyn_cast<SubRegion>(Super)) {
1803 Super = SR->getSuperRegion();
1804 if (Invalidated.count(Super)) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00001805 Entries = F.remove(Entries, MR);
Jordy Rosea5261542010-08-14 21:02:52 +00001806 break;
1807 }
1808 }
1809 }
1810
1811 return state->set<CStringLength>(Entries);
1812}
1813
Ted Kremenek8bef8232012-01-26 21:29:00 +00001814void CStringChecker::checkLiveSymbols(ProgramStateRef state,
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +00001815 SymbolReaper &SR) const {
Jordy Rosea5261542010-08-14 21:02:52 +00001816 // Mark all symbols in our string length map as valid.
1817 CStringLength::EntryMap Entries = state->get<CStringLength>();
1818
1819 for (CStringLength::EntryMap::iterator I = Entries.begin(), E = Entries.end();
1820 I != E; ++I) {
1821 SVal Len = I.getData();
Jordy Rosed5af0e12011-06-15 05:52:56 +00001822
Anna Zaks1d1d5152011-12-06 23:12:33 +00001823 for (SymExpr::symbol_iterator si = Len.symbol_begin(),
1824 se = Len.symbol_end(); si != se; ++si)
Jordy Rosed5af0e12011-06-15 05:52:56 +00001825 SR.markInUse(*si);
Jordy Rosea5261542010-08-14 21:02:52 +00001826 }
1827}
1828
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +00001829void CStringChecker::checkDeadSymbols(SymbolReaper &SR,
1830 CheckerContext &C) const {
Jordy Rosea5261542010-08-14 21:02:52 +00001831 if (!SR.hasDeadSymbols())
1832 return;
1833
Ted Kremenek8bef8232012-01-26 21:29:00 +00001834 ProgramStateRef state = C.getState();
Jordy Rosea5261542010-08-14 21:02:52 +00001835 CStringLength::EntryMap Entries = state->get<CStringLength>();
1836 if (Entries.isEmpty())
1837 return;
1838
1839 CStringLength::EntryMap::Factory &F = state->get_context<CStringLength>();
1840 for (CStringLength::EntryMap::iterator I = Entries.begin(), E = Entries.end();
1841 I != E; ++I) {
1842 SVal Len = I.getData();
1843 if (SymbolRef Sym = Len.getAsSymbol()) {
1844 if (SR.isDead(Sym))
Ted Kremenek3baf6722010-11-24 00:54:37 +00001845 Entries = F.remove(Entries, I.getKey());
Jordy Rosea5261542010-08-14 21:02:52 +00001846 }
1847 }
1848
1849 state = state->set<CStringLength>(Entries);
Anna Zaks0bd6b112011-10-26 21:06:34 +00001850 C.addTransition(state);
Jordy Rosea5261542010-08-14 21:02:52 +00001851}
Argyrios Kyrtzidis183ff982011-02-24 01:05:30 +00001852
1853void ento::registerCStringChecker(CheckerManager &mgr) {
1854 mgr.registerChecker<CStringChecker>();
1855}