Anna Zaks | c800f68 | 2011-10-11 16:49:54 +0000 | [diff] [blame] | 1 | //= CStringChecker.cpp - Checks calls to C string functions --------*- C++ -*-// |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 2 | // |
| 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 Kyrtzidis | a0decc9 | 2011-02-15 21:25:03 +0000 | [diff] [blame] | 15 | #include "ClangSACheckers.h" |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 16 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | 695fb50 | 2011-02-17 21:39:17 +0000 | [diff] [blame] | 17 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 18 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Ted Kremenek | 9b66371 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 19 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallString.h" |
Benjamin Kramer | 00bd44d | 2012-02-04 12:31:12 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/STLExtras.h" |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringSwitch.h" |
| 24 | |
| 25 | using namespace clang; |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 26 | using namespace ento; |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 27 | |
| 28 | namespace { |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 29 | class CStringChecker : public Checker< eval::Call, |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 30 | check::PreStmt<DeclStmt>, |
| 31 | check::LiveSymbols, |
| 32 | check::DeadSymbols, |
| 33 | check::RegionChanges |
| 34 | > { |
Anna Zaks | 5730076 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 35 | mutable OwningPtr<BugType> BT_Null, |
| 36 | BT_Bounds, |
| 37 | BT_Overlap, |
| 38 | BT_NotCString, |
| 39 | BT_AdditionOverflow; |
| 40 | |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 41 | mutable const char *CurrentFunctionDescription; |
| 42 | |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 43 | public: |
Anna Zaks | 5730076 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 44 | /// The filter is used to filter out the diagnostics which are not enabled by |
| 45 | /// the user. |
| 46 | struct CStringChecksFilter { |
| 47 | DefaultBool CheckCStringNullArg; |
| 48 | DefaultBool CheckCStringOutOfBounds; |
| 49 | DefaultBool CheckCStringBufferOverlap; |
| 50 | DefaultBool CheckCStringNotNullTerm; |
| 51 | }; |
| 52 | |
| 53 | CStringChecksFilter Filter; |
| 54 | |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 55 | static void *getTag() { static int tag; return &tag; } |
| 56 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 57 | bool evalCall(const CallExpr *CE, CheckerContext &C) const; |
| 58 | void checkPreStmt(const DeclStmt *DS, CheckerContext &C) const; |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 59 | void checkLiveSymbols(ProgramStateRef state, SymbolReaper &SR) const; |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 60 | void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const; |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 61 | bool wantsRegionChangeUpdate(ProgramStateRef state) const; |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 62 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 63 | ProgramStateRef |
| 64 | checkRegionChanges(ProgramStateRef state, |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 65 | const StoreManager::InvalidatedSymbols *, |
Jordy Rose | 537716a | 2011-08-27 22:51:26 +0000 | [diff] [blame] | 66 | ArrayRef<const MemRegion *> ExplicitRegions, |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 67 | ArrayRef<const MemRegion *> Regions, |
| 68 | const CallOrObjCMessage *Call) const; |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 69 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 70 | typedef void (CStringChecker::*FnCheck)(CheckerContext &, |
| 71 | const CallExpr *) const; |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 72 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 73 | void evalMemcpy(CheckerContext &C, const CallExpr *CE) const; |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 74 | void evalMempcpy(CheckerContext &C, const CallExpr *CE) const; |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 75 | void evalMemmove(CheckerContext &C, const CallExpr *CE) const; |
| 76 | void evalBcopy(CheckerContext &C, const CallExpr *CE) const; |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 77 | void evalCopyCommon(CheckerContext &C, const CallExpr *CE, |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 78 | ProgramStateRef state, |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 79 | const Expr *Size, |
| 80 | const Expr *Source, |
| 81 | const Expr *Dest, |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 82 | bool Restricted = false, |
| 83 | bool IsMempcpy = false) const; |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 84 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 85 | void evalMemcmp(CheckerContext &C, const CallExpr *CE) const; |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 86 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 87 | void evalstrLength(CheckerContext &C, const CallExpr *CE) const; |
| 88 | void evalstrnLength(CheckerContext &C, const CallExpr *CE) const; |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 89 | void evalstrLengthCommon(CheckerContext &C, |
| 90 | const CallExpr *CE, |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 91 | bool IsStrnlen = false) const; |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 92 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 93 | void evalStrcpy(CheckerContext &C, const CallExpr *CE) const; |
| 94 | void evalStrncpy(CheckerContext &C, const CallExpr *CE) const; |
| 95 | void evalStpcpy(CheckerContext &C, const CallExpr *CE) const; |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 96 | void evalStrcpyCommon(CheckerContext &C, |
| 97 | const CallExpr *CE, |
| 98 | bool returnEnd, |
| 99 | bool isBounded, |
| 100 | bool isAppending) const; |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 101 | |
| 102 | void evalStrcat(CheckerContext &C, const CallExpr *CE) const; |
| 103 | void evalStrncat(CheckerContext &C, const CallExpr *CE) const; |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 104 | |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 105 | void evalStrcmp(CheckerContext &C, const CallExpr *CE) const; |
Lenny Maiorani | 357f6ee | 2011-04-25 22:21:00 +0000 | [diff] [blame] | 106 | void evalStrncmp(CheckerContext &C, const CallExpr *CE) const; |
Lenny Maiorani | bd1d16a | 2011-04-28 15:09:11 +0000 | [diff] [blame] | 107 | void evalStrcasecmp(CheckerContext &C, const CallExpr *CE) const; |
Lenny Maiorani | 454fd2d | 2011-05-02 19:05:49 +0000 | [diff] [blame] | 108 | void evalStrncasecmp(CheckerContext &C, const CallExpr *CE) const; |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 109 | void evalStrcmpCommon(CheckerContext &C, |
| 110 | const CallExpr *CE, |
| 111 | bool isBounded = false, |
| 112 | bool ignoreCase = false) const; |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 113 | |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 114 | // Utility methods |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 115 | std::pair<ProgramStateRef , ProgramStateRef > |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 116 | static assumeZero(CheckerContext &C, |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 117 | ProgramStateRef state, SVal V, QualType Ty); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 118 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 119 | static ProgramStateRef setCStringLength(ProgramStateRef state, |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 120 | const MemRegion *MR, |
| 121 | SVal strLength); |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 122 | static SVal getCStringLengthForRegion(CheckerContext &C, |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 123 | ProgramStateRef &state, |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 124 | const Expr *Ex, |
| 125 | const MemRegion *MR, |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 126 | bool hypothetical); |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 127 | SVal getCStringLength(CheckerContext &C, |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 128 | ProgramStateRef &state, |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 129 | const Expr *Ex, |
| 130 | SVal Buf, |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 131 | bool hypothetical = false) const; |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 132 | |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 133 | const StringLiteral *getCStringLiteral(CheckerContext &C, |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 134 | ProgramStateRef &state, |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 135 | const Expr *expr, |
| 136 | SVal val) const; |
| 137 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 138 | static ProgramStateRef InvalidateBuffer(CheckerContext &C, |
| 139 | ProgramStateRef state, |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 140 | const Expr *Ex, SVal V); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 141 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 142 | static bool SummarizeRegion(raw_ostream &os, ASTContext &Ctx, |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 143 | const MemRegion *MR); |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 144 | |
| 145 | // Re-usable checks |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 146 | ProgramStateRef checkNonNull(CheckerContext &C, |
| 147 | ProgramStateRef state, |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 148 | const Expr *S, |
| 149 | SVal l) const; |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 150 | ProgramStateRef CheckLocation(CheckerContext &C, |
| 151 | ProgramStateRef state, |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 152 | const Expr *S, |
| 153 | SVal l, |
| 154 | const char *message = NULL) const; |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 155 | ProgramStateRef CheckBufferAccess(CheckerContext &C, |
| 156 | ProgramStateRef state, |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 157 | const Expr *Size, |
| 158 | const Expr *FirstBuf, |
| 159 | const Expr *SecondBuf, |
| 160 | const char *firstMessage = NULL, |
| 161 | const char *secondMessage = NULL, |
| 162 | bool WarnAboutSize = false) const; |
| 163 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 164 | ProgramStateRef CheckBufferAccess(CheckerContext &C, |
| 165 | ProgramStateRef state, |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 166 | const Expr *Size, |
| 167 | const Expr *Buf, |
| 168 | const char *message = NULL, |
| 169 | bool WarnAboutSize = false) const { |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 170 | // This is a convenience override. |
Jordy Rose | 5e5f150 | 2011-06-20 03:49:16 +0000 | [diff] [blame] | 171 | return CheckBufferAccess(C, state, Size, Buf, NULL, message, NULL, |
| 172 | WarnAboutSize); |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 173 | } |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 174 | ProgramStateRef CheckOverlap(CheckerContext &C, |
| 175 | ProgramStateRef state, |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 176 | const Expr *Size, |
| 177 | const Expr *First, |
| 178 | const Expr *Second) const; |
| 179 | void emitOverlapBug(CheckerContext &C, |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 180 | ProgramStateRef state, |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 181 | const Stmt *First, |
| 182 | const Stmt *Second) const; |
| 183 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 184 | ProgramStateRef checkAdditionOverflow(CheckerContext &C, |
| 185 | ProgramStateRef state, |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 186 | NonLoc left, |
| 187 | NonLoc right) const; |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 188 | }; |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 189 | |
| 190 | class CStringLength { |
| 191 | public: |
| 192 | typedef llvm::ImmutableMap<const MemRegion *, SVal> EntryMap; |
| 193 | }; |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 194 | } //end anonymous namespace |
| 195 | |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 196 | namespace clang { |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 197 | namespace ento { |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 198 | template <> |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 199 | struct ProgramStateTrait<CStringLength> |
| 200 | : public ProgramStatePartialTrait<CStringLength::EntryMap> { |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 201 | static void *GDMIndex() { return CStringChecker::getTag(); } |
| 202 | }; |
| 203 | } |
Argyrios Kyrtzidis | 5a4f98f | 2010-12-22 18:53:20 +0000 | [diff] [blame] | 204 | } |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 205 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 206 | //===----------------------------------------------------------------------===// |
| 207 | // Individual checks and utility methods. |
| 208 | //===----------------------------------------------------------------------===// |
| 209 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 210 | std::pair<ProgramStateRef , ProgramStateRef > |
| 211 | CStringChecker::assumeZero(CheckerContext &C, ProgramStateRef state, SVal V, |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 212 | QualType Ty) { |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 213 | DefinedSVal *val = dyn_cast<DefinedSVal>(&V); |
| 214 | if (!val) |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 215 | return std::pair<ProgramStateRef , ProgramStateRef >(state, state); |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 216 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 217 | SValBuilder &svalBuilder = C.getSValBuilder(); |
| 218 | DefinedOrUnknownSVal zero = svalBuilder.makeZeroVal(Ty); |
| 219 | return state->assume(svalBuilder.evalEQ(state, *val, zero)); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 220 | } |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 221 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 222 | ProgramStateRef CStringChecker::checkNonNull(CheckerContext &C, |
| 223 | ProgramStateRef state, |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 224 | const Expr *S, SVal l) const { |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 225 | // If a previous check has failed, propagate the failure. |
| 226 | if (!state) |
| 227 | return NULL; |
| 228 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 229 | ProgramStateRef stateNull, stateNonNull; |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 230 | llvm::tie(stateNull, stateNonNull) = assumeZero(C, state, l, S->getType()); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 231 | |
| 232 | if (stateNull && !stateNonNull) { |
Anna Zaks | 5730076 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 233 | if (!Filter.CheckCStringNullArg) |
| 234 | return NULL; |
| 235 | |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 236 | ExplodedNode *N = C.generateSink(stateNull); |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 237 | if (!N) |
| 238 | return NULL; |
| 239 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 240 | if (!BT_Null) |
Anna Zaks | 5730076 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 241 | BT_Null.reset(new BuiltinBug("Unix API", |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 242 | "Null pointer argument in call to byte string function")); |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 243 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 244 | SmallString<80> buf; |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 245 | llvm::raw_svector_ostream os(buf); |
| 246 | assert(CurrentFunctionDescription); |
| 247 | os << "Null pointer argument in call to " << CurrentFunctionDescription; |
| 248 | |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 249 | // Generate a report for this bug. |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 250 | BuiltinBug *BT = static_cast<BuiltinBug*>(BT_Null.get()); |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 251 | BugReport *report = new BugReport(*BT, os.str(), N); |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 252 | |
| 253 | report->addRange(S->getSourceRange()); |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 254 | report->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N, S)); |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 255 | C.EmitReport(report); |
| 256 | return NULL; |
| 257 | } |
| 258 | |
| 259 | // From here on, assume that the value is non-null. |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 260 | assert(stateNonNull); |
| 261 | return stateNonNull; |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 264 | // FIXME: This was originally copied from ArrayBoundChecker.cpp. Refactor? |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 265 | ProgramStateRef CStringChecker::CheckLocation(CheckerContext &C, |
| 266 | ProgramStateRef state, |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 267 | const Expr *S, SVal l, |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 268 | const char *warningMsg) const { |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 269 | // If a previous check has failed, propagate the failure. |
| 270 | if (!state) |
| 271 | return NULL; |
| 272 | |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 273 | // Check for out of bound array element access. |
| 274 | const MemRegion *R = l.getAsRegion(); |
| 275 | if (!R) |
| 276 | return state; |
| 277 | |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 278 | const ElementRegion *ER = dyn_cast<ElementRegion>(R); |
| 279 | if (!ER) |
| 280 | return state; |
| 281 | |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 282 | assert(ER->getValueType() == C.getASTContext().CharTy && |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 283 | "CheckLocation should only be called with char* ElementRegions"); |
| 284 | |
| 285 | // Get the size of the array. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 286 | const SubRegion *superReg = cast<SubRegion>(ER->getSuperRegion()); |
| 287 | SValBuilder &svalBuilder = C.getSValBuilder(); |
Jordy Rose | 1e02241 | 2011-06-16 05:51:02 +0000 | [diff] [blame] | 288 | SVal Extent = |
| 289 | svalBuilder.convertToArrayIndex(superReg->getExtent(svalBuilder)); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 290 | DefinedOrUnknownSVal Size = cast<DefinedOrUnknownSVal>(Extent); |
| 291 | |
| 292 | // Get the index of the accessed element. |
Gabor Greif | 89b0658 | 2010-09-09 10:51:37 +0000 | [diff] [blame] | 293 | DefinedOrUnknownSVal Idx = cast<DefinedOrUnknownSVal>(ER->getIndex()); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 294 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 295 | ProgramStateRef StInBound = state->assumeInBound(Idx, Size, true); |
| 296 | ProgramStateRef StOutBound = state->assumeInBound(Idx, Size, false); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 297 | if (StOutBound && !StInBound) { |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 298 | ExplodedNode *N = C.generateSink(StOutBound); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 299 | if (!N) |
| 300 | return NULL; |
| 301 | |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 302 | if (!BT_Bounds) { |
| 303 | BT_Bounds.reset(new BuiltinBug("Out-of-bound array access", |
| 304 | "Byte string function accesses out-of-bound array element")); |
| 305 | } |
| 306 | BuiltinBug *BT = static_cast<BuiltinBug*>(BT_Bounds.get()); |
| 307 | |
| 308 | // Generate a report for this bug. |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 309 | BugReport *report; |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 310 | if (warningMsg) { |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 311 | report = new BugReport(*BT, warningMsg, N); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 312 | } else { |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 313 | assert(CurrentFunctionDescription); |
| 314 | assert(CurrentFunctionDescription[0] != '\0'); |
| 315 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 316 | SmallString<80> buf; |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 317 | llvm::raw_svector_ostream os(buf); |
| 318 | os << (char)toupper(CurrentFunctionDescription[0]) |
| 319 | << &CurrentFunctionDescription[1] |
| 320 | << " accesses out-of-bound array element"; |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 321 | report = new BugReport(*BT, os.str(), N); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 322 | } |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 323 | |
| 324 | // FIXME: It would be nice to eventually make this diagnostic more clear, |
| 325 | // e.g., by referencing the original declaration or by saying *why* this |
| 326 | // reference is outside the range. |
| 327 | |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 328 | report->addRange(S->getSourceRange()); |
| 329 | C.EmitReport(report); |
| 330 | return NULL; |
| 331 | } |
| 332 | |
| 333 | // Array bound check succeeded. From this point forward the array bound |
| 334 | // should always succeed. |
| 335 | return StInBound; |
| 336 | } |
| 337 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 338 | ProgramStateRef CStringChecker::CheckBufferAccess(CheckerContext &C, |
| 339 | ProgramStateRef state, |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 340 | const Expr *Size, |
| 341 | const Expr *FirstBuf, |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 342 | const Expr *SecondBuf, |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 343 | const char *firstMessage, |
Jordy Rose | 5e5f150 | 2011-06-20 03:49:16 +0000 | [diff] [blame] | 344 | const char *secondMessage, |
| 345 | bool WarnAboutSize) const { |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 346 | // If a previous check has failed, propagate the failure. |
| 347 | if (!state) |
| 348 | return NULL; |
| 349 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 350 | SValBuilder &svalBuilder = C.getSValBuilder(); |
Jordy Rose | 1e02241 | 2011-06-16 05:51:02 +0000 | [diff] [blame] | 351 | ASTContext &Ctx = svalBuilder.getContext(); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 352 | const LocationContext *LCtx = C.getLocationContext(); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 353 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 354 | QualType sizeTy = Size->getType(); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 355 | QualType PtrTy = Ctx.getPointerType(Ctx.CharTy); |
| 356 | |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 357 | // Check that the first buffer is non-null. |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 358 | SVal BufVal = state->getSVal(FirstBuf, LCtx); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 359 | state = checkNonNull(C, state, FirstBuf, BufVal); |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 360 | if (!state) |
| 361 | return NULL; |
| 362 | |
Anna Zaks | 5730076 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 363 | // If out-of-bounds checking is turned off, skip the rest. |
| 364 | if (!Filter.CheckCStringOutOfBounds) |
| 365 | return state; |
| 366 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 367 | // Get the access length and make sure it is known. |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 368 | // FIXME: This assumes the caller has already checked that the access length |
| 369 | // is positive. And that it's unsigned. |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 370 | SVal LengthVal = state->getSVal(Size, LCtx); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 371 | NonLoc *Length = dyn_cast<NonLoc>(&LengthVal); |
| 372 | if (!Length) |
| 373 | return state; |
| 374 | |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 375 | // Compute the offset of the last element to be accessed: size-1. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 376 | NonLoc One = cast<NonLoc>(svalBuilder.makeIntVal(1, sizeTy)); |
| 377 | NonLoc LastOffset = cast<NonLoc>(svalBuilder.evalBinOpNN(state, BO_Sub, |
| 378 | *Length, One, sizeTy)); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 379 | |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 380 | // Check that the first buffer is sufficiently long. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 381 | SVal BufStart = svalBuilder.evalCast(BufVal, PtrTy, FirstBuf->getType()); |
Jordy Rose | b6a4026 | 2010-08-05 23:11:30 +0000 | [diff] [blame] | 382 | if (Loc *BufLoc = dyn_cast<Loc>(&BufStart)) { |
Jordy Rose | 5e5f150 | 2011-06-20 03:49:16 +0000 | [diff] [blame] | 383 | const Expr *warningExpr = (WarnAboutSize ? Size : FirstBuf); |
| 384 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 385 | SVal BufEnd = svalBuilder.evalBinOpLN(state, BO_Add, *BufLoc, |
| 386 | LastOffset, PtrTy); |
Jordy Rose | 5e5f150 | 2011-06-20 03:49:16 +0000 | [diff] [blame] | 387 | state = CheckLocation(C, state, warningExpr, BufEnd, firstMessage); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 388 | |
Jordy Rose | b6a4026 | 2010-08-05 23:11:30 +0000 | [diff] [blame] | 389 | // If the buffer isn't large enough, abort. |
| 390 | if (!state) |
| 391 | return NULL; |
| 392 | } |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 393 | |
| 394 | // If there's a second buffer, check it as well. |
| 395 | if (SecondBuf) { |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 396 | BufVal = state->getSVal(SecondBuf, LCtx); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 397 | state = checkNonNull(C, state, SecondBuf, BufVal); |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 398 | if (!state) |
| 399 | return NULL; |
| 400 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 401 | BufStart = svalBuilder.evalCast(BufVal, PtrTy, SecondBuf->getType()); |
Jordy Rose | b6a4026 | 2010-08-05 23:11:30 +0000 | [diff] [blame] | 402 | if (Loc *BufLoc = dyn_cast<Loc>(&BufStart)) { |
Jordy Rose | 5e5f150 | 2011-06-20 03:49:16 +0000 | [diff] [blame] | 403 | const Expr *warningExpr = (WarnAboutSize ? Size : SecondBuf); |
| 404 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 405 | SVal BufEnd = svalBuilder.evalBinOpLN(state, BO_Add, *BufLoc, |
| 406 | LastOffset, PtrTy); |
Jordy Rose | 5e5f150 | 2011-06-20 03:49:16 +0000 | [diff] [blame] | 407 | state = CheckLocation(C, state, warningExpr, BufEnd, secondMessage); |
Jordy Rose | b6a4026 | 2010-08-05 23:11:30 +0000 | [diff] [blame] | 408 | } |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | // Large enough or not, return this state! |
| 412 | return state; |
| 413 | } |
| 414 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 415 | ProgramStateRef CStringChecker::CheckOverlap(CheckerContext &C, |
| 416 | ProgramStateRef state, |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 417 | const Expr *Size, |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 418 | const Expr *First, |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 419 | const Expr *Second) const { |
Anna Zaks | 5730076 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 420 | if (!Filter.CheckCStringBufferOverlap) |
| 421 | return state; |
| 422 | |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 423 | // Do a simple check for overlap: if the two arguments are from the same |
| 424 | // buffer, see if the end of the first is greater than the start of the second |
| 425 | // or vice versa. |
| 426 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 427 | // If a previous check has failed, propagate the failure. |
| 428 | if (!state) |
| 429 | return NULL; |
| 430 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 431 | ProgramStateRef stateTrue, stateFalse; |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 432 | |
| 433 | // Get the buffer values and make sure they're known locations. |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 434 | const LocationContext *LCtx = C.getLocationContext(); |
| 435 | SVal firstVal = state->getSVal(First, LCtx); |
| 436 | SVal secondVal = state->getSVal(Second, LCtx); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 437 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 438 | Loc *firstLoc = dyn_cast<Loc>(&firstVal); |
| 439 | if (!firstLoc) |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 440 | return state; |
| 441 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 442 | Loc *secondLoc = dyn_cast<Loc>(&secondVal); |
| 443 | if (!secondLoc) |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 444 | return state; |
| 445 | |
| 446 | // Are the two values the same? |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 447 | SValBuilder &svalBuilder = C.getSValBuilder(); |
| 448 | llvm::tie(stateTrue, stateFalse) = |
| 449 | state->assume(svalBuilder.evalEQ(state, *firstLoc, *secondLoc)); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 450 | |
| 451 | if (stateTrue && !stateFalse) { |
| 452 | // If the values are known to be equal, that's automatically an overlap. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 453 | emitOverlapBug(C, stateTrue, First, Second); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 454 | return NULL; |
| 455 | } |
| 456 | |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 457 | // assume the two expressions are not equal. |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 458 | assert(stateFalse); |
| 459 | state = stateFalse; |
| 460 | |
| 461 | // Which value comes first? |
Jordy Rose | ee2fde1 | 2011-06-16 05:56:50 +0000 | [diff] [blame] | 462 | QualType cmpTy = svalBuilder.getConditionType(); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 463 | SVal reverse = svalBuilder.evalBinOpLL(state, BO_GT, |
| 464 | *firstLoc, *secondLoc, cmpTy); |
| 465 | DefinedOrUnknownSVal *reverseTest = dyn_cast<DefinedOrUnknownSVal>(&reverse); |
| 466 | if (!reverseTest) |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 467 | return state; |
| 468 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 469 | llvm::tie(stateTrue, stateFalse) = state->assume(*reverseTest); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 470 | if (stateTrue) { |
| 471 | if (stateFalse) { |
| 472 | // If we don't know which one comes first, we can't perform this test. |
| 473 | return state; |
| 474 | } else { |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 475 | // Switch the values so that firstVal is before secondVal. |
| 476 | Loc *tmpLoc = firstLoc; |
| 477 | firstLoc = secondLoc; |
| 478 | secondLoc = tmpLoc; |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 479 | |
| 480 | // Switch the Exprs as well, so that they still correspond. |
| 481 | const Expr *tmpExpr = First; |
| 482 | First = Second; |
| 483 | Second = tmpExpr; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | // Get the length, and make sure it too is known. |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 488 | SVal LengthVal = state->getSVal(Size, LCtx); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 489 | NonLoc *Length = dyn_cast<NonLoc>(&LengthVal); |
| 490 | if (!Length) |
| 491 | return state; |
| 492 | |
| 493 | // Convert the first buffer's start address to char*. |
| 494 | // Bail out if the cast fails. |
Jordy Rose | ee2fde1 | 2011-06-16 05:56:50 +0000 | [diff] [blame] | 495 | ASTContext &Ctx = svalBuilder.getContext(); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 496 | QualType CharPtrTy = Ctx.getPointerType(Ctx.CharTy); |
Jordy Rose | 1e02241 | 2011-06-16 05:51:02 +0000 | [diff] [blame] | 497 | SVal FirstStart = svalBuilder.evalCast(*firstLoc, CharPtrTy, |
| 498 | First->getType()); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 499 | Loc *FirstStartLoc = dyn_cast<Loc>(&FirstStart); |
| 500 | if (!FirstStartLoc) |
| 501 | return state; |
| 502 | |
| 503 | // Compute the end of the first buffer. Bail out if THAT fails. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 504 | SVal FirstEnd = svalBuilder.evalBinOpLN(state, BO_Add, |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 505 | *FirstStartLoc, *Length, CharPtrTy); |
| 506 | Loc *FirstEndLoc = dyn_cast<Loc>(&FirstEnd); |
| 507 | if (!FirstEndLoc) |
| 508 | return state; |
| 509 | |
| 510 | // Is the end of the first buffer past the start of the second buffer? |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 511 | SVal Overlap = svalBuilder.evalBinOpLL(state, BO_GT, |
| 512 | *FirstEndLoc, *secondLoc, cmpTy); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 513 | DefinedOrUnknownSVal *OverlapTest = dyn_cast<DefinedOrUnknownSVal>(&Overlap); |
| 514 | if (!OverlapTest) |
| 515 | return state; |
| 516 | |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 517 | llvm::tie(stateTrue, stateFalse) = state->assume(*OverlapTest); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 518 | |
| 519 | if (stateTrue && !stateFalse) { |
| 520 | // Overlap! |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 521 | emitOverlapBug(C, stateTrue, First, Second); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 522 | return NULL; |
| 523 | } |
| 524 | |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 525 | // assume the two expressions don't overlap. |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 526 | assert(stateFalse); |
| 527 | return stateFalse; |
| 528 | } |
| 529 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 530 | void CStringChecker::emitOverlapBug(CheckerContext &C, ProgramStateRef state, |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 531 | const Stmt *First, const Stmt *Second) const { |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 532 | ExplodedNode *N = C.generateSink(state); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 533 | if (!N) |
| 534 | return; |
| 535 | |
| 536 | if (!BT_Overlap) |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 537 | BT_Overlap.reset(new BugType("Unix API", "Improper arguments")); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 538 | |
| 539 | // Generate a report for this bug. |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 540 | BugReport *report = |
| 541 | new BugReport(*BT_Overlap, |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 542 | "Arguments must not be overlapping buffers", N); |
| 543 | report->addRange(First->getSourceRange()); |
| 544 | report->addRange(Second->getSourceRange()); |
| 545 | |
| 546 | C.EmitReport(report); |
| 547 | } |
| 548 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 549 | ProgramStateRef CStringChecker::checkAdditionOverflow(CheckerContext &C, |
| 550 | ProgramStateRef state, |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 551 | NonLoc left, |
| 552 | NonLoc right) const { |
Anna Zaks | 5730076 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 553 | // If out-of-bounds checking is turned off, skip the rest. |
| 554 | if (!Filter.CheckCStringOutOfBounds) |
| 555 | return state; |
| 556 | |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 557 | // If a previous check has failed, propagate the failure. |
| 558 | if (!state) |
| 559 | return NULL; |
| 560 | |
| 561 | SValBuilder &svalBuilder = C.getSValBuilder(); |
| 562 | BasicValueFactory &BVF = svalBuilder.getBasicValueFactory(); |
| 563 | |
| 564 | QualType sizeTy = svalBuilder.getContext().getSizeType(); |
| 565 | const llvm::APSInt &maxValInt = BVF.getMaxValue(sizeTy); |
| 566 | NonLoc maxVal = svalBuilder.makeIntVal(maxValInt); |
| 567 | |
Anna Zaks | e3d250e | 2011-12-11 18:43:40 +0000 | [diff] [blame] | 568 | SVal maxMinusRight; |
| 569 | if (isa<nonloc::ConcreteInt>(right)) { |
| 570 | maxMinusRight = svalBuilder.evalBinOpNN(state, BO_Sub, maxVal, right, |
| 571 | sizeTy); |
| 572 | } else { |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 573 | // Try switching the operands. (The order of these two assignments is |
| 574 | // important!) |
| 575 | maxMinusRight = svalBuilder.evalBinOpNN(state, BO_Sub, maxVal, left, |
| 576 | sizeTy); |
| 577 | left = right; |
| 578 | } |
| 579 | |
| 580 | if (NonLoc *maxMinusRightNL = dyn_cast<NonLoc>(&maxMinusRight)) { |
| 581 | QualType cmpTy = svalBuilder.getConditionType(); |
| 582 | // If left > max - right, we have an overflow. |
| 583 | SVal willOverflow = svalBuilder.evalBinOpNN(state, BO_GT, left, |
| 584 | *maxMinusRightNL, cmpTy); |
| 585 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 586 | ProgramStateRef stateOverflow, stateOkay; |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 587 | llvm::tie(stateOverflow, stateOkay) = |
| 588 | state->assume(cast<DefinedOrUnknownSVal>(willOverflow)); |
| 589 | |
| 590 | if (stateOverflow && !stateOkay) { |
| 591 | // We have an overflow. Emit a bug report. |
| 592 | ExplodedNode *N = C.generateSink(stateOverflow); |
| 593 | if (!N) |
| 594 | return NULL; |
| 595 | |
| 596 | if (!BT_AdditionOverflow) |
| 597 | BT_AdditionOverflow.reset(new BuiltinBug("API", |
| 598 | "Sum of expressions causes overflow")); |
| 599 | |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 600 | // This isn't a great error message, but this should never occur in real |
| 601 | // code anyway -- you'd have to create a buffer longer than a size_t can |
| 602 | // represent, which is sort of a contradiction. |
Jordy Rose | 8cc2491 | 2011-06-20 03:51:53 +0000 | [diff] [blame] | 603 | const char *warning = |
| 604 | "This expression will create a string whose length is too big to " |
| 605 | "be represented as a size_t"; |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 606 | |
| 607 | // Generate a report for this bug. |
Jordy Rose | 8cc2491 | 2011-06-20 03:51:53 +0000 | [diff] [blame] | 608 | BugReport *report = new BugReport(*BT_AdditionOverflow, warning, N); |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 609 | C.EmitReport(report); |
| 610 | |
| 611 | return NULL; |
| 612 | } |
| 613 | |
| 614 | // From now on, assume an overflow didn't occur. |
| 615 | assert(stateOkay); |
| 616 | state = stateOkay; |
| 617 | } |
| 618 | |
| 619 | return state; |
| 620 | } |
| 621 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 622 | ProgramStateRef CStringChecker::setCStringLength(ProgramStateRef state, |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 623 | const MemRegion *MR, |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 624 | SVal strLength) { |
| 625 | assert(!strLength.isUndef() && "Attempt to set an undefined string length"); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 626 | |
| 627 | MR = MR->StripCasts(); |
| 628 | |
| 629 | switch (MR->getKind()) { |
| 630 | case MemRegion::StringRegionKind: |
| 631 | // FIXME: This can happen if we strcpy() into a string region. This is |
| 632 | // undefined [C99 6.4.5p6], but we should still warn about it. |
| 633 | return state; |
| 634 | |
| 635 | case MemRegion::SymbolicRegionKind: |
| 636 | case MemRegion::AllocaRegionKind: |
| 637 | case MemRegion::VarRegionKind: |
| 638 | case MemRegion::FieldRegionKind: |
| 639 | case MemRegion::ObjCIvarRegionKind: |
Jordy Rose | 210c05b | 2011-06-15 05:14:03 +0000 | [diff] [blame] | 640 | // These are the types we can currently track string lengths for. |
| 641 | break; |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 642 | |
| 643 | case MemRegion::ElementRegionKind: |
| 644 | // FIXME: Handle element regions by upper-bounding the parent region's |
| 645 | // string length. |
| 646 | return state; |
| 647 | |
| 648 | default: |
| 649 | // Other regions (mostly non-data) can't have a reliable C string length. |
| 650 | // For now, just ignore the change. |
| 651 | // FIXME: These are rare but not impossible. We should output some kind of |
| 652 | // warning for things like strcpy((char[]){'a', 0}, "b"); |
| 653 | return state; |
| 654 | } |
Jordy Rose | 210c05b | 2011-06-15 05:14:03 +0000 | [diff] [blame] | 655 | |
| 656 | if (strLength.isUnknown()) |
| 657 | return state->remove<CStringLength>(MR); |
| 658 | |
| 659 | return state->set<CStringLength>(MR, strLength); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 662 | SVal CStringChecker::getCStringLengthForRegion(CheckerContext &C, |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 663 | ProgramStateRef &state, |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 664 | const Expr *Ex, |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 665 | const MemRegion *MR, |
| 666 | bool hypothetical) { |
| 667 | if (!hypothetical) { |
| 668 | // If there's a recorded length, go ahead and return it. |
| 669 | const SVal *Recorded = state->get<CStringLength>(MR); |
| 670 | if (Recorded) |
| 671 | return *Recorded; |
| 672 | } |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 673 | |
| 674 | // Otherwise, get a new symbol and update the state. |
Anna Zaks | 5d0ea6d | 2011-10-04 20:43:05 +0000 | [diff] [blame] | 675 | unsigned Count = C.getCurrentBlockCount(); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 676 | SValBuilder &svalBuilder = C.getSValBuilder(); |
| 677 | QualType sizeTy = svalBuilder.getContext().getSizeType(); |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 678 | SVal strLength = svalBuilder.getMetadataSymbolVal(CStringChecker::getTag(), |
| 679 | MR, Ex, sizeTy, Count); |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 680 | |
| 681 | if (!hypothetical) |
| 682 | state = state->set<CStringLength>(MR, strLength); |
| 683 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 684 | return strLength; |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 687 | SVal CStringChecker::getCStringLength(CheckerContext &C, ProgramStateRef &state, |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 688 | const Expr *Ex, SVal Buf, |
| 689 | bool hypothetical) const { |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 690 | const MemRegion *MR = Buf.getAsRegion(); |
| 691 | if (!MR) { |
| 692 | // If we can't get a region, see if it's something we /know/ isn't a |
| 693 | // C string. In the context of locations, the only time we can issue such |
| 694 | // a warning is for labels. |
| 695 | if (loc::GotoLabel *Label = dyn_cast<loc::GotoLabel>(&Buf)) { |
Anna Zaks | 5730076 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 696 | if (!Filter.CheckCStringNotNullTerm) |
| 697 | return UndefinedVal(); |
| 698 | |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 699 | if (ExplodedNode *N = C.addTransition(state)) { |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 700 | if (!BT_NotCString) |
Anna Zaks | 5730076 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 701 | BT_NotCString.reset(new BuiltinBug("Unix API", |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 702 | "Argument is not a null-terminated string.")); |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 703 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 704 | SmallString<120> buf; |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 705 | llvm::raw_svector_ostream os(buf); |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 706 | assert(CurrentFunctionDescription); |
| 707 | os << "Argument to " << CurrentFunctionDescription |
| 708 | << " is the address of the label '" << Label->getLabel()->getName() |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 709 | << "', which is not a null-terminated string"; |
| 710 | |
| 711 | // Generate a report for this bug. |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 712 | BugReport *report = new BugReport(*BT_NotCString, |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 713 | os.str(), N); |
| 714 | |
| 715 | report->addRange(Ex->getSourceRange()); |
| 716 | C.EmitReport(report); |
| 717 | } |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 718 | return UndefinedVal(); |
Anna Zaks | 5730076 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 719 | |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 722 | // If it's not a region and not a label, give up. |
| 723 | return UnknownVal(); |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 726 | // If we have a region, strip casts from it and see if we can figure out |
| 727 | // its length. For anything we can't figure out, just return UnknownVal. |
| 728 | MR = MR->StripCasts(); |
| 729 | |
| 730 | switch (MR->getKind()) { |
| 731 | case MemRegion::StringRegionKind: { |
| 732 | // Modifying the contents of string regions is undefined [C99 6.4.5p6], |
| 733 | // so we can assume that the byte length is the correct C string length. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 734 | SValBuilder &svalBuilder = C.getSValBuilder(); |
| 735 | QualType sizeTy = svalBuilder.getContext().getSizeType(); |
| 736 | const StringLiteral *strLit = cast<StringRegion>(MR)->getStringLiteral(); |
| 737 | return svalBuilder.makeIntVal(strLit->getByteLength(), sizeTy); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 738 | } |
| 739 | case MemRegion::SymbolicRegionKind: |
| 740 | case MemRegion::AllocaRegionKind: |
| 741 | case MemRegion::VarRegionKind: |
| 742 | case MemRegion::FieldRegionKind: |
| 743 | case MemRegion::ObjCIvarRegionKind: |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 744 | return getCStringLengthForRegion(C, state, Ex, MR, hypothetical); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 745 | case MemRegion::CompoundLiteralRegionKind: |
| 746 | // FIXME: Can we track this? Is it necessary? |
| 747 | return UnknownVal(); |
| 748 | case MemRegion::ElementRegionKind: |
| 749 | // FIXME: How can we handle this? It's not good enough to subtract the |
| 750 | // offset from the base string length; consider "123\x00567" and &a[5]. |
| 751 | return UnknownVal(); |
| 752 | default: |
| 753 | // Other regions (mostly non-data) can't have a reliable C string length. |
| 754 | // In this case, an error is emitted and UndefinedVal is returned. |
| 755 | // The caller should always be prepared to handle this case. |
Anna Zaks | 5730076 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 756 | if (!Filter.CheckCStringNotNullTerm) |
| 757 | return UndefinedVal(); |
| 758 | |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 759 | if (ExplodedNode *N = C.addTransition(state)) { |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 760 | if (!BT_NotCString) |
Anna Zaks | 5730076 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 761 | BT_NotCString.reset(new BuiltinBug("Unix API", |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 762 | "Argument is not a null-terminated string.")); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 763 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 764 | SmallString<120> buf; |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 765 | llvm::raw_svector_ostream os(buf); |
| 766 | |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 767 | assert(CurrentFunctionDescription); |
| 768 | os << "Argument to " << CurrentFunctionDescription << " is "; |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 769 | |
| 770 | if (SummarizeRegion(os, C.getASTContext(), MR)) |
| 771 | os << ", which is not a null-terminated string"; |
| 772 | else |
| 773 | os << "not a null-terminated string"; |
| 774 | |
| 775 | // Generate a report for this bug. |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 776 | BugReport *report = new BugReport(*BT_NotCString, |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 777 | os.str(), N); |
| 778 | |
| 779 | report->addRange(Ex->getSourceRange()); |
| 780 | C.EmitReport(report); |
| 781 | } |
| 782 | |
| 783 | return UndefinedVal(); |
| 784 | } |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 787 | const StringLiteral *CStringChecker::getCStringLiteral(CheckerContext &C, |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 788 | ProgramStateRef &state, const Expr *expr, SVal val) const { |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 789 | |
| 790 | // Get the memory region pointed to by the val. |
| 791 | const MemRegion *bufRegion = val.getAsRegion(); |
| 792 | if (!bufRegion) |
| 793 | return NULL; |
| 794 | |
| 795 | // Strip casts off the memory region. |
| 796 | bufRegion = bufRegion->StripCasts(); |
| 797 | |
| 798 | // Cast the memory region to a string region. |
| 799 | const StringRegion *strRegion= dyn_cast<StringRegion>(bufRegion); |
| 800 | if (!strRegion) |
| 801 | return NULL; |
| 802 | |
| 803 | // Return the actual string in the string region. |
| 804 | return strRegion->getStringLiteral(); |
| 805 | } |
| 806 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 807 | ProgramStateRef CStringChecker::InvalidateBuffer(CheckerContext &C, |
| 808 | ProgramStateRef state, |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 809 | const Expr *E, SVal V) { |
| 810 | Loc *L = dyn_cast<Loc>(&V); |
| 811 | if (!L) |
| 812 | return state; |
| 813 | |
| 814 | // FIXME: This is a simplified version of what's in CFRefCount.cpp -- it makes |
| 815 | // some assumptions about the value that CFRefCount can't. Even so, it should |
| 816 | // probably be refactored. |
| 817 | if (loc::MemRegionVal* MR = dyn_cast<loc::MemRegionVal>(L)) { |
| 818 | const MemRegion *R = MR->getRegion()->StripCasts(); |
| 819 | |
| 820 | // Are we dealing with an ElementRegion? If so, we should be invalidating |
| 821 | // the super-region. |
| 822 | if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) { |
| 823 | R = ER->getSuperRegion(); |
| 824 | // FIXME: What about layers of ElementRegions? |
| 825 | } |
| 826 | |
| 827 | // Invalidate this region. |
Anna Zaks | 5d0ea6d | 2011-10-04 20:43:05 +0000 | [diff] [blame] | 828 | unsigned Count = C.getCurrentBlockCount(); |
Jordy Rose | 537716a | 2011-08-27 22:51:26 +0000 | [diff] [blame] | 829 | return state->invalidateRegions(R, E, Count); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | // If we have a non-region value by chance, just remove the binding. |
| 833 | // FIXME: is this necessary or correct? This handles the non-Region |
| 834 | // cases. Is it ever valid to store to these? |
| 835 | return state->unbindLoc(*L); |
| 836 | } |
| 837 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 838 | bool CStringChecker::SummarizeRegion(raw_ostream &os, ASTContext &Ctx, |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 839 | const MemRegion *MR) { |
Ted Kremenek | 9697934 | 2011-08-12 20:02:48 +0000 | [diff] [blame] | 840 | const TypedValueRegion *TVR = dyn_cast<TypedValueRegion>(MR); |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 841 | |
Jordy Rose | 096aef9 | 2011-08-12 21:41:07 +0000 | [diff] [blame] | 842 | switch (MR->getKind()) { |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 843 | case MemRegion::FunctionTextRegionKind: { |
Jordy Rose | 096aef9 | 2011-08-12 21:41:07 +0000 | [diff] [blame] | 844 | const FunctionDecl *FD = cast<FunctionTextRegion>(MR)->getDecl(); |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 845 | if (FD) |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 846 | os << "the address of the function '" << *FD << '\''; |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 847 | else |
| 848 | os << "the address of a function"; |
| 849 | return true; |
| 850 | } |
| 851 | case MemRegion::BlockTextRegionKind: |
| 852 | os << "block text"; |
| 853 | return true; |
| 854 | case MemRegion::BlockDataRegionKind: |
| 855 | os << "a block"; |
| 856 | return true; |
| 857 | case MemRegion::CXXThisRegionKind: |
Zhongxing Xu | 02fe28c | 2010-11-26 08:52:48 +0000 | [diff] [blame] | 858 | case MemRegion::CXXTempObjectRegionKind: |
Ted Kremenek | 9697934 | 2011-08-12 20:02:48 +0000 | [diff] [blame] | 859 | os << "a C++ temp object of type " << TVR->getValueType().getAsString(); |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 860 | return true; |
| 861 | case MemRegion::VarRegionKind: |
Ted Kremenek | 9697934 | 2011-08-12 20:02:48 +0000 | [diff] [blame] | 862 | os << "a variable of type" << TVR->getValueType().getAsString(); |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 863 | return true; |
| 864 | case MemRegion::FieldRegionKind: |
Ted Kremenek | 9697934 | 2011-08-12 20:02:48 +0000 | [diff] [blame] | 865 | os << "a field of type " << TVR->getValueType().getAsString(); |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 866 | return true; |
| 867 | case MemRegion::ObjCIvarRegionKind: |
Ted Kremenek | 9697934 | 2011-08-12 20:02:48 +0000 | [diff] [blame] | 868 | os << "an instance variable of type " << TVR->getValueType().getAsString(); |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 869 | return true; |
| 870 | default: |
| 871 | return false; |
| 872 | } |
| 873 | } |
| 874 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 875 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 876 | // evaluation of individual function calls. |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 877 | //===----------------------------------------------------------------------===// |
| 878 | |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 879 | void CStringChecker::evalCopyCommon(CheckerContext &C, |
| 880 | const CallExpr *CE, |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 881 | ProgramStateRef state, |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 882 | const Expr *Size, const Expr *Dest, |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 883 | const Expr *Source, bool Restricted, |
| 884 | bool IsMempcpy) const { |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 885 | CurrentFunctionDescription = "memory copy function"; |
| 886 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 887 | // See if the size argument is zero. |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 888 | const LocationContext *LCtx = C.getLocationContext(); |
| 889 | SVal sizeVal = state->getSVal(Size, LCtx); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 890 | QualType sizeTy = Size->getType(); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 891 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 892 | ProgramStateRef stateZeroSize, stateNonZeroSize; |
Jordy Rose | 1e02241 | 2011-06-16 05:51:02 +0000 | [diff] [blame] | 893 | llvm::tie(stateZeroSize, stateNonZeroSize) = |
| 894 | assumeZero(C, state, sizeVal, sizeTy); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 895 | |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 896 | // Get the value of the Dest. |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 897 | SVal destVal = state->getSVal(Dest, LCtx); |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 898 | |
| 899 | // If the size is zero, there won't be any actual memory access, so |
| 900 | // just bind the return value to the destination buffer and return. |
| 901 | if (stateZeroSize) { |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 902 | stateZeroSize = stateZeroSize->BindExpr(CE, LCtx, destVal); |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 903 | C.addTransition(stateZeroSize); |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 904 | } |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 905 | |
| 906 | // If the size can be nonzero, we have to check the other arguments. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 907 | if (stateNonZeroSize) { |
Jordy Rose | 22d2717 | 2011-06-04 00:04:22 +0000 | [diff] [blame] | 908 | state = stateNonZeroSize; |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 909 | |
| 910 | // Ensure the destination is not null. If it is NULL there will be a |
| 911 | // NULL pointer dereference. |
| 912 | state = checkNonNull(C, state, Dest, destVal); |
| 913 | if (!state) |
| 914 | return; |
| 915 | |
| 916 | // Get the value of the Src. |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 917 | SVal srcVal = state->getSVal(Source, LCtx); |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 918 | |
| 919 | // Ensure the source is not null. If it is NULL there will be a |
| 920 | // NULL pointer dereference. |
| 921 | state = checkNonNull(C, state, Source, srcVal); |
| 922 | if (!state) |
| 923 | return; |
| 924 | |
Jordy Rose | 7182b96 | 2011-06-04 01:50:25 +0000 | [diff] [blame] | 925 | // Ensure the accesses are valid and that the buffers do not overlap. |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 926 | const char * const writeWarning = |
| 927 | "Memory copy function overflows destination buffer"; |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 928 | state = CheckBufferAccess(C, state, Size, Dest, Source, |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 929 | writeWarning, /* sourceWarning = */ NULL); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 930 | if (Restricted) |
| 931 | state = CheckOverlap(C, state, Size, Dest, Source); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 932 | |
Jordy Rose | 7182b96 | 2011-06-04 01:50:25 +0000 | [diff] [blame] | 933 | if (!state) |
| 934 | return; |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 935 | |
Jordy Rose | 7182b96 | 2011-06-04 01:50:25 +0000 | [diff] [blame] | 936 | // If this is mempcpy, get the byte after the last byte copied and |
| 937 | // bind the expr. |
| 938 | if (IsMempcpy) { |
| 939 | loc::MemRegionVal *destRegVal = dyn_cast<loc::MemRegionVal>(&destVal); |
| 940 | assert(destRegVal && "Destination should be a known MemRegionVal here"); |
| 941 | |
| 942 | // Get the length to copy. |
| 943 | NonLoc *lenValNonLoc = dyn_cast<NonLoc>(&sizeVal); |
| 944 | |
| 945 | if (lenValNonLoc) { |
| 946 | // Get the byte after the last byte copied. |
| 947 | SVal lastElement = C.getSValBuilder().evalBinOpLN(state, BO_Add, |
| 948 | *destRegVal, |
| 949 | *lenValNonLoc, |
| 950 | Dest->getType()); |
| 951 | |
| 952 | // The byte after the last byte copied is the return value. |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 953 | state = state->BindExpr(CE, LCtx, lastElement); |
Jordy Rose | 3f8bb2f | 2011-06-04 01:47:27 +0000 | [diff] [blame] | 954 | } else { |
Jordy Rose | 7182b96 | 2011-06-04 01:50:25 +0000 | [diff] [blame] | 955 | // If we don't know how much we copied, we can at least |
| 956 | // conjure a return value for later. |
Anna Zaks | 5d0ea6d | 2011-10-04 20:43:05 +0000 | [diff] [blame] | 957 | unsigned Count = C.getCurrentBlockCount(); |
Jordy Rose | 7182b96 | 2011-06-04 01:50:25 +0000 | [diff] [blame] | 958 | SVal result = |
| 959 | C.getSValBuilder().getConjuredSymbolVal(NULL, CE, Count); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 960 | state = state->BindExpr(CE, LCtx, result); |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 961 | } |
| 962 | |
Jordy Rose | 7182b96 | 2011-06-04 01:50:25 +0000 | [diff] [blame] | 963 | } else { |
| 964 | // All other copies return the destination buffer. |
| 965 | // (Well, bcopy() has a void return type, but this won't hurt.) |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 966 | state = state->BindExpr(CE, LCtx, destVal); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 967 | } |
Jordy Rose | 7182b96 | 2011-06-04 01:50:25 +0000 | [diff] [blame] | 968 | |
| 969 | // Invalidate the destination. |
| 970 | // FIXME: Even if we can't perfectly model the copy, we should see if we |
| 971 | // can use LazyCompoundVals to copy the source values into the destination. |
| 972 | // This would probably remove any existing bindings past the end of the |
| 973 | // copied region, but that's still an improvement over blank invalidation. |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 974 | state = InvalidateBuffer(C, state, Dest, |
| 975 | state->getSVal(Dest, C.getLocationContext())); |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 976 | C.addTransition(state); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 977 | } |
| 978 | } |
| 979 | |
| 980 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 981 | void CStringChecker::evalMemcpy(CheckerContext &C, const CallExpr *CE) const { |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 982 | // void *memcpy(void *restrict dst, const void *restrict src, size_t n); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 983 | // The return value is the address of the destination buffer. |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 984 | const Expr *Dest = CE->getArg(0); |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 985 | ProgramStateRef state = C.getState(); |
Jordy Rose | 3f8bb2f | 2011-06-04 01:47:27 +0000 | [diff] [blame] | 986 | |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 987 | evalCopyCommon(C, CE, state, CE->getArg(2), Dest, CE->getArg(1), true); |
| 988 | } |
| 989 | |
| 990 | void CStringChecker::evalMempcpy(CheckerContext &C, const CallExpr *CE) const { |
| 991 | // void *mempcpy(void *restrict dst, const void *restrict src, size_t n); |
| 992 | // The return value is a pointer to the byte following the last written byte. |
| 993 | const Expr *Dest = CE->getArg(0); |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 994 | ProgramStateRef state = C.getState(); |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 995 | |
| 996 | evalCopyCommon(C, CE, state, CE->getArg(2), Dest, CE->getArg(1), true, true); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 997 | } |
| 998 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 999 | void CStringChecker::evalMemmove(CheckerContext &C, const CallExpr *CE) const { |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1000 | // void *memmove(void *dst, const void *src, size_t n); |
| 1001 | // The return value is the address of the destination buffer. |
| 1002 | const Expr *Dest = CE->getArg(0); |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1003 | ProgramStateRef state = C.getState(); |
Jordy Rose | 3f8bb2f | 2011-06-04 01:47:27 +0000 | [diff] [blame] | 1004 | |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 1005 | evalCopyCommon(C, CE, state, CE->getArg(2), Dest, CE->getArg(1)); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1008 | void CStringChecker::evalBcopy(CheckerContext &C, const CallExpr *CE) const { |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1009 | // void bcopy(const void *src, void *dst, size_t n); |
Lenny Maiorani | b8b875b | 2011-03-31 21:36:53 +0000 | [diff] [blame] | 1010 | evalCopyCommon(C, CE, C.getState(), |
| 1011 | CE->getArg(2), CE->getArg(1), CE->getArg(0)); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1014 | void CStringChecker::evalMemcmp(CheckerContext &C, const CallExpr *CE) const { |
Jordy Rose | bc56d1f | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 1015 | // int memcmp(const void *s1, const void *s2, size_t n); |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 1016 | CurrentFunctionDescription = "memory comparison function"; |
| 1017 | |
Jordy Rose | bc56d1f | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 1018 | const Expr *Left = CE->getArg(0); |
| 1019 | const Expr *Right = CE->getArg(1); |
| 1020 | const Expr *Size = CE->getArg(2); |
| 1021 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1022 | ProgramStateRef state = C.getState(); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1023 | SValBuilder &svalBuilder = C.getSValBuilder(); |
Jordy Rose | bc56d1f | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 1024 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1025 | // See if the size argument is zero. |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1026 | const LocationContext *LCtx = C.getLocationContext(); |
| 1027 | SVal sizeVal = state->getSVal(Size, LCtx); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1028 | QualType sizeTy = Size->getType(); |
Jordy Rose | bc56d1f | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 1029 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1030 | ProgramStateRef stateZeroSize, stateNonZeroSize; |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1031 | llvm::tie(stateZeroSize, stateNonZeroSize) = |
| 1032 | assumeZero(C, state, sizeVal, sizeTy); |
Jordy Rose | bc56d1f | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 1033 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1034 | // If the size can be zero, the result will be 0 in that case, and we don't |
| 1035 | // have to check either of the buffers. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1036 | if (stateZeroSize) { |
| 1037 | state = stateZeroSize; |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1038 | state = state->BindExpr(CE, LCtx, |
| 1039 | svalBuilder.makeZeroVal(CE->getType())); |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 1040 | C.addTransition(state); |
Jordy Rose | bc56d1f | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1043 | // If the size can be nonzero, we have to check the other arguments. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1044 | if (stateNonZeroSize) { |
| 1045 | state = stateNonZeroSize; |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1046 | // If we know the two buffers are the same, we know the result is 0. |
| 1047 | // First, get the two buffers' addresses. Another checker will have already |
| 1048 | // made sure they're not undefined. |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1049 | DefinedOrUnknownSVal LV = |
| 1050 | cast<DefinedOrUnknownSVal>(state->getSVal(Left, LCtx)); |
| 1051 | DefinedOrUnknownSVal RV = |
| 1052 | cast<DefinedOrUnknownSVal>(state->getSVal(Right, LCtx)); |
Jordy Rose | bc56d1f | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 1053 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1054 | // See if they are the same. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1055 | DefinedOrUnknownSVal SameBuf = svalBuilder.evalEQ(state, LV, RV); |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1056 | ProgramStateRef StSameBuf, StNotSameBuf; |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 1057 | llvm::tie(StSameBuf, StNotSameBuf) = state->assume(SameBuf); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1058 | |
Jordy Rose | 1e02241 | 2011-06-16 05:51:02 +0000 | [diff] [blame] | 1059 | // If the two arguments might be the same buffer, we know the result is 0, |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1060 | // and we only need to check one size. |
| 1061 | if (StSameBuf) { |
| 1062 | state = StSameBuf; |
| 1063 | state = CheckBufferAccess(C, state, Size, Left); |
| 1064 | if (state) { |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1065 | state = StSameBuf->BindExpr(CE, LCtx, |
| 1066 | svalBuilder.makeZeroVal(CE->getType())); |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 1067 | C.addTransition(state); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | // If the two arguments might be different buffers, we have to check the |
| 1072 | // size of both of them. |
| 1073 | if (StNotSameBuf) { |
| 1074 | state = StNotSameBuf; |
| 1075 | state = CheckBufferAccess(C, state, Size, Left, Right); |
| 1076 | if (state) { |
| 1077 | // The return value is the comparison result, which we don't know. |
Anna Zaks | 5d0ea6d | 2011-10-04 20:43:05 +0000 | [diff] [blame] | 1078 | unsigned Count = C.getCurrentBlockCount(); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1079 | SVal CmpV = svalBuilder.getConjuredSymbolVal(NULL, CE, Count); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1080 | state = state->BindExpr(CE, LCtx, CmpV); |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 1081 | C.addTransition(state); |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1082 | } |
| 1083 | } |
| 1084 | } |
Jordy Rose | bc56d1f | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1087 | void CStringChecker::evalstrLength(CheckerContext &C, |
| 1088 | const CallExpr *CE) const { |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 1089 | // size_t strlen(const char *s); |
Ted Kremenek | be4242c | 2011-02-22 04:55:05 +0000 | [diff] [blame] | 1090 | evalstrLengthCommon(C, CE, /* IsStrnlen = */ false); |
| 1091 | } |
| 1092 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1093 | void CStringChecker::evalstrnLength(CheckerContext &C, |
| 1094 | const CallExpr *CE) const { |
Ted Kremenek | be4242c | 2011-02-22 04:55:05 +0000 | [diff] [blame] | 1095 | // size_t strnlen(const char *s, size_t maxlen); |
| 1096 | evalstrLengthCommon(C, CE, /* IsStrnlen = */ true); |
| 1097 | } |
| 1098 | |
| 1099 | void CStringChecker::evalstrLengthCommon(CheckerContext &C, const CallExpr *CE, |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1100 | bool IsStrnlen) const { |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 1101 | CurrentFunctionDescription = "string length function"; |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1102 | ProgramStateRef state = C.getState(); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1103 | const LocationContext *LCtx = C.getLocationContext(); |
Jordy Rose | 793bff3 | 2011-06-14 01:15:31 +0000 | [diff] [blame] | 1104 | |
| 1105 | if (IsStrnlen) { |
| 1106 | const Expr *maxlenExpr = CE->getArg(1); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1107 | SVal maxlenVal = state->getSVal(maxlenExpr, LCtx); |
Jordy Rose | 793bff3 | 2011-06-14 01:15:31 +0000 | [diff] [blame] | 1108 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1109 | ProgramStateRef stateZeroSize, stateNonZeroSize; |
Jordy Rose | 793bff3 | 2011-06-14 01:15:31 +0000 | [diff] [blame] | 1110 | llvm::tie(stateZeroSize, stateNonZeroSize) = |
| 1111 | assumeZero(C, state, maxlenVal, maxlenExpr->getType()); |
| 1112 | |
| 1113 | // If the size can be zero, the result will be 0 in that case, and we don't |
| 1114 | // have to check the string itself. |
| 1115 | if (stateZeroSize) { |
| 1116 | SVal zero = C.getSValBuilder().makeZeroVal(CE->getType()); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1117 | stateZeroSize = stateZeroSize->BindExpr(CE, LCtx, zero); |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 1118 | C.addTransition(stateZeroSize); |
Jordy Rose | 793bff3 | 2011-06-14 01:15:31 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | // If the size is GUARANTEED to be zero, we're done! |
| 1122 | if (!stateNonZeroSize) |
| 1123 | return; |
| 1124 | |
| 1125 | // Otherwise, record the assumption that the size is nonzero. |
| 1126 | state = stateNonZeroSize; |
| 1127 | } |
| 1128 | |
| 1129 | // Check that the string argument is non-null. |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 1130 | const Expr *Arg = CE->getArg(0); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1131 | SVal ArgVal = state->getSVal(Arg, LCtx); |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 1132 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1133 | state = checkNonNull(C, state, Arg, ArgVal); |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 1134 | |
Jordy Rose | bd32bee | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1135 | if (!state) |
| 1136 | return; |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1137 | |
Jordy Rose | bd32bee | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1138 | SVal strLength = getCStringLength(C, state, Arg, ArgVal); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1139 | |
Jordy Rose | bd32bee | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1140 | // If the argument isn't a valid C string, there's no valid state to |
| 1141 | // transition to. |
| 1142 | if (strLength.isUndef()) |
| 1143 | return; |
Jordy Rose | 793bff3 | 2011-06-14 01:15:31 +0000 | [diff] [blame] | 1144 | |
Jordy Rose | bd32bee | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1145 | DefinedOrUnknownSVal result = UnknownVal(); |
Jordy Rose | 793bff3 | 2011-06-14 01:15:31 +0000 | [diff] [blame] | 1146 | |
Jordy Rose | bd32bee | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1147 | // If the check is for strnlen() then bind the return value to no more than |
| 1148 | // the maxlen value. |
| 1149 | if (IsStrnlen) { |
Jordy Rose | ee2fde1 | 2011-06-16 05:56:50 +0000 | [diff] [blame] | 1150 | QualType cmpTy = C.getSValBuilder().getConditionType(); |
Jordy Rose | 793bff3 | 2011-06-14 01:15:31 +0000 | [diff] [blame] | 1151 | |
Jordy Rose | bd32bee | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1152 | // It's a little unfortunate to be getting this again, |
| 1153 | // but it's not that expensive... |
| 1154 | const Expr *maxlenExpr = CE->getArg(1); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1155 | SVal maxlenVal = state->getSVal(maxlenExpr, LCtx); |
Ted Kremenek | be4242c | 2011-02-22 04:55:05 +0000 | [diff] [blame] | 1156 | |
Jordy Rose | bd32bee | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1157 | NonLoc *strLengthNL = dyn_cast<NonLoc>(&strLength); |
| 1158 | NonLoc *maxlenValNL = dyn_cast<NonLoc>(&maxlenVal); |
Ted Kremenek | be4242c | 2011-02-22 04:55:05 +0000 | [diff] [blame] | 1159 | |
Jordy Rose | bd32bee | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1160 | if (strLengthNL && maxlenValNL) { |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1161 | ProgramStateRef stateStringTooLong, stateStringNotTooLong; |
Jordy Rose | 793bff3 | 2011-06-14 01:15:31 +0000 | [diff] [blame] | 1162 | |
Jordy Rose | bd32bee | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1163 | // Check if the strLength is greater than the maxlen. |
| 1164 | llvm::tie(stateStringTooLong, stateStringNotTooLong) = |
| 1165 | state->assume(cast<DefinedOrUnknownSVal> |
| 1166 | (C.getSValBuilder().evalBinOpNN(state, BO_GT, |
| 1167 | *strLengthNL, |
| 1168 | *maxlenValNL, |
| 1169 | cmpTy))); |
Jordy Rose | 793bff3 | 2011-06-14 01:15:31 +0000 | [diff] [blame] | 1170 | |
Jordy Rose | bd32bee | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1171 | if (stateStringTooLong && !stateStringNotTooLong) { |
| 1172 | // If the string is longer than maxlen, return maxlen. |
| 1173 | result = *maxlenValNL; |
| 1174 | } else if (stateStringNotTooLong && !stateStringTooLong) { |
| 1175 | // If the string is shorter than maxlen, return its length. |
| 1176 | result = *strLengthNL; |
Ted Kremenek | be4242c | 2011-02-22 04:55:05 +0000 | [diff] [blame] | 1177 | } |
| 1178 | } |
| 1179 | |
Jordy Rose | bd32bee | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1180 | if (result.isUnknown()) { |
| 1181 | // If we don't have enough information for a comparison, there's |
| 1182 | // no guarantee the full string length will actually be returned. |
| 1183 | // All we know is the return value is the min of the string length |
| 1184 | // and the limit. This is better than nothing. |
Anna Zaks | 5d0ea6d | 2011-10-04 20:43:05 +0000 | [diff] [blame] | 1185 | unsigned Count = C.getCurrentBlockCount(); |
Jordy Rose | bd32bee | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1186 | result = C.getSValBuilder().getConjuredSymbolVal(NULL, CE, Count); |
| 1187 | NonLoc *resultNL = cast<NonLoc>(&result); |
| 1188 | |
| 1189 | if (strLengthNL) { |
| 1190 | state = state->assume(cast<DefinedOrUnknownSVal> |
| 1191 | (C.getSValBuilder().evalBinOpNN(state, BO_LE, |
| 1192 | *resultNL, |
| 1193 | *strLengthNL, |
| 1194 | cmpTy)), true); |
| 1195 | } |
| 1196 | |
| 1197 | if (maxlenValNL) { |
| 1198 | state = state->assume(cast<DefinedOrUnknownSVal> |
| 1199 | (C.getSValBuilder().evalBinOpNN(state, BO_LE, |
| 1200 | *resultNL, |
| 1201 | *maxlenValNL, |
| 1202 | cmpTy)), true); |
| 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | } else { |
| 1207 | // This is a plain strlen(), not strnlen(). |
| 1208 | result = cast<DefinedOrUnknownSVal>(strLength); |
| 1209 | |
| 1210 | // If we don't know the length of the string, conjure a return |
| 1211 | // value, so it can be used in constraints, at least. |
| 1212 | if (result.isUnknown()) { |
Anna Zaks | 5d0ea6d | 2011-10-04 20:43:05 +0000 | [diff] [blame] | 1213 | unsigned Count = C.getCurrentBlockCount(); |
Jordy Rose | bd32bee | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1214 | result = C.getSValBuilder().getConjuredSymbolVal(NULL, CE, Count); |
| 1215 | } |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 1216 | } |
Jordy Rose | bd32bee | 2011-06-14 01:26:48 +0000 | [diff] [blame] | 1217 | |
| 1218 | // Bind the return value. |
| 1219 | assert(!result.isUnknown() && "Should have conjured a value by now"); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1220 | state = state->BindExpr(CE, LCtx, result); |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 1221 | C.addTransition(state); |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1224 | void CStringChecker::evalStrcpy(CheckerContext &C, const CallExpr *CE) const { |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1225 | // char *strcpy(char *restrict dst, const char *restrict src); |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1226 | evalStrcpyCommon(C, CE, |
| 1227 | /* returnEnd = */ false, |
| 1228 | /* isBounded = */ false, |
| 1229 | /* isAppending = */ false); |
Ted Kremenek | 0ef473f | 2011-02-22 04:58:34 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1232 | void CStringChecker::evalStrncpy(CheckerContext &C, const CallExpr *CE) const { |
Jordy Rose | c152586 | 2011-06-04 00:05:23 +0000 | [diff] [blame] | 1233 | // char *strncpy(char *restrict dst, const char *restrict src, size_t n); |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1234 | evalStrcpyCommon(C, CE, |
| 1235 | /* returnEnd = */ false, |
| 1236 | /* isBounded = */ true, |
| 1237 | /* isAppending = */ false); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1240 | void CStringChecker::evalStpcpy(CheckerContext &C, const CallExpr *CE) const { |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1241 | // char *stpcpy(char *restrict dst, const char *restrict src); |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1242 | evalStrcpyCommon(C, CE, |
| 1243 | /* returnEnd = */ true, |
| 1244 | /* isBounded = */ false, |
| 1245 | /* isAppending = */ false); |
| 1246 | } |
| 1247 | |
| 1248 | void CStringChecker::evalStrcat(CheckerContext &C, const CallExpr *CE) const { |
| 1249 | //char *strcat(char *restrict s1, const char *restrict s2); |
| 1250 | evalStrcpyCommon(C, CE, |
| 1251 | /* returnEnd = */ false, |
| 1252 | /* isBounded = */ false, |
| 1253 | /* isAppending = */ true); |
| 1254 | } |
| 1255 | |
| 1256 | void CStringChecker::evalStrncat(CheckerContext &C, const CallExpr *CE) const { |
| 1257 | //char *strncat(char *restrict s1, const char *restrict s2, size_t n); |
| 1258 | evalStrcpyCommon(C, CE, |
| 1259 | /* returnEnd = */ false, |
| 1260 | /* isBounded = */ true, |
| 1261 | /* isAppending = */ true); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 1264 | void CStringChecker::evalStrcpyCommon(CheckerContext &C, const CallExpr *CE, |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1265 | bool returnEnd, bool isBounded, |
| 1266 | bool isAppending) const { |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 1267 | CurrentFunctionDescription = "string copy function"; |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1268 | ProgramStateRef state = C.getState(); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1269 | const LocationContext *LCtx = C.getLocationContext(); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1270 | |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1271 | // Check that the destination is non-null. |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1272 | const Expr *Dst = CE->getArg(0); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1273 | SVal DstVal = state->getSVal(Dst, LCtx); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1274 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1275 | state = checkNonNull(C, state, Dst, DstVal); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1276 | if (!state) |
| 1277 | return; |
| 1278 | |
| 1279 | // Check that the source is non-null. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1280 | const Expr *srcExpr = CE->getArg(1); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1281 | SVal srcVal = state->getSVal(srcExpr, LCtx); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1282 | state = checkNonNull(C, state, srcExpr, srcVal); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1283 | if (!state) |
| 1284 | return; |
| 1285 | |
| 1286 | // Get the string length of the source. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1287 | SVal strLength = getCStringLength(C, state, srcExpr, srcVal); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1288 | |
| 1289 | // If the source isn't a valid C string, give up. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1290 | if (strLength.isUndef()) |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1291 | return; |
| 1292 | |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1293 | SValBuilder &svalBuilder = C.getSValBuilder(); |
| 1294 | QualType cmpTy = svalBuilder.getConditionType(); |
Jordy Rose | 8912aae | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1295 | QualType sizeTy = svalBuilder.getContext().getSizeType(); |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1296 | |
Jordy Rose | 8912aae | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1297 | // These two values allow checking two kinds of errors: |
| 1298 | // - actual overflows caused by a source that doesn't fit in the destination |
| 1299 | // - potential overflows caused by a bound that could exceed the destination |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1300 | SVal amountCopied = UnknownVal(); |
Jordy Rose | 8912aae | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1301 | SVal maxLastElementIndex = UnknownVal(); |
| 1302 | const char *boundWarning = NULL; |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1303 | |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1304 | // If the function is strncpy, strncat, etc... it is bounded. |
| 1305 | if (isBounded) { |
| 1306 | // Get the max number of characters to copy. |
Ted Kremenek | 0ef473f | 2011-02-22 04:58:34 +0000 | [diff] [blame] | 1307 | const Expr *lenExpr = CE->getArg(2); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1308 | SVal lenVal = state->getSVal(lenExpr, LCtx); |
Ted Kremenek | 0ef473f | 2011-02-22 04:58:34 +0000 | [diff] [blame] | 1309 | |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1310 | // Protect against misdeclared strncpy(). |
Jordy Rose | 8912aae | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1311 | lenVal = svalBuilder.evalCast(lenVal, sizeTy, lenExpr->getType()); |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1312 | |
Ted Kremenek | 0ef473f | 2011-02-22 04:58:34 +0000 | [diff] [blame] | 1313 | NonLoc *strLengthNL = dyn_cast<NonLoc>(&strLength); |
| 1314 | NonLoc *lenValNL = dyn_cast<NonLoc>(&lenVal); |
| 1315 | |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1316 | // If we know both values, we might be able to figure out how much |
| 1317 | // we're copying. |
| 1318 | if (strLengthNL && lenValNL) { |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1319 | ProgramStateRef stateSourceTooLong, stateSourceNotTooLong; |
Ted Kremenek | 0ef473f | 2011-02-22 04:58:34 +0000 | [diff] [blame] | 1320 | |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1321 | // Check if the max number to copy is less than the length of the src. |
Jordy Rose | 8912aae | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1322 | // If the bound is equal to the source length, strncpy won't null- |
| 1323 | // terminate the result! |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1324 | llvm::tie(stateSourceTooLong, stateSourceNotTooLong) = |
| 1325 | state->assume(cast<DefinedOrUnknownSVal> |
Jordy Rose | 8912aae | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1326 | (svalBuilder.evalBinOpNN(state, BO_GE, *strLengthNL, |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1327 | *lenValNL, cmpTy))); |
| 1328 | |
| 1329 | if (stateSourceTooLong && !stateSourceNotTooLong) { |
| 1330 | // Max number to copy is less than the length of the src, so the actual |
| 1331 | // strLength copied is the max number arg. |
| 1332 | state = stateSourceTooLong; |
| 1333 | amountCopied = lenVal; |
| 1334 | |
| 1335 | } else if (!stateSourceTooLong && stateSourceNotTooLong) { |
| 1336 | // The source buffer entirely fits in the bound. |
| 1337 | state = stateSourceNotTooLong; |
| 1338 | amountCopied = strLength; |
| 1339 | } |
| 1340 | } |
| 1341 | |
Jordy Rose | 5e5f150 | 2011-06-20 03:49:16 +0000 | [diff] [blame] | 1342 | // We still want to know if the bound is known to be too large. |
Jordy Rose | 8912aae | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1343 | if (lenValNL) { |
| 1344 | if (isAppending) { |
| 1345 | // For strncat, the check is strlen(dst) + lenVal < sizeof(dst) |
| 1346 | |
| 1347 | // Get the string length of the destination. If the destination is |
| 1348 | // memory that can't have a string length, we shouldn't be copying |
| 1349 | // into it anyway. |
| 1350 | SVal dstStrLength = getCStringLength(C, state, Dst, DstVal); |
| 1351 | if (dstStrLength.isUndef()) |
| 1352 | return; |
| 1353 | |
| 1354 | if (NonLoc *dstStrLengthNL = dyn_cast<NonLoc>(&dstStrLength)) { |
| 1355 | maxLastElementIndex = svalBuilder.evalBinOpNN(state, BO_Add, |
| 1356 | *lenValNL, |
| 1357 | *dstStrLengthNL, |
| 1358 | sizeTy); |
| 1359 | boundWarning = "Size argument is greater than the free space in the " |
| 1360 | "destination buffer"; |
| 1361 | } |
| 1362 | |
| 1363 | } else { |
| 1364 | // For strncpy, this is just checking that lenVal <= sizeof(dst) |
| 1365 | // (Yes, strncpy and strncat differ in how they treat termination. |
| 1366 | // strncat ALWAYS terminates, but strncpy doesn't.) |
| 1367 | NonLoc one = cast<NonLoc>(svalBuilder.makeIntVal(1, sizeTy)); |
| 1368 | maxLastElementIndex = svalBuilder.evalBinOpNN(state, BO_Sub, *lenValNL, |
| 1369 | one, sizeTy); |
| 1370 | boundWarning = "Size argument is greater than the length of the " |
| 1371 | "destination buffer"; |
| 1372 | } |
| 1373 | } |
Jordy Rose | 5e5f150 | 2011-06-20 03:49:16 +0000 | [diff] [blame] | 1374 | |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1375 | // If we couldn't pin down the copy length, at least bound it. |
Jordy Rose | 8912aae | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1376 | // FIXME: We should actually run this code path for append as well, but |
| 1377 | // right now it creates problems with constraints (since we can end up |
| 1378 | // trying to pass constraints from symbol to symbol). |
| 1379 | if (amountCopied.isUnknown() && !isAppending) { |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1380 | // Try to get a "hypothetical" string length symbol, which we can later |
| 1381 | // set as a real value if that turns out to be the case. |
| 1382 | amountCopied = getCStringLength(C, state, lenExpr, srcVal, true); |
| 1383 | assert(!amountCopied.isUndef()); |
| 1384 | |
| 1385 | if (NonLoc *amountCopiedNL = dyn_cast<NonLoc>(&amountCopied)) { |
| 1386 | if (lenValNL) { |
| 1387 | // amountCopied <= lenVal |
| 1388 | SVal copiedLessThanBound = svalBuilder.evalBinOpNN(state, BO_LE, |
| 1389 | *amountCopiedNL, |
| 1390 | *lenValNL, |
| 1391 | cmpTy); |
| 1392 | state = state->assume(cast<DefinedOrUnknownSVal>(copiedLessThanBound), |
| 1393 | true); |
| 1394 | if (!state) |
| 1395 | return; |
| 1396 | } |
| 1397 | |
| 1398 | if (strLengthNL) { |
| 1399 | // amountCopied <= strlen(source) |
| 1400 | SVal copiedLessThanSrc = svalBuilder.evalBinOpNN(state, BO_LE, |
| 1401 | *amountCopiedNL, |
| 1402 | *strLengthNL, |
| 1403 | cmpTy); |
| 1404 | state = state->assume(cast<DefinedOrUnknownSVal>(copiedLessThanSrc), |
| 1405 | true); |
| 1406 | if (!state) |
| 1407 | return; |
| 1408 | } |
| 1409 | } |
| 1410 | } |
| 1411 | |
| 1412 | } else { |
| 1413 | // The function isn't bounded. The amount copied should match the length |
| 1414 | // of the source buffer. |
| 1415 | amountCopied = strLength; |
Ted Kremenek | 0ef473f | 2011-02-22 04:58:34 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1418 | assert(state); |
| 1419 | |
| 1420 | // This represents the number of characters copied into the destination |
| 1421 | // buffer. (It may not actually be the strlen if the destination buffer |
| 1422 | // is not terminated.) |
| 1423 | SVal finalStrLength = UnknownVal(); |
| 1424 | |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1425 | // If this is an appending function (strcat, strncat...) then set the |
| 1426 | // string length to strlen(src) + strlen(dst) since the buffer will |
| 1427 | // ultimately contain both. |
| 1428 | if (isAppending) { |
Jordy Rose | 1e02241 | 2011-06-16 05:51:02 +0000 | [diff] [blame] | 1429 | // Get the string length of the destination. If the destination is memory |
| 1430 | // that can't have a string length, we shouldn't be copying into it anyway. |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1431 | SVal dstStrLength = getCStringLength(C, state, Dst, DstVal); |
| 1432 | if (dstStrLength.isUndef()) |
| 1433 | return; |
| 1434 | |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1435 | NonLoc *srcStrLengthNL = dyn_cast<NonLoc>(&amountCopied); |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1436 | NonLoc *dstStrLengthNL = dyn_cast<NonLoc>(&dstStrLength); |
| 1437 | |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1438 | // If we know both string lengths, we might know the final string length. |
| 1439 | if (srcStrLengthNL && dstStrLengthNL) { |
| 1440 | // Make sure the two lengths together don't overflow a size_t. |
| 1441 | state = checkAdditionOverflow(C, state, *srcStrLengthNL, *dstStrLengthNL); |
| 1442 | if (!state) |
| 1443 | return; |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1444 | |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1445 | finalStrLength = svalBuilder.evalBinOpNN(state, BO_Add, *srcStrLengthNL, |
| 1446 | *dstStrLengthNL, sizeTy); |
| 1447 | } |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1448 | |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1449 | // If we couldn't get a single value for the final string length, |
| 1450 | // we can at least bound it by the individual lengths. |
| 1451 | if (finalStrLength.isUnknown()) { |
| 1452 | // Try to get a "hypothetical" string length symbol, which we can later |
| 1453 | // set as a real value if that turns out to be the case. |
| 1454 | finalStrLength = getCStringLength(C, state, CE, DstVal, true); |
| 1455 | assert(!finalStrLength.isUndef()); |
| 1456 | |
| 1457 | if (NonLoc *finalStrLengthNL = dyn_cast<NonLoc>(&finalStrLength)) { |
| 1458 | if (srcStrLengthNL) { |
| 1459 | // finalStrLength >= srcStrLength |
| 1460 | SVal sourceInResult = svalBuilder.evalBinOpNN(state, BO_GE, |
| 1461 | *finalStrLengthNL, |
| 1462 | *srcStrLengthNL, |
| 1463 | cmpTy); |
| 1464 | state = state->assume(cast<DefinedOrUnknownSVal>(sourceInResult), |
| 1465 | true); |
| 1466 | if (!state) |
| 1467 | return; |
| 1468 | } |
| 1469 | |
| 1470 | if (dstStrLengthNL) { |
| 1471 | // finalStrLength >= dstStrLength |
| 1472 | SVal destInResult = svalBuilder.evalBinOpNN(state, BO_GE, |
| 1473 | *finalStrLengthNL, |
| 1474 | *dstStrLengthNL, |
| 1475 | cmpTy); |
| 1476 | state = state->assume(cast<DefinedOrUnknownSVal>(destInResult), |
| 1477 | true); |
| 1478 | if (!state) |
| 1479 | return; |
| 1480 | } |
| 1481 | } |
| 1482 | } |
| 1483 | |
| 1484 | } else { |
| 1485 | // Otherwise, this is a copy-over function (strcpy, strncpy, ...), and |
| 1486 | // the final string length will match the input string length. |
| 1487 | finalStrLength = amountCopied; |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1490 | // The final result of the function will either be a pointer past the last |
| 1491 | // copied element, or a pointer to the start of the destination buffer. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1492 | SVal Result = (returnEnd ? UnknownVal() : DstVal); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1493 | |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1494 | assert(state); |
| 1495 | |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1496 | // If the destination is a MemRegion, try to check for a buffer overflow and |
| 1497 | // record the new string length. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1498 | if (loc::MemRegionVal *dstRegVal = dyn_cast<loc::MemRegionVal>(&DstVal)) { |
Jordy Rose | 8912aae | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1499 | QualType ptrTy = Dst->getType(); |
| 1500 | |
| 1501 | // If we have an exact value on a bounded copy, use that to check for |
| 1502 | // overflows, rather than our estimate about how much is actually copied. |
| 1503 | if (boundWarning) { |
| 1504 | if (NonLoc *maxLastNL = dyn_cast<NonLoc>(&maxLastElementIndex)) { |
| 1505 | SVal maxLastElement = svalBuilder.evalBinOpLN(state, BO_Add, *dstRegVal, |
| 1506 | *maxLastNL, ptrTy); |
| 1507 | state = CheckLocation(C, state, CE->getArg(2), maxLastElement, |
| 1508 | boundWarning); |
| 1509 | if (!state) |
| 1510 | return; |
| 1511 | } |
| 1512 | } |
| 1513 | |
| 1514 | // Then, if the final length is known... |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1515 | if (NonLoc *knownStrLength = dyn_cast<NonLoc>(&finalStrLength)) { |
| 1516 | SVal lastElement = svalBuilder.evalBinOpLN(state, BO_Add, *dstRegVal, |
Jordy Rose | 8912aae | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1517 | *knownStrLength, ptrTy); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1518 | |
Jordy Rose | 8912aae | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1519 | // ...and we haven't checked the bound, we'll check the actual copy. |
| 1520 | if (!boundWarning) { |
| 1521 | const char * const warningMsg = |
| 1522 | "String copy function overflows destination buffer"; |
| 1523 | state = CheckLocation(C, state, Dst, lastElement, warningMsg); |
| 1524 | if (!state) |
| 1525 | return; |
| 1526 | } |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1527 | |
| 1528 | // If this is a stpcpy-style copy, the last element is the return value. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1529 | if (returnEnd) |
| 1530 | Result = lastElement; |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | // Invalidate the destination. This must happen before we set the C string |
| 1534 | // length because invalidation will clear the length. |
| 1535 | // FIXME: Even if we can't perfectly model the copy, we should see if we |
| 1536 | // can use LazyCompoundVals to copy the source values into the destination. |
| 1537 | // This would probably remove any existing bindings past the end of the |
| 1538 | // string, but that's still an improvement over blank invalidation. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1539 | state = InvalidateBuffer(C, state, Dst, *dstRegVal); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1540 | |
Jordy Rose | 5e5f150 | 2011-06-20 03:49:16 +0000 | [diff] [blame] | 1541 | // Set the C string length of the destination, if we know it. |
Jordy Rose | 8912aae | 2011-06-20 21:55:40 +0000 | [diff] [blame] | 1542 | if (isBounded && !isAppending) { |
| 1543 | // strncpy is annoying in that it doesn't guarantee to null-terminate |
| 1544 | // the result string. If the original string didn't fit entirely inside |
| 1545 | // the bound (including the null-terminator), we don't know how long the |
| 1546 | // result is. |
| 1547 | if (amountCopied != strLength) |
| 1548 | finalStrLength = UnknownVal(); |
| 1549 | } |
| 1550 | state = setCStringLength(state, dstRegVal->getRegion(), finalStrLength); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1553 | assert(state); |
| 1554 | |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1555 | // If this is a stpcpy-style copy, but we were unable to check for a buffer |
| 1556 | // overflow, we still need a result. Conjure a return value. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1557 | if (returnEnd && Result.isUnknown()) { |
Anna Zaks | 5d0ea6d | 2011-10-04 20:43:05 +0000 | [diff] [blame] | 1558 | unsigned Count = C.getCurrentBlockCount(); |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1559 | Result = svalBuilder.getConjuredSymbolVal(NULL, CE, Count); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | // Set the return value. |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1563 | state = state->BindExpr(CE, LCtx, Result); |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 1564 | C.addTransition(state); |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 1567 | void CStringChecker::evalStrcmp(CheckerContext &C, const CallExpr *CE) const { |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1568 | //int strcmp(const char *s1, const char *s2); |
Lenny Maiorani | bd1d16a | 2011-04-28 15:09:11 +0000 | [diff] [blame] | 1569 | evalStrcmpCommon(C, CE, /* isBounded = */ false, /* ignoreCase = */ false); |
Lenny Maiorani | 357f6ee | 2011-04-25 22:21:00 +0000 | [diff] [blame] | 1570 | } |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 1571 | |
Lenny Maiorani | 357f6ee | 2011-04-25 22:21:00 +0000 | [diff] [blame] | 1572 | void CStringChecker::evalStrncmp(CheckerContext &C, const CallExpr *CE) const { |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1573 | //int strncmp(const char *s1, const char *s2, size_t n); |
Lenny Maiorani | bd1d16a | 2011-04-28 15:09:11 +0000 | [diff] [blame] | 1574 | evalStrcmpCommon(C, CE, /* isBounded = */ true, /* ignoreCase = */ false); |
| 1575 | } |
| 1576 | |
| 1577 | void CStringChecker::evalStrcasecmp(CheckerContext &C, |
| 1578 | const CallExpr *CE) const { |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1579 | //int strcasecmp(const char *s1, const char *s2); |
Lenny Maiorani | bd1d16a | 2011-04-28 15:09:11 +0000 | [diff] [blame] | 1580 | evalStrcmpCommon(C, CE, /* isBounded = */ false, /* ignoreCase = */ true); |
Lenny Maiorani | 357f6ee | 2011-04-25 22:21:00 +0000 | [diff] [blame] | 1581 | } |
| 1582 | |
Lenny Maiorani | 454fd2d | 2011-05-02 19:05:49 +0000 | [diff] [blame] | 1583 | void CStringChecker::evalStrncasecmp(CheckerContext &C, |
| 1584 | const CallExpr *CE) const { |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1585 | //int strncasecmp(const char *s1, const char *s2, size_t n); |
Lenny Maiorani | 454fd2d | 2011-05-02 19:05:49 +0000 | [diff] [blame] | 1586 | evalStrcmpCommon(C, CE, /* isBounded = */ true, /* ignoreCase = */ true); |
| 1587 | } |
| 1588 | |
Lenny Maiorani | 357f6ee | 2011-04-25 22:21:00 +0000 | [diff] [blame] | 1589 | void CStringChecker::evalStrcmpCommon(CheckerContext &C, const CallExpr *CE, |
Lenny Maiorani | bd1d16a | 2011-04-28 15:09:11 +0000 | [diff] [blame] | 1590 | bool isBounded, bool ignoreCase) const { |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 1591 | CurrentFunctionDescription = "string comparison function"; |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1592 | ProgramStateRef state = C.getState(); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1593 | const LocationContext *LCtx = C.getLocationContext(); |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 1594 | |
| 1595 | // Check that the first string is non-null |
| 1596 | const Expr *s1 = CE->getArg(0); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1597 | SVal s1Val = state->getSVal(s1, LCtx); |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 1598 | state = checkNonNull(C, state, s1, s1Val); |
| 1599 | if (!state) |
| 1600 | return; |
| 1601 | |
| 1602 | // Check that the second string is non-null. |
| 1603 | const Expr *s2 = CE->getArg(1); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1604 | SVal s2Val = state->getSVal(s2, LCtx); |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 1605 | state = checkNonNull(C, state, s2, s2Val); |
| 1606 | if (!state) |
| 1607 | return; |
| 1608 | |
| 1609 | // Get the string length of the first string or give up. |
| 1610 | SVal s1Length = getCStringLength(C, state, s1, s1Val); |
| 1611 | if (s1Length.isUndef()) |
| 1612 | return; |
| 1613 | |
| 1614 | // Get the string length of the second string or give up. |
| 1615 | SVal s2Length = getCStringLength(C, state, s2, s2Val); |
| 1616 | if (s2Length.isUndef()) |
| 1617 | return; |
| 1618 | |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1619 | // If we know the two buffers are the same, we know the result is 0. |
| 1620 | // First, get the two buffers' addresses. Another checker will have already |
| 1621 | // made sure they're not undefined. |
| 1622 | DefinedOrUnknownSVal LV = cast<DefinedOrUnknownSVal>(s1Val); |
| 1623 | DefinedOrUnknownSVal RV = cast<DefinedOrUnknownSVal>(s2Val); |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 1624 | |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1625 | // See if they are the same. |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 1626 | SValBuilder &svalBuilder = C.getSValBuilder(); |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1627 | DefinedOrUnknownSVal SameBuf = svalBuilder.evalEQ(state, LV, RV); |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1628 | ProgramStateRef StSameBuf, StNotSameBuf; |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1629 | llvm::tie(StSameBuf, StNotSameBuf) = state->assume(SameBuf); |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 1630 | |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1631 | // If the two arguments might be the same buffer, we know the result is 0, |
| 1632 | // and we only need to check one size. |
| 1633 | if (StSameBuf) { |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1634 | StSameBuf = StSameBuf->BindExpr(CE, LCtx, |
| 1635 | svalBuilder.makeZeroVal(CE->getType())); |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 1636 | C.addTransition(StSameBuf); |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1637 | |
| 1638 | // If the two arguments are GUARANTEED to be the same, we're done! |
| 1639 | if (!StNotSameBuf) |
| 1640 | return; |
| 1641 | } |
| 1642 | |
| 1643 | assert(StNotSameBuf); |
| 1644 | state = StNotSameBuf; |
| 1645 | |
| 1646 | // At this point we can go about comparing the two buffers. |
| 1647 | // For now, we only do this if they're both known string literals. |
| 1648 | |
| 1649 | // Attempt to extract string literals from both expressions. |
| 1650 | const StringLiteral *s1StrLiteral = getCStringLiteral(C, state, s1, s1Val); |
| 1651 | const StringLiteral *s2StrLiteral = getCStringLiteral(C, state, s2, s2Val); |
| 1652 | bool canComputeResult = false; |
| 1653 | |
| 1654 | if (s1StrLiteral && s2StrLiteral) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1655 | StringRef s1StrRef = s1StrLiteral->getString(); |
| 1656 | StringRef s2StrRef = s2StrLiteral->getString(); |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1657 | |
| 1658 | if (isBounded) { |
| 1659 | // Get the max number of characters to compare. |
| 1660 | const Expr *lenExpr = CE->getArg(2); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1661 | SVal lenVal = state->getSVal(lenExpr, LCtx); |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1662 | |
| 1663 | // If the length is known, we can get the right substrings. |
| 1664 | if (const llvm::APSInt *len = svalBuilder.getKnownValue(state, lenVal)) { |
| 1665 | // Create substrings of each to compare the prefix. |
| 1666 | s1StrRef = s1StrRef.substr(0, (size_t)len->getZExtValue()); |
| 1667 | s2StrRef = s2StrRef.substr(0, (size_t)len->getZExtValue()); |
| 1668 | canComputeResult = true; |
| 1669 | } |
| 1670 | } else { |
| 1671 | // This is a normal, unbounded strcmp. |
| 1672 | canComputeResult = true; |
| 1673 | } |
| 1674 | |
| 1675 | if (canComputeResult) { |
| 1676 | // Real strcmp stops at null characters. |
| 1677 | size_t s1Term = s1StrRef.find('\0'); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1678 | if (s1Term != StringRef::npos) |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1679 | s1StrRef = s1StrRef.substr(0, s1Term); |
| 1680 | |
| 1681 | size_t s2Term = s2StrRef.find('\0'); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1682 | if (s2Term != StringRef::npos) |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1683 | s2StrRef = s2StrRef.substr(0, s2Term); |
| 1684 | |
| 1685 | // Use StringRef's comparison methods to compute the actual result. |
| 1686 | int result; |
| 1687 | |
| 1688 | if (ignoreCase) { |
| 1689 | // Compare string 1 to string 2 the same way strcasecmp() does. |
| 1690 | result = s1StrRef.compare_lower(s2StrRef); |
| 1691 | } else { |
| 1692 | // Compare string 1 to string 2 the same way strcmp() does. |
| 1693 | result = s1StrRef.compare(s2StrRef); |
| 1694 | } |
| 1695 | |
| 1696 | // Build the SVal of the comparison and bind the return value. |
| 1697 | SVal resultVal = svalBuilder.makeIntVal(result, CE->getType()); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1698 | state = state->BindExpr(CE, LCtx, resultVal); |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1699 | } |
| 1700 | } |
| 1701 | |
| 1702 | if (!canComputeResult) { |
| 1703 | // Conjure a symbolic value. It's the best we can do. |
Anna Zaks | 5d0ea6d | 2011-10-04 20:43:05 +0000 | [diff] [blame] | 1704 | unsigned Count = C.getCurrentBlockCount(); |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1705 | SVal resultVal = svalBuilder.getConjuredSymbolVal(NULL, CE, Count); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1706 | state = state->BindExpr(CE, LCtx, resultVal); |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame] | 1707 | } |
| 1708 | |
| 1709 | // Record this as a possible path. |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 1710 | C.addTransition(state); |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1713 | //===----------------------------------------------------------------------===// |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1714 | // The driver method, and other Checker callbacks. |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1715 | //===----------------------------------------------------------------------===// |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 1716 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1717 | bool CStringChecker::evalCall(const CallExpr *CE, CheckerContext &C) const { |
Anna Zaks | b805c8f | 2011-12-01 05:57:37 +0000 | [diff] [blame] | 1718 | StringRef Name = C.getCalleeName(CE); |
| 1719 | if (Name.empty()) |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 1720 | return false; |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 1721 | if (Name.startswith("__builtin_")) |
| 1722 | Name = Name.substr(10); |
| 1723 | |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 1724 | FnCheck evalFunction = llvm::StringSwitch<FnCheck>(Name) |
| 1725 | .Cases("memcpy", "__memcpy_chk", &CStringChecker::evalMemcpy) |
Jordy Rose | be460d8 | 2011-06-03 23:42:56 +0000 | [diff] [blame] | 1726 | .Cases("mempcpy", "__mempcpy_chk", &CStringChecker::evalMempcpy) |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 1727 | .Cases("memcmp", "bcmp", &CStringChecker::evalMemcmp) |
| 1728 | .Cases("memmove", "__memmove_chk", &CStringChecker::evalMemmove) |
| 1729 | .Cases("strcpy", "__strcpy_chk", &CStringChecker::evalStrcpy) |
Jordy Rose | 5e5f150 | 2011-06-20 03:49:16 +0000 | [diff] [blame] | 1730 | .Cases("strncpy", "__strncpy_chk", &CStringChecker::evalStrncpy) |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 1731 | .Cases("stpcpy", "__stpcpy_chk", &CStringChecker::evalStpcpy) |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 1732 | .Cases("strcat", "__strcat_chk", &CStringChecker::evalStrcat) |
| 1733 | .Cases("strncat", "__strncat_chk", &CStringChecker::evalStrncat) |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1734 | .Case("strlen", &CStringChecker::evalstrLength) |
Ted Kremenek | be4242c | 2011-02-22 04:55:05 +0000 | [diff] [blame] | 1735 | .Case("strnlen", &CStringChecker::evalstrnLength) |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 1736 | .Case("strcmp", &CStringChecker::evalStrcmp) |
Lenny Maiorani | 357f6ee | 2011-04-25 22:21:00 +0000 | [diff] [blame] | 1737 | .Case("strncmp", &CStringChecker::evalStrncmp) |
Lenny Maiorani | bd1d16a | 2011-04-28 15:09:11 +0000 | [diff] [blame] | 1738 | .Case("strcasecmp", &CStringChecker::evalStrcasecmp) |
Lenny Maiorani | 454fd2d | 2011-05-02 19:05:49 +0000 | [diff] [blame] | 1739 | .Case("strncasecmp", &CStringChecker::evalStrncasecmp) |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 1740 | .Case("bcopy", &CStringChecker::evalBcopy) |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 1741 | .Default(NULL); |
| 1742 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1743 | // If the callee isn't a string function, let another checker handle it. |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 1744 | if (!evalFunction) |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 1745 | return false; |
| 1746 | |
Jordy Rose | 9e49d9f | 2011-06-20 02:06:40 +0000 | [diff] [blame] | 1747 | // Make sure each function sets its own description. |
| 1748 | // (But don't bother in a release build.) |
| 1749 | assert(!(CurrentFunctionDescription = NULL)); |
| 1750 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 1751 | // Check and evaluate the call. |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 1752 | (this->*evalFunction)(C, CE); |
Anna Zaks | 5730076 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 1753 | |
| 1754 | // If the evaluate call resulted in no change, chain to the next eval call |
| 1755 | // handler. |
| 1756 | // Note, the custom CString evaluation calls assume that basic safety |
| 1757 | // properties are held. However, if the user chooses to turn off some of these |
| 1758 | // checks, we ignore the issues and leave the call evaluation to a generic |
| 1759 | // handler. |
| 1760 | if (!C.isDifferent()) |
| 1761 | return false; |
| 1762 | |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 1763 | return true; |
| 1764 | } |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1765 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1766 | void CStringChecker::checkPreStmt(const DeclStmt *DS, CheckerContext &C) const { |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1767 | // Record string length for char a[] = "abc"; |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1768 | ProgramStateRef state = C.getState(); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1769 | |
| 1770 | for (DeclStmt::const_decl_iterator I = DS->decl_begin(), E = DS->decl_end(); |
| 1771 | I != E; ++I) { |
| 1772 | const VarDecl *D = dyn_cast<VarDecl>(*I); |
| 1773 | if (!D) |
| 1774 | continue; |
| 1775 | |
| 1776 | // FIXME: Handle array fields of structs. |
| 1777 | if (!D->getType()->isArrayType()) |
| 1778 | continue; |
| 1779 | |
| 1780 | const Expr *Init = D->getInit(); |
| 1781 | if (!Init) |
| 1782 | continue; |
| 1783 | if (!isa<StringLiteral>(Init)) |
| 1784 | continue; |
| 1785 | |
Anna Zaks | 39ac187 | 2011-10-26 21:06:44 +0000 | [diff] [blame] | 1786 | Loc VarLoc = state->getLValue(D, C.getLocationContext()); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1787 | const MemRegion *MR = VarLoc.getAsRegion(); |
| 1788 | if (!MR) |
| 1789 | continue; |
| 1790 | |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1791 | SVal StrVal = state->getSVal(Init, C.getLocationContext()); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1792 | assert(StrVal.isValid() && "Initializer string is unknown or undefined"); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1793 | DefinedOrUnknownSVal strLength |
| 1794 | = cast<DefinedOrUnknownSVal>(getCStringLength(C, state, Init, StrVal)); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1795 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1796 | state = state->set<CStringLength>(MR, strLength); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 1799 | C.addTransition(state); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1800 | } |
| 1801 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1802 | bool CStringChecker::wantsRegionChangeUpdate(ProgramStateRef state) const { |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1803 | CStringLength::EntryMap Entries = state->get<CStringLength>(); |
| 1804 | return !Entries.isEmpty(); |
| 1805 | } |
| 1806 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1807 | ProgramStateRef |
| 1808 | CStringChecker::checkRegionChanges(ProgramStateRef state, |
Ted Kremenek | 35bdbf4 | 2011-05-02 19:42:42 +0000 | [diff] [blame] | 1809 | const StoreManager::InvalidatedSymbols *, |
Jordy Rose | 537716a | 2011-08-27 22:51:26 +0000 | [diff] [blame] | 1810 | ArrayRef<const MemRegion *> ExplicitRegions, |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 1811 | ArrayRef<const MemRegion *> Regions, |
| 1812 | const CallOrObjCMessage *Call) const { |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1813 | CStringLength::EntryMap Entries = state->get<CStringLength>(); |
| 1814 | if (Entries.isEmpty()) |
| 1815 | return state; |
| 1816 | |
| 1817 | llvm::SmallPtrSet<const MemRegion *, 8> Invalidated; |
| 1818 | llvm::SmallPtrSet<const MemRegion *, 32> SuperRegions; |
| 1819 | |
| 1820 | // First build sets for the changed regions and their super-regions. |
Jordy Rose | 537716a | 2011-08-27 22:51:26 +0000 | [diff] [blame] | 1821 | for (ArrayRef<const MemRegion *>::iterator |
| 1822 | I = Regions.begin(), E = Regions.end(); I != E; ++I) { |
| 1823 | const MemRegion *MR = *I; |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1824 | Invalidated.insert(MR); |
| 1825 | |
| 1826 | SuperRegions.insert(MR); |
| 1827 | while (const SubRegion *SR = dyn_cast<SubRegion>(MR)) { |
| 1828 | MR = SR->getSuperRegion(); |
| 1829 | SuperRegions.insert(MR); |
| 1830 | } |
| 1831 | } |
| 1832 | |
| 1833 | CStringLength::EntryMap::Factory &F = state->get_context<CStringLength>(); |
| 1834 | |
| 1835 | // Then loop over the entries in the current state. |
| 1836 | for (CStringLength::EntryMap::iterator I = Entries.begin(), |
| 1837 | E = Entries.end(); I != E; ++I) { |
| 1838 | const MemRegion *MR = I.getKey(); |
| 1839 | |
| 1840 | // Is this entry for a super-region of a changed region? |
| 1841 | if (SuperRegions.count(MR)) { |
Ted Kremenek | 3baf672 | 2010-11-24 00:54:37 +0000 | [diff] [blame] | 1842 | Entries = F.remove(Entries, MR); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1843 | continue; |
| 1844 | } |
| 1845 | |
| 1846 | // Is this entry for a sub-region of a changed region? |
| 1847 | const MemRegion *Super = MR; |
| 1848 | while (const SubRegion *SR = dyn_cast<SubRegion>(Super)) { |
| 1849 | Super = SR->getSuperRegion(); |
| 1850 | if (Invalidated.count(Super)) { |
Ted Kremenek | 3baf672 | 2010-11-24 00:54:37 +0000 | [diff] [blame] | 1851 | Entries = F.remove(Entries, MR); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1852 | break; |
| 1853 | } |
| 1854 | } |
| 1855 | } |
| 1856 | |
| 1857 | return state->set<CStringLength>(Entries); |
| 1858 | } |
| 1859 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1860 | void CStringChecker::checkLiveSymbols(ProgramStateRef state, |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1861 | SymbolReaper &SR) const { |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1862 | // Mark all symbols in our string length map as valid. |
| 1863 | CStringLength::EntryMap Entries = state->get<CStringLength>(); |
| 1864 | |
| 1865 | for (CStringLength::EntryMap::iterator I = Entries.begin(), E = Entries.end(); |
| 1866 | I != E; ++I) { |
| 1867 | SVal Len = I.getData(); |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1868 | |
Anna Zaks | 1d1d515 | 2011-12-06 23:12:33 +0000 | [diff] [blame] | 1869 | for (SymExpr::symbol_iterator si = Len.symbol_begin(), |
| 1870 | se = Len.symbol_end(); si != se; ++si) |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 1871 | SR.markInUse(*si); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1872 | } |
| 1873 | } |
| 1874 | |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1875 | void CStringChecker::checkDeadSymbols(SymbolReaper &SR, |
| 1876 | CheckerContext &C) const { |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1877 | if (!SR.hasDeadSymbols()) |
| 1878 | return; |
| 1879 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1880 | ProgramStateRef state = C.getState(); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1881 | CStringLength::EntryMap Entries = state->get<CStringLength>(); |
| 1882 | if (Entries.isEmpty()) |
| 1883 | return; |
| 1884 | |
| 1885 | CStringLength::EntryMap::Factory &F = state->get_context<CStringLength>(); |
| 1886 | for (CStringLength::EntryMap::iterator I = Entries.begin(), E = Entries.end(); |
| 1887 | I != E; ++I) { |
| 1888 | SVal Len = I.getData(); |
| 1889 | if (SymbolRef Sym = Len.getAsSymbol()) { |
| 1890 | if (SR.isDead(Sym)) |
Ted Kremenek | 3baf672 | 2010-11-24 00:54:37 +0000 | [diff] [blame] | 1891 | Entries = F.remove(Entries, I.getKey()); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1892 | } |
| 1893 | } |
| 1894 | |
| 1895 | state = state->set<CStringLength>(Entries); |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 1896 | C.addTransition(state); |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 1897 | } |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1898 | |
Anna Zaks | 5730076 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 1899 | #define REGISTER_CHECKER(name) \ |
| 1900 | void ento::register##name(CheckerManager &mgr) {\ |
| 1901 | static CStringChecker *TheChecker = 0; \ |
| 1902 | if (TheChecker == 0) \ |
| 1903 | TheChecker = mgr.registerChecker<CStringChecker>(); \ |
| 1904 | TheChecker->Filter.Check##name = true; \ |
Argyrios Kyrtzidis | 183ff98 | 2011-02-24 01:05:30 +0000 | [diff] [blame] | 1905 | } |
Anna Zaks | 5730076 | 2012-02-07 00:56:14 +0000 | [diff] [blame] | 1906 | |
| 1907 | REGISTER_CHECKER(CStringNullArg) |
| 1908 | REGISTER_CHECKER(CStringOutOfBounds) |
| 1909 | REGISTER_CHECKER(CStringBufferOverlap) |
| 1910 | REGISTER_CHECKER(CStringNotNullTerm) |