1 //= CStringChecker.cpp - Checks calls to C string functions --------*- C++ -*-//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This defines CStringChecker, which is an assortment of checks on calls
10 // to functions in <string.h>.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "InterCheckerAPI.h"
15 #include "clang/Basic/CharInfo.h"
16 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
17 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
18 #include "clang/StaticAnalyzer/Core/Checker.h"
19 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h"
21 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
22 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
23 #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicExtent.h"
24 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
25 #include "llvm/ADT/STLExtras.h"
26 #include "llvm/ADT/SmallString.h"
27 #include "llvm/ADT/StringExtras.h"
28 #include "llvm/Support/raw_ostream.h"
29 
30 using namespace clang;
31 using namespace ento;
32 
33 namespace {
34 struct AnyArgExpr {
35   // FIXME: Remove constructor in C++17 to turn it into an aggregate.
36   AnyArgExpr(const Expr *Expression, unsigned ArgumentIndex)
37       : Expression{Expression}, ArgumentIndex{ArgumentIndex} {}
38   const Expr *Expression;
39   unsigned ArgumentIndex;
40 };
41 
42 struct SourceArgExpr : AnyArgExpr {
43   using AnyArgExpr::AnyArgExpr; // FIXME: Remove using in C++17.
44 };
45 
46 struct DestinationArgExpr : AnyArgExpr {
47   using AnyArgExpr::AnyArgExpr; // FIXME: Same.
48 };
49 
50 struct SizeArgExpr : AnyArgExpr {
51   using AnyArgExpr::AnyArgExpr; // FIXME: Same.
52 };
53 
54 using ErrorMessage = SmallString<128>;
55 enum class AccessKind { write, read };
56 
57 static ErrorMessage createOutOfBoundErrorMsg(StringRef FunctionDescription,
58                                              AccessKind Access) {
59   ErrorMessage Message;
60   llvm::raw_svector_ostream Os(Message);
61 
62   // Function classification like: Memory copy function
63   Os << toUppercase(FunctionDescription.front())
64      << &FunctionDescription.data()[1];
65 
66   if (Access == AccessKind::write) {
67     Os << " overflows the destination buffer";
68   } else { // read access
69     Os << " accesses out-of-bound array element";
70   }
71 
72   return Message;
73 }
74 
75 enum class ConcatFnKind { none = 0, strcat = 1, strlcat = 2 };
76 class CStringChecker : public Checker< eval::Call,
77                                          check::PreStmt<DeclStmt>,
78                                          check::LiveSymbols,
79                                          check::DeadSymbols,
80                                          check::RegionChanges
81                                          > {
82   mutable std::unique_ptr<BugType> BT_Null, BT_Bounds, BT_Overlap,
83       BT_NotCString, BT_AdditionOverflow;
84 
85   mutable const char *CurrentFunctionDescription;
86 
87 public:
88   /// The filter is used to filter out the diagnostics which are not enabled by
89   /// the user.
90   struct CStringChecksFilter {
91     DefaultBool CheckCStringNullArg;
92     DefaultBool CheckCStringOutOfBounds;
93     DefaultBool CheckCStringBufferOverlap;
94     DefaultBool CheckCStringNotNullTerm;
95 
96     CheckerNameRef CheckNameCStringNullArg;
97     CheckerNameRef CheckNameCStringOutOfBounds;
98     CheckerNameRef CheckNameCStringBufferOverlap;
99     CheckerNameRef CheckNameCStringNotNullTerm;
100   };
101 
102   CStringChecksFilter Filter;
103 
104   static void *getTag() { static int tag; return &tag; }
105 
106   bool evalCall(const CallEvent &Call, CheckerContext &C) const;
107   void checkPreStmt(const DeclStmt *DS, CheckerContext &C) const;
108   void checkLiveSymbols(ProgramStateRef state, SymbolReaper &SR) const;
109   void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const;
110 
111   ProgramStateRef
112     checkRegionChanges(ProgramStateRef state,
113                        const InvalidatedSymbols *,
114                        ArrayRef<const MemRegion *> ExplicitRegions,
115                        ArrayRef<const MemRegion *> Regions,
116                        const LocationContext *LCtx,
117                        const CallEvent *Call) const;
118 
119   typedef void (CStringChecker::*FnCheck)(CheckerContext &,
120                                           const CallExpr *) const;
121   CallDescriptionMap<FnCheck> Callbacks = {
122       {{CDF_MaybeBuiltin, "memcpy", 3}, &CStringChecker::evalMemcpy},
123       {{CDF_MaybeBuiltin, "mempcpy", 3}, &CStringChecker::evalMempcpy},
124       {{CDF_MaybeBuiltin, "memcmp", 3}, &CStringChecker::evalMemcmp},
125       {{CDF_MaybeBuiltin, "memmove", 3}, &CStringChecker::evalMemmove},
126       {{CDF_MaybeBuiltin, "memset", 3}, &CStringChecker::evalMemset},
127       {{CDF_MaybeBuiltin, "explicit_memset", 3}, &CStringChecker::evalMemset},
128       {{CDF_MaybeBuiltin, "strcpy", 2}, &CStringChecker::evalStrcpy},
129       {{CDF_MaybeBuiltin, "strncpy", 3}, &CStringChecker::evalStrncpy},
130       {{CDF_MaybeBuiltin, "stpcpy", 2}, &CStringChecker::evalStpcpy},
131       {{CDF_MaybeBuiltin, "strlcpy", 3}, &CStringChecker::evalStrlcpy},
132       {{CDF_MaybeBuiltin, "strcat", 2}, &CStringChecker::evalStrcat},
133       {{CDF_MaybeBuiltin, "strncat", 3}, &CStringChecker::evalStrncat},
134       {{CDF_MaybeBuiltin, "strlcat", 3}, &CStringChecker::evalStrlcat},
135       {{CDF_MaybeBuiltin, "strlen", 1}, &CStringChecker::evalstrLength},
136       {{CDF_MaybeBuiltin, "strnlen", 2}, &CStringChecker::evalstrnLength},
137       {{CDF_MaybeBuiltin, "strcmp", 2}, &CStringChecker::evalStrcmp},
138       {{CDF_MaybeBuiltin, "strncmp", 3}, &CStringChecker::evalStrncmp},
139       {{CDF_MaybeBuiltin, "strcasecmp", 2}, &CStringChecker::evalStrcasecmp},
140       {{CDF_MaybeBuiltin, "strncasecmp", 3}, &CStringChecker::evalStrncasecmp},
141       {{CDF_MaybeBuiltin, "strsep", 2}, &CStringChecker::evalStrsep},
142       {{CDF_MaybeBuiltin, "bcopy", 3}, &CStringChecker::evalBcopy},
143       {{CDF_MaybeBuiltin, "bcmp", 3}, &CStringChecker::evalMemcmp},
144       {{CDF_MaybeBuiltin, "bzero", 2}, &CStringChecker::evalBzero},
145       {{CDF_MaybeBuiltin, "explicit_bzero", 2}, &CStringChecker::evalBzero},
146   };
147 
148   // These require a bit of special handling.
149   CallDescription StdCopy{{"std", "copy"}, 3},
150       StdCopyBackward{{"std", "copy_backward"}, 3};
151 
152   FnCheck identifyCall(const CallEvent &Call, CheckerContext &C) const;
153   void evalMemcpy(CheckerContext &C, const CallExpr *CE) const;
154   void evalMempcpy(CheckerContext &C, const CallExpr *CE) const;
155   void evalMemmove(CheckerContext &C, const CallExpr *CE) const;
156   void evalBcopy(CheckerContext &C, const CallExpr *CE) const;
157   void evalCopyCommon(CheckerContext &C, const CallExpr *CE,
158                       ProgramStateRef state, SizeArgExpr Size,
159                       DestinationArgExpr Dest, SourceArgExpr Source,
160                       bool Restricted, bool IsMempcpy) const;
161 
162   void evalMemcmp(CheckerContext &C, const CallExpr *CE) const;
163 
164   void evalstrLength(CheckerContext &C, const CallExpr *CE) const;
165   void evalstrnLength(CheckerContext &C, const CallExpr *CE) const;
166   void evalstrLengthCommon(CheckerContext &C,
167                            const CallExpr *CE,
168                            bool IsStrnlen = false) const;
169 
170   void evalStrcpy(CheckerContext &C, const CallExpr *CE) const;
171   void evalStrncpy(CheckerContext &C, const CallExpr *CE) const;
172   void evalStpcpy(CheckerContext &C, const CallExpr *CE) const;
173   void evalStrlcpy(CheckerContext &C, const CallExpr *CE) const;
174   void evalStrcpyCommon(CheckerContext &C, const CallExpr *CE, bool ReturnEnd,
175                         bool IsBounded, ConcatFnKind appendK,
176                         bool returnPtr = true) const;
177 
178   void evalStrcat(CheckerContext &C, const CallExpr *CE) const;
179   void evalStrncat(CheckerContext &C, const CallExpr *CE) const;
180   void evalStrlcat(CheckerContext &C, const CallExpr *CE) const;
181 
182   void evalStrcmp(CheckerContext &C, const CallExpr *CE) const;
183   void evalStrncmp(CheckerContext &C, const CallExpr *CE) const;
184   void evalStrcasecmp(CheckerContext &C, const CallExpr *CE) const;
185   void evalStrncasecmp(CheckerContext &C, const CallExpr *CE) const;
186   void evalStrcmpCommon(CheckerContext &C,
187                         const CallExpr *CE,
188                         bool IsBounded = false,
189                         bool IgnoreCase = false) const;
190 
191   void evalStrsep(CheckerContext &C, const CallExpr *CE) const;
192 
193   void evalStdCopy(CheckerContext &C, const CallExpr *CE) const;
194   void evalStdCopyBackward(CheckerContext &C, const CallExpr *CE) const;
195   void evalStdCopyCommon(CheckerContext &C, const CallExpr *CE) const;
196   void evalMemset(CheckerContext &C, const CallExpr *CE) const;
197   void evalBzero(CheckerContext &C, const CallExpr *CE) const;
198 
199   // Utility methods
200   std::pair<ProgramStateRef , ProgramStateRef >
201   static assumeZero(CheckerContext &C,
202                     ProgramStateRef state, SVal V, QualType Ty);
203 
204   static ProgramStateRef setCStringLength(ProgramStateRef state,
205                                               const MemRegion *MR,
206                                               SVal strLength);
207   static SVal getCStringLengthForRegion(CheckerContext &C,
208                                         ProgramStateRef &state,
209                                         const Expr *Ex,
210                                         const MemRegion *MR,
211                                         bool hypothetical);
212   SVal getCStringLength(CheckerContext &C,
213                         ProgramStateRef &state,
214                         const Expr *Ex,
215                         SVal Buf,
216                         bool hypothetical = false) const;
217 
218   const StringLiteral *getCStringLiteral(CheckerContext &C,
219                                          ProgramStateRef &state,
220                                          const Expr *expr,
221                                          SVal val) const;
222 
223   static ProgramStateRef InvalidateBuffer(CheckerContext &C,
224                                           ProgramStateRef state,
225                                           const Expr *Ex, SVal V,
226                                           bool IsSourceBuffer,
227                                           const Expr *Size);
228 
229   static bool SummarizeRegion(raw_ostream &os, ASTContext &Ctx,
230                               const MemRegion *MR);
231 
232   static bool memsetAux(const Expr *DstBuffer, SVal CharE,
233                         const Expr *Size, CheckerContext &C,
234                         ProgramStateRef &State);
235 
236   // Re-usable checks
237   ProgramStateRef checkNonNull(CheckerContext &C, ProgramStateRef State,
238                                AnyArgExpr Arg, SVal l) const;
239   ProgramStateRef CheckLocation(CheckerContext &C, ProgramStateRef state,
240                                 AnyArgExpr Buffer, SVal Element,
241                                 AccessKind Access) const;
242   ProgramStateRef CheckBufferAccess(CheckerContext &C, ProgramStateRef State,
243                                     AnyArgExpr Buffer, SizeArgExpr Size,
244                                     AccessKind Access) const;
245   ProgramStateRef CheckOverlap(CheckerContext &C, ProgramStateRef state,
246                                SizeArgExpr Size, AnyArgExpr First,
247                                AnyArgExpr Second) const;
248   void emitOverlapBug(CheckerContext &C,
249                       ProgramStateRef state,
250                       const Stmt *First,
251                       const Stmt *Second) const;
252 
253   void emitNullArgBug(CheckerContext &C, ProgramStateRef State, const Stmt *S,
254                       StringRef WarningMsg) const;
255   void emitOutOfBoundsBug(CheckerContext &C, ProgramStateRef State,
256                           const Stmt *S, StringRef WarningMsg) const;
257   void emitNotCStringBug(CheckerContext &C, ProgramStateRef State,
258                          const Stmt *S, StringRef WarningMsg) const;
259   void emitAdditionOverflowBug(CheckerContext &C, ProgramStateRef State) const;
260   ProgramStateRef checkAdditionOverflow(CheckerContext &C,
261                                             ProgramStateRef state,
262                                             NonLoc left,
263                                             NonLoc right) const;
264 
265   // Return true if the destination buffer of the copy function may be in bound.
266   // Expects SVal of Size to be positive and unsigned.
267   // Expects SVal of FirstBuf to be a FieldRegion.
268   static bool IsFirstBufInBound(CheckerContext &C,
269                                 ProgramStateRef state,
270                                 const Expr *FirstBuf,
271                                 const Expr *Size);
272 };
273 
274 } //end anonymous namespace
275 
276 REGISTER_MAP_WITH_PROGRAMSTATE(CStringLength, const MemRegion *, SVal)
277 
278 //===----------------------------------------------------------------------===//
279 // Individual checks and utility methods.
280 //===----------------------------------------------------------------------===//
281 
282 std::pair<ProgramStateRef , ProgramStateRef >
283 CStringChecker::assumeZero(CheckerContext &C, ProgramStateRef state, SVal V,
284                            QualType Ty) {
285   Optional<DefinedSVal> val = V.getAs<DefinedSVal>();
286   if (!val)
287     return std::pair<ProgramStateRef , ProgramStateRef >(state, state);
288 
289   SValBuilder &svalBuilder = C.getSValBuilder();
290   DefinedOrUnknownSVal zero = svalBuilder.makeZeroVal(Ty);
291   return state->assume(svalBuilder.evalEQ(state, *val, zero));
292 }
293 
294 ProgramStateRef CStringChecker::checkNonNull(CheckerContext &C,
295                                              ProgramStateRef State,
296                                              AnyArgExpr Arg, SVal l) const {
297   // If a previous check has failed, propagate the failure.
298   if (!State)
299     return nullptr;
300 
301   ProgramStateRef stateNull, stateNonNull;
302   std::tie(stateNull, stateNonNull) =
303       assumeZero(C, State, l, Arg.Expression->getType());
304 
305   if (stateNull && !stateNonNull) {
306     if (Filter.CheckCStringNullArg) {
307       SmallString<80> buf;
308       llvm::raw_svector_ostream OS(buf);
309       assert(CurrentFunctionDescription);
310       OS << "Null pointer passed as " << (Arg.ArgumentIndex + 1)
311          << llvm::getOrdinalSuffix(Arg.ArgumentIndex + 1) << " argument to "
312          << CurrentFunctionDescription;
313 
314       emitNullArgBug(C, stateNull, Arg.Expression, OS.str());
315     }
316     return nullptr;
317   }
318 
319   // From here on, assume that the value is non-null.
320   assert(stateNonNull);
321   return stateNonNull;
322 }
323 
324 // FIXME: This was originally copied from ArrayBoundChecker.cpp. Refactor?
325 ProgramStateRef CStringChecker::CheckLocation(CheckerContext &C,
326                                               ProgramStateRef state,
327                                               AnyArgExpr Buffer, SVal Element,
328                                               AccessKind Access) const {
329 
330   // If a previous check has failed, propagate the failure.
331   if (!state)
332     return nullptr;
333 
334   // Check for out of bound array element access.
335   const MemRegion *R = Element.getAsRegion();
336   if (!R)
337     return state;
338 
339   const auto *ER = dyn_cast<ElementRegion>(R);
340   if (!ER)
341     return state;
342 
343   if (ER->getValueType() != C.getASTContext().CharTy)
344     return state;
345 
346   // Get the size of the array.
347   const auto *superReg = cast<SubRegion>(ER->getSuperRegion());
348   DefinedOrUnknownSVal Size =
349       getDynamicExtent(state, superReg, C.getSValBuilder());
350 
351   // Get the index of the accessed element.
352   DefinedOrUnknownSVal Idx = ER->getIndex().castAs<DefinedOrUnknownSVal>();
353 
354   ProgramStateRef StInBound = state->assumeInBound(Idx, Size, true);
355   ProgramStateRef StOutBound = state->assumeInBound(Idx, Size, false);
356   if (StOutBound && !StInBound) {
357     // These checks are either enabled by the CString out-of-bounds checker
358     // explicitly or implicitly by the Malloc checker.
359     // In the latter case we only do modeling but do not emit warning.
360     if (!Filter.CheckCStringOutOfBounds)
361       return nullptr;
362 
363     // Emit a bug report.
364     ErrorMessage Message =
365         createOutOfBoundErrorMsg(CurrentFunctionDescription, Access);
366     emitOutOfBoundsBug(C, StOutBound, Buffer.Expression, Message);
367     return nullptr;
368   }
369 
370   // Array bound check succeeded.  From this point forward the array bound
371   // should always succeed.
372   return StInBound;
373 }
374 
375 ProgramStateRef CStringChecker::CheckBufferAccess(CheckerContext &C,
376                                                   ProgramStateRef State,
377                                                   AnyArgExpr Buffer,
378                                                   SizeArgExpr Size,
379                                                   AccessKind Access) const {
380   // If a previous check has failed, propagate the failure.
381   if (!State)
382     return nullptr;
383 
384   SValBuilder &svalBuilder = C.getSValBuilder();
385   ASTContext &Ctx = svalBuilder.getContext();
386 
387   QualType SizeTy = Size.Expression->getType();
388   QualType PtrTy = Ctx.getPointerType(Ctx.CharTy);
389 
390   // Check that the first buffer is non-null.
391   SVal BufVal = C.getSVal(Buffer.Expression);
392   State = checkNonNull(C, State, Buffer, BufVal);
393   if (!State)
394     return nullptr;
395 
396   // If out-of-bounds checking is turned off, skip the rest.
397   if (!Filter.CheckCStringOutOfBounds)
398     return State;
399 
400   // Get the access length and make sure it is known.
401   // FIXME: This assumes the caller has already checked that the access length
402   // is positive. And that it's unsigned.
403   SVal LengthVal = C.getSVal(Size.Expression);
404   Optional<NonLoc> Length = LengthVal.getAs<NonLoc>();
405   if (!Length)
406     return State;
407 
408   // Compute the offset of the last element to be accessed: size-1.
409   NonLoc One = svalBuilder.makeIntVal(1, SizeTy).castAs<NonLoc>();
410   SVal Offset = svalBuilder.evalBinOpNN(State, BO_Sub, *Length, One, SizeTy);
411   if (Offset.isUnknown())
412     return nullptr;
413   NonLoc LastOffset = Offset.castAs<NonLoc>();
414 
415   // Check that the first buffer is sufficiently long.
416   SVal BufStart =
417       svalBuilder.evalCast(BufVal, PtrTy, Buffer.Expression->getType());
418   if (Optional<Loc> BufLoc = BufStart.getAs<Loc>()) {
419 
420     SVal BufEnd =
421         svalBuilder.evalBinOpLN(State, BO_Add, *BufLoc, LastOffset, PtrTy);
422     State = CheckLocation(C, State, Buffer, BufEnd, Access);
423 
424     // If the buffer isn't large enough, abort.
425     if (!State)
426       return nullptr;
427   }
428 
429   // Large enough or not, return this state!
430   return State;
431 }
432 
433 ProgramStateRef CStringChecker::CheckOverlap(CheckerContext &C,
434                                              ProgramStateRef state,
435                                              SizeArgExpr Size, AnyArgExpr First,
436                                              AnyArgExpr Second) const {
437   if (!Filter.CheckCStringBufferOverlap)
438     return state;
439 
440   // Do a simple check for overlap: if the two arguments are from the same
441   // buffer, see if the end of the first is greater than the start of the second
442   // or vice versa.
443 
444   // If a previous check has failed, propagate the failure.
445   if (!state)
446     return nullptr;
447 
448   ProgramStateRef stateTrue, stateFalse;
449 
450   // Get the buffer values and make sure they're known locations.
451   const LocationContext *LCtx = C.getLocationContext();
452   SVal firstVal = state->getSVal(First.Expression, LCtx);
453   SVal secondVal = state->getSVal(Second.Expression, LCtx);
454 
455   Optional<Loc> firstLoc = firstVal.getAs<Loc>();
456   if (!firstLoc)
457     return state;
458 
459   Optional<Loc> secondLoc = secondVal.getAs<Loc>();
460   if (!secondLoc)
461     return state;
462 
463   // Are the two values the same?
464   SValBuilder &svalBuilder = C.getSValBuilder();
465   std::tie(stateTrue, stateFalse) =
466       state->assume(svalBuilder.evalEQ(state, *firstLoc, *secondLoc));
467 
468   if (stateTrue && !stateFalse) {
469     // If the values are known to be equal, that's automatically an overlap.
470     emitOverlapBug(C, stateTrue, First.Expression, Second.Expression);
471     return nullptr;
472   }
473 
474   // assume the two expressions are not equal.
475   assert(stateFalse);
476   state = stateFalse;
477 
478   // Which value comes first?
479   QualType cmpTy = svalBuilder.getConditionType();
480   SVal reverse =
481       svalBuilder.evalBinOpLL(state, BO_GT, *firstLoc, *secondLoc, cmpTy);
482   Optional<DefinedOrUnknownSVal> reverseTest =
483       reverse.getAs<DefinedOrUnknownSVal>();
484   if (!reverseTest)
485     return state;
486 
487   std::tie(stateTrue, stateFalse) = state->assume(*reverseTest);
488   if (stateTrue) {
489     if (stateFalse) {
490       // If we don't know which one comes first, we can't perform this test.
491       return state;
492     } else {
493       // Switch the values so that firstVal is before secondVal.
494       std::swap(firstLoc, secondLoc);
495 
496       // Switch the Exprs as well, so that they still correspond.
497       std::swap(First, Second);
498     }
499   }
500 
501   // Get the length, and make sure it too is known.
502   SVal LengthVal = state->getSVal(Size.Expression, LCtx);
503   Optional<NonLoc> Length = LengthVal.getAs<NonLoc>();
504   if (!Length)
505     return state;
506 
507   // Convert the first buffer's start address to char*.
508   // Bail out if the cast fails.
509   ASTContext &Ctx = svalBuilder.getContext();
510   QualType CharPtrTy = Ctx.getPointerType(Ctx.CharTy);
511   SVal FirstStart =
512       svalBuilder.evalCast(*firstLoc, CharPtrTy, First.Expression->getType());
513   Optional<Loc> FirstStartLoc = FirstStart.getAs<Loc>();
514   if (!FirstStartLoc)
515     return state;
516 
517   // Compute the end of the first buffer. Bail out if THAT fails.
518   SVal FirstEnd = svalBuilder.evalBinOpLN(state, BO_Add, *FirstStartLoc,
519                                           *Length, CharPtrTy);
520   Optional<Loc> FirstEndLoc = FirstEnd.getAs<Loc>();
521   if (!FirstEndLoc)
522     return state;
523 
524   // Is the end of the first buffer past the start of the second buffer?
525   SVal Overlap =
526       svalBuilder.evalBinOpLL(state, BO_GT, *FirstEndLoc, *secondLoc, cmpTy);
527   Optional<DefinedOrUnknownSVal> OverlapTest =
528       Overlap.getAs<DefinedOrUnknownSVal>();
529   if (!OverlapTest)
530     return state;
531 
532   std::tie(stateTrue, stateFalse) = state->assume(*OverlapTest);
533 
534   if (stateTrue && !stateFalse) {
535     // Overlap!
536     emitOverlapBug(C, stateTrue, First.Expression, Second.Expression);
537     return nullptr;
538   }
539 
540   // assume the two expressions don't overlap.
541   assert(stateFalse);
542   return stateFalse;
543 }
544 
545 void CStringChecker::emitOverlapBug(CheckerContext &C, ProgramStateRef state,
546                                   const Stmt *First, const Stmt *Second) const {
547   ExplodedNode *N = C.generateErrorNode(state);
548   if (!N)
549     return;
550 
551   if (!BT_Overlap)
552     BT_Overlap.reset(new BugType(Filter.CheckNameCStringBufferOverlap,
553                                  categories::UnixAPI, "Improper arguments"));
554 
555   // Generate a report for this bug.
556   auto report = std::make_unique<PathSensitiveBugReport>(
557       *BT_Overlap, "Arguments must not be overlapping buffers", N);
558   report->addRange(First->getSourceRange());
559   report->addRange(Second->getSourceRange());
560 
561   C.emitReport(std::move(report));
562 }
563 
564 void CStringChecker::emitNullArgBug(CheckerContext &C, ProgramStateRef State,
565                                     const Stmt *S, StringRef WarningMsg) const {
566   if (ExplodedNode *N = C.generateErrorNode(State)) {
567     if (!BT_Null)
568       BT_Null.reset(new BuiltinBug(
569           Filter.CheckNameCStringNullArg, categories::UnixAPI,
570           "Null pointer argument in call to byte string function"));
571 
572     BuiltinBug *BT = static_cast<BuiltinBug *>(BT_Null.get());
573     auto Report = std::make_unique<PathSensitiveBugReport>(*BT, WarningMsg, N);
574     Report->addRange(S->getSourceRange());
575     if (const auto *Ex = dyn_cast<Expr>(S))
576       bugreporter::trackExpressionValue(N, Ex, *Report);
577     C.emitReport(std::move(Report));
578   }
579 }
580 
581 void CStringChecker::emitOutOfBoundsBug(CheckerContext &C,
582                                         ProgramStateRef State, const Stmt *S,
583                                         StringRef WarningMsg) const {
584   if (ExplodedNode *N = C.generateErrorNode(State)) {
585     if (!BT_Bounds)
586       BT_Bounds.reset(new BuiltinBug(
587           Filter.CheckCStringOutOfBounds ? Filter.CheckNameCStringOutOfBounds
588                                          : Filter.CheckNameCStringNullArg,
589           "Out-of-bound array access",
590           "Byte string function accesses out-of-bound array element"));
591 
592     BuiltinBug *BT = static_cast<BuiltinBug *>(BT_Bounds.get());
593 
594     // FIXME: It would be nice to eventually make this diagnostic more clear,
595     // e.g., by referencing the original declaration or by saying *why* this
596     // reference is outside the range.
597     auto Report = std::make_unique<PathSensitiveBugReport>(*BT, WarningMsg, N);
598     Report->addRange(S->getSourceRange());
599     C.emitReport(std::move(Report));
600   }
601 }
602 
603 void CStringChecker::emitNotCStringBug(CheckerContext &C, ProgramStateRef State,
604                                        const Stmt *S,
605                                        StringRef WarningMsg) const {
606   if (ExplodedNode *N = C.generateNonFatalErrorNode(State)) {
607     if (!BT_NotCString)
608       BT_NotCString.reset(new BuiltinBug(
609           Filter.CheckNameCStringNotNullTerm, categories::UnixAPI,
610           "Argument is not a null-terminated string."));
611 
612     auto Report =
613         std::make_unique<PathSensitiveBugReport>(*BT_NotCString, WarningMsg, N);
614 
615     Report->addRange(S->getSourceRange());
616     C.emitReport(std::move(Report));
617   }
618 }
619 
620 void CStringChecker::emitAdditionOverflowBug(CheckerContext &C,
621                                              ProgramStateRef State) const {
622   if (ExplodedNode *N = C.generateErrorNode(State)) {
623     if (!BT_AdditionOverflow)
624       BT_AdditionOverflow.reset(
625           new BuiltinBug(Filter.CheckNameCStringOutOfBounds, "API",
626                          "Sum of expressions causes overflow."));
627 
628     // This isn't a great error message, but this should never occur in real
629     // code anyway -- you'd have to create a buffer longer than a size_t can
630     // represent, which is sort of a contradiction.
631     const char *WarningMsg =
632         "This expression will create a string whose length is too big to "
633         "be represented as a size_t";
634 
635     auto Report = std::make_unique<PathSensitiveBugReport>(*BT_AdditionOverflow,
636                                                            WarningMsg, N);
637     C.emitReport(std::move(Report));
638   }
639 }
640 
641 ProgramStateRef CStringChecker::checkAdditionOverflow(CheckerContext &C,
642                                                      ProgramStateRef state,
643                                                      NonLoc left,
644                                                      NonLoc right) const {
645   // If out-of-bounds checking is turned off, skip the rest.
646   if (!Filter.CheckCStringOutOfBounds)
647     return state;
648 
649   // If a previous check has failed, propagate the failure.
650   if (!state)
651     return nullptr;
652 
653   SValBuilder &svalBuilder = C.getSValBuilder();
654   BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
655 
656   QualType sizeTy = svalBuilder.getContext().getSizeType();
657   const llvm::APSInt &maxValInt = BVF.getMaxValue(sizeTy);
658   NonLoc maxVal = svalBuilder.makeIntVal(maxValInt);
659 
660   SVal maxMinusRight;
661   if (right.getAs<nonloc::ConcreteInt>()) {
662     maxMinusRight = svalBuilder.evalBinOpNN(state, BO_Sub, maxVal, right,
663                                                  sizeTy);
664   } else {
665     // Try switching the operands. (The order of these two assignments is
666     // important!)
667     maxMinusRight = svalBuilder.evalBinOpNN(state, BO_Sub, maxVal, left,
668                                             sizeTy);
669     left = right;
670   }
671 
672   if (Optional<NonLoc> maxMinusRightNL = maxMinusRight.getAs<NonLoc>()) {
673     QualType cmpTy = svalBuilder.getConditionType();
674     // If left > max - right, we have an overflow.
675     SVal willOverflow = svalBuilder.evalBinOpNN(state, BO_GT, left,
676                                                 *maxMinusRightNL, cmpTy);
677 
678     ProgramStateRef stateOverflow, stateOkay;
679     std::tie(stateOverflow, stateOkay) =
680       state->assume(willOverflow.castAs<DefinedOrUnknownSVal>());
681 
682     if (stateOverflow && !stateOkay) {
683       // We have an overflow. Emit a bug report.
684       emitAdditionOverflowBug(C, stateOverflow);
685       return nullptr;
686     }
687 
688     // From now on, assume an overflow didn't occur.
689     assert(stateOkay);
690     state = stateOkay;
691   }
692 
693   return state;
694 }
695 
696 ProgramStateRef CStringChecker::setCStringLength(ProgramStateRef state,
697                                                 const MemRegion *MR,
698                                                 SVal strLength) {
699   assert(!strLength.isUndef() && "Attempt to set an undefined string length");
700 
701   MR = MR->StripCasts();
702 
703   switch (MR->getKind()) {
704   case MemRegion::StringRegionKind:
705     // FIXME: This can happen if we strcpy() into a string region. This is
706     // undefined [C99 6.4.5p6], but we should still warn about it.
707     return state;
708 
709   case MemRegion::SymbolicRegionKind:
710   case MemRegion::AllocaRegionKind:
711   case MemRegion::NonParamVarRegionKind:
712   case MemRegion::ParamVarRegionKind:
713   case MemRegion::FieldRegionKind:
714   case MemRegion::ObjCIvarRegionKind:
715     // These are the types we can currently track string lengths for.
716     break;
717 
718   case MemRegion::ElementRegionKind:
719     // FIXME: Handle element regions by upper-bounding the parent region's
720     // string length.
721     return state;
722 
723   default:
724     // Other regions (mostly non-data) can't have a reliable C string length.
725     // For now, just ignore the change.
726     // FIXME: These are rare but not impossible. We should output some kind of
727     // warning for things like strcpy((char[]){'a', 0}, "b");
728     return state;
729   }
730 
731   if (strLength.isUnknown())
732     return state->remove<CStringLength>(MR);
733 
734   return state->set<CStringLength>(MR, strLength);
735 }
736 
737 SVal CStringChecker::getCStringLengthForRegion(CheckerContext &C,
738                                                ProgramStateRef &state,
739                                                const Expr *Ex,
740                                                const MemRegion *MR,
741                                                bool hypothetical) {
742   if (!hypothetical) {
743     // If there's a recorded length, go ahead and return it.
744     const SVal *Recorded = state->get<CStringLength>(MR);
745     if (Recorded)
746       return *Recorded;
747   }
748 
749   // Otherwise, get a new symbol and update the state.
750   SValBuilder &svalBuilder = C.getSValBuilder();
751   QualType sizeTy = svalBuilder.getContext().getSizeType();
752   SVal strLength = svalBuilder.getMetadataSymbolVal(CStringChecker::getTag(),
753                                                     MR, Ex, sizeTy,
754                                                     C.getLocationContext(),
755                                                     C.blockCount());
756 
757   if (!hypothetical) {
758     if (Optional<NonLoc> strLn = strLength.getAs<NonLoc>()) {
759       // In case of unbounded calls strlen etc bound the range to SIZE_MAX/4
760       BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
761       const llvm::APSInt &maxValInt = BVF.getMaxValue(sizeTy);
762       llvm::APSInt fourInt = APSIntType(maxValInt).getValue(4);
763       const llvm::APSInt *maxLengthInt = BVF.evalAPSInt(BO_Div, maxValInt,
764                                                         fourInt);
765       NonLoc maxLength = svalBuilder.makeIntVal(*maxLengthInt);
766       SVal evalLength = svalBuilder.evalBinOpNN(state, BO_LE, *strLn,
767                                                 maxLength, sizeTy);
768       state = state->assume(evalLength.castAs<DefinedOrUnknownSVal>(), true);
769     }
770     state = state->set<CStringLength>(MR, strLength);
771   }
772 
773   return strLength;
774 }
775 
776 SVal CStringChecker::getCStringLength(CheckerContext &C, ProgramStateRef &state,
777                                       const Expr *Ex, SVal Buf,
778                                       bool hypothetical) const {
779   const MemRegion *MR = Buf.getAsRegion();
780   if (!MR) {
781     // If we can't get a region, see if it's something we /know/ isn't a
782     // C string. In the context of locations, the only time we can issue such
783     // a warning is for labels.
784     if (Optional<loc::GotoLabel> Label = Buf.getAs<loc::GotoLabel>()) {
785       if (Filter.CheckCStringNotNullTerm) {
786         SmallString<120> buf;
787         llvm::raw_svector_ostream os(buf);
788         assert(CurrentFunctionDescription);
789         os << "Argument to " << CurrentFunctionDescription
790            << " is the address of the label '" << Label->getLabel()->getName()
791            << "', which is not a null-terminated string";
792 
793         emitNotCStringBug(C, state, Ex, os.str());
794       }
795       return UndefinedVal();
796     }
797 
798     // If it's not a region and not a label, give up.
799     return UnknownVal();
800   }
801 
802   // If we have a region, strip casts from it and see if we can figure out
803   // its length. For anything we can't figure out, just return UnknownVal.
804   MR = MR->StripCasts();
805 
806   switch (MR->getKind()) {
807   case MemRegion::StringRegionKind: {
808     // Modifying the contents of string regions is undefined [C99 6.4.5p6],
809     // so we can assume that the byte length is the correct C string length.
810     SValBuilder &svalBuilder = C.getSValBuilder();
811     QualType sizeTy = svalBuilder.getContext().getSizeType();
812     const StringLiteral *strLit = cast<StringRegion>(MR)->getStringLiteral();
813     return svalBuilder.makeIntVal(strLit->getByteLength(), sizeTy);
814   }
815   case MemRegion::SymbolicRegionKind:
816   case MemRegion::AllocaRegionKind:
817   case MemRegion::NonParamVarRegionKind:
818   case MemRegion::ParamVarRegionKind:
819   case MemRegion::FieldRegionKind:
820   case MemRegion::ObjCIvarRegionKind:
821     return getCStringLengthForRegion(C, state, Ex, MR, hypothetical);
822   case MemRegion::CompoundLiteralRegionKind:
823     // FIXME: Can we track this? Is it necessary?
824     return UnknownVal();
825   case MemRegion::ElementRegionKind:
826     // FIXME: How can we handle this? It's not good enough to subtract the
827     // offset from the base string length; consider "123\x00567" and &a[5].
828     return UnknownVal();
829   default:
830     // Other regions (mostly non-data) can't have a reliable C string length.
831     // In this case, an error is emitted and UndefinedVal is returned.
832     // The caller should always be prepared to handle this case.
833     if (Filter.CheckCStringNotNullTerm) {
834       SmallString<120> buf;
835       llvm::raw_svector_ostream os(buf);
836 
837       assert(CurrentFunctionDescription);
838       os << "Argument to " << CurrentFunctionDescription << " is ";
839 
840       if (SummarizeRegion(os, C.getASTContext(), MR))
841         os << ", which is not a null-terminated string";
842       else
843         os << "not a null-terminated string";
844 
845       emitNotCStringBug(C, state, Ex, os.str());
846     }
847     return UndefinedVal();
848   }
849 }
850 
851 const StringLiteral *CStringChecker::getCStringLiteral(CheckerContext &C,
852   ProgramStateRef &state, const Expr *expr, SVal val) const {
853 
854   // Get the memory region pointed to by the val.
855   const MemRegion *bufRegion = val.getAsRegion();
856   if (!bufRegion)
857     return nullptr;
858 
859   // Strip casts off the memory region.
860   bufRegion = bufRegion->StripCasts();
861 
862   // Cast the memory region to a string region.
863   const StringRegion *strRegion= dyn_cast<StringRegion>(bufRegion);
864   if (!strRegion)
865     return nullptr;
866 
867   // Return the actual string in the string region.
868   return strRegion->getStringLiteral();
869 }
870 
871 bool CStringChecker::IsFirstBufInBound(CheckerContext &C,
872                                        ProgramStateRef state,
873                                        const Expr *FirstBuf,
874                                        const Expr *Size) {
875   // If we do not know that the buffer is long enough we return 'true'.
876   // Otherwise the parent region of this field region would also get
877   // invalidated, which would lead to warnings based on an unknown state.
878 
879   // Originally copied from CheckBufferAccess and CheckLocation.
880   SValBuilder &svalBuilder = C.getSValBuilder();
881   ASTContext &Ctx = svalBuilder.getContext();
882   const LocationContext *LCtx = C.getLocationContext();
883 
884   QualType sizeTy = Size->getType();
885   QualType PtrTy = Ctx.getPointerType(Ctx.CharTy);
886   SVal BufVal = state->getSVal(FirstBuf, LCtx);
887 
888   SVal LengthVal = state->getSVal(Size, LCtx);
889   Optional<NonLoc> Length = LengthVal.getAs<NonLoc>();
890   if (!Length)
891     return true; // cf top comment.
892 
893   // Compute the offset of the last element to be accessed: size-1.
894   NonLoc One = svalBuilder.makeIntVal(1, sizeTy).castAs<NonLoc>();
895   SVal Offset = svalBuilder.evalBinOpNN(state, BO_Sub, *Length, One, sizeTy);
896   if (Offset.isUnknown())
897     return true; // cf top comment
898   NonLoc LastOffset = Offset.castAs<NonLoc>();
899 
900   // Check that the first buffer is sufficiently long.
901   SVal BufStart = svalBuilder.evalCast(BufVal, PtrTy, FirstBuf->getType());
902   Optional<Loc> BufLoc = BufStart.getAs<Loc>();
903   if (!BufLoc)
904     return true; // cf top comment.
905 
906   SVal BufEnd =
907       svalBuilder.evalBinOpLN(state, BO_Add, *BufLoc, LastOffset, PtrTy);
908 
909   // Check for out of bound array element access.
910   const MemRegion *R = BufEnd.getAsRegion();
911   if (!R)
912     return true; // cf top comment.
913 
914   const ElementRegion *ER = dyn_cast<ElementRegion>(R);
915   if (!ER)
916     return true; // cf top comment.
917 
918   // FIXME: Does this crash when a non-standard definition
919   // of a library function is encountered?
920   assert(ER->getValueType() == C.getASTContext().CharTy &&
921          "IsFirstBufInBound should only be called with char* ElementRegions");
922 
923   // Get the size of the array.
924   const SubRegion *superReg = cast<SubRegion>(ER->getSuperRegion());
925   DefinedOrUnknownSVal SizeDV = getDynamicExtent(state, superReg, svalBuilder);
926 
927   // Get the index of the accessed element.
928   DefinedOrUnknownSVal Idx = ER->getIndex().castAs<DefinedOrUnknownSVal>();
929 
930   ProgramStateRef StInBound = state->assumeInBound(Idx, SizeDV, true);
931 
932   return static_cast<bool>(StInBound);
933 }
934 
935 ProgramStateRef CStringChecker::InvalidateBuffer(CheckerContext &C,
936                                                  ProgramStateRef state,
937                                                  const Expr *E, SVal V,
938                                                  bool IsSourceBuffer,
939                                                  const Expr *Size) {
940   Optional<Loc> L = V.getAs<Loc>();
941   if (!L)
942     return state;
943 
944   // FIXME: This is a simplified version of what's in CFRefCount.cpp -- it makes
945   // some assumptions about the value that CFRefCount can't. Even so, it should
946   // probably be refactored.
947   if (Optional<loc::MemRegionVal> MR = L->getAs<loc::MemRegionVal>()) {
948     const MemRegion *R = MR->getRegion()->StripCasts();
949 
950     // Are we dealing with an ElementRegion?  If so, we should be invalidating
951     // the super-region.
952     if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
953       R = ER->getSuperRegion();
954       // FIXME: What about layers of ElementRegions?
955     }
956 
957     // Invalidate this region.
958     const LocationContext *LCtx = C.getPredecessor()->getLocationContext();
959 
960     bool CausesPointerEscape = false;
961     RegionAndSymbolInvalidationTraits ITraits;
962     // Invalidate and escape only indirect regions accessible through the source
963     // buffer.
964     if (IsSourceBuffer) {
965       ITraits.setTrait(R->getBaseRegion(),
966                        RegionAndSymbolInvalidationTraits::TK_PreserveContents);
967       ITraits.setTrait(R, RegionAndSymbolInvalidationTraits::TK_SuppressEscape);
968       CausesPointerEscape = true;
969     } else {
970       const MemRegion::Kind& K = R->getKind();
971       if (K == MemRegion::FieldRegionKind)
972         if (Size && IsFirstBufInBound(C, state, E, Size)) {
973           // If destination buffer is a field region and access is in bound,
974           // do not invalidate its super region.
975           ITraits.setTrait(
976               R,
977               RegionAndSymbolInvalidationTraits::TK_DoNotInvalidateSuperRegion);
978         }
979     }
980 
981     return state->invalidateRegions(R, E, C.blockCount(), LCtx,
982                                     CausesPointerEscape, nullptr, nullptr,
983                                     &ITraits);
984   }
985 
986   // If we have a non-region value by chance, just remove the binding.
987   // FIXME: is this necessary or correct? This handles the non-Region
988   //  cases.  Is it ever valid to store to these?
989   return state->killBinding(*L);
990 }
991 
992 bool CStringChecker::SummarizeRegion(raw_ostream &os, ASTContext &Ctx,
993                                      const MemRegion *MR) {
994   switch (MR->getKind()) {
995   case MemRegion::FunctionCodeRegionKind: {
996     if (const auto *FD = cast<FunctionCodeRegion>(MR)->getDecl())
997       os << "the address of the function '" << *FD << '\'';
998     else
999       os << "the address of a function";
1000     return true;
1001   }
1002   case MemRegion::BlockCodeRegionKind:
1003     os << "block text";
1004     return true;
1005   case MemRegion::BlockDataRegionKind:
1006     os << "a block";
1007     return true;
1008   case MemRegion::CXXThisRegionKind:
1009   case MemRegion::CXXTempObjectRegionKind:
1010     os << "a C++ temp object of type "
1011        << cast<TypedValueRegion>(MR)->getValueType().getAsString();
1012     return true;
1013   case MemRegion::NonParamVarRegionKind:
1014     os << "a variable of type"
1015        << cast<TypedValueRegion>(MR)->getValueType().getAsString();
1016     return true;
1017   case MemRegion::ParamVarRegionKind:
1018     os << "a parameter of type"
1019        << cast<TypedValueRegion>(MR)->getValueType().getAsString();
1020     return true;
1021   case MemRegion::FieldRegionKind:
1022     os << "a field of type "
1023        << cast<TypedValueRegion>(MR)->getValueType().getAsString();
1024     return true;
1025   case MemRegion::ObjCIvarRegionKind:
1026     os << "an instance variable of type "
1027        << cast<TypedValueRegion>(MR)->getValueType().getAsString();
1028     return true;
1029   default:
1030     return false;
1031   }
1032 }
1033 
1034 bool CStringChecker::memsetAux(const Expr *DstBuffer, SVal CharVal,
1035                                const Expr *Size, CheckerContext &C,
1036                                ProgramStateRef &State) {
1037   SVal MemVal = C.getSVal(DstBuffer);
1038   SVal SizeVal = C.getSVal(Size);
1039   const MemRegion *MR = MemVal.getAsRegion();
1040   if (!MR)
1041     return false;
1042 
1043   // We're about to model memset by producing a "default binding" in the Store.
1044   // Our current implementation - RegionStore - doesn't support default bindings
1045   // that don't cover the whole base region. So we should first get the offset
1046   // and the base region to figure out whether the offset of buffer is 0.
1047   RegionOffset Offset = MR->getAsOffset();
1048   const MemRegion *BR = Offset.getRegion();
1049 
1050   Optional<NonLoc> SizeNL = SizeVal.getAs<NonLoc>();
1051   if (!SizeNL)
1052     return false;
1053 
1054   SValBuilder &svalBuilder = C.getSValBuilder();
1055   ASTContext &Ctx = C.getASTContext();
1056 
1057   // void *memset(void *dest, int ch, size_t count);
1058   // For now we can only handle the case of offset is 0 and concrete char value.
1059   if (Offset.isValid() && !Offset.hasSymbolicOffset() &&
1060       Offset.getOffset() == 0) {
1061     // Get the base region's size.
1062     DefinedOrUnknownSVal SizeDV = getDynamicExtent(State, BR, svalBuilder);
1063 
1064     ProgramStateRef StateWholeReg, StateNotWholeReg;
1065     std::tie(StateWholeReg, StateNotWholeReg) =
1066         State->assume(svalBuilder.evalEQ(State, SizeDV, *SizeNL));
1067 
1068     // With the semantic of 'memset()', we should convert the CharVal to
1069     // unsigned char.
1070     CharVal = svalBuilder.evalCast(CharVal, Ctx.UnsignedCharTy, Ctx.IntTy);
1071 
1072     ProgramStateRef StateNullChar, StateNonNullChar;
1073     std::tie(StateNullChar, StateNonNullChar) =
1074         assumeZero(C, State, CharVal, Ctx.UnsignedCharTy);
1075 
1076     if (StateWholeReg && !StateNotWholeReg && StateNullChar &&
1077         !StateNonNullChar) {
1078       // If the 'memset()' acts on the whole region of destination buffer and
1079       // the value of the second argument of 'memset()' is zero, bind the second
1080       // argument's value to the destination buffer with 'default binding'.
1081       // FIXME: Since there is no perfect way to bind the non-zero character, we
1082       // can only deal with zero value here. In the future, we need to deal with
1083       // the binding of non-zero value in the case of whole region.
1084       State = State->bindDefaultZero(svalBuilder.makeLoc(BR),
1085                                      C.getLocationContext());
1086     } else {
1087       // If the destination buffer's extent is not equal to the value of
1088       // third argument, just invalidate buffer.
1089       State = InvalidateBuffer(C, State, DstBuffer, MemVal,
1090                                /*IsSourceBuffer*/ false, Size);
1091     }
1092 
1093     if (StateNullChar && !StateNonNullChar) {
1094       // If the value of the second argument of 'memset()' is zero, set the
1095       // string length of destination buffer to 0 directly.
1096       State = setCStringLength(State, MR,
1097                                svalBuilder.makeZeroVal(Ctx.getSizeType()));
1098     } else if (!StateNullChar && StateNonNullChar) {
1099       SVal NewStrLen = svalBuilder.getMetadataSymbolVal(
1100           CStringChecker::getTag(), MR, DstBuffer, Ctx.getSizeType(),
1101           C.getLocationContext(), C.blockCount());
1102 
1103       // If the value of second argument is not zero, then the string length
1104       // is at least the size argument.
1105       SVal NewStrLenGESize = svalBuilder.evalBinOp(
1106           State, BO_GE, NewStrLen, SizeVal, svalBuilder.getConditionType());
1107 
1108       State = setCStringLength(
1109           State->assume(NewStrLenGESize.castAs<DefinedOrUnknownSVal>(), true),
1110           MR, NewStrLen);
1111     }
1112   } else {
1113     // If the offset is not zero and char value is not concrete, we can do
1114     // nothing but invalidate the buffer.
1115     State = InvalidateBuffer(C, State, DstBuffer, MemVal,
1116                              /*IsSourceBuffer*/ false, Size);
1117   }
1118   return true;
1119 }
1120 
1121 //===----------------------------------------------------------------------===//
1122 // evaluation of individual function calls.
1123 //===----------------------------------------------------------------------===//
1124 
1125 void CStringChecker::evalCopyCommon(CheckerContext &C, const CallExpr *CE,
1126                                     ProgramStateRef state, SizeArgExpr Size,
1127                                     DestinationArgExpr Dest,
1128                                     SourceArgExpr Source, bool Restricted,
1129                                     bool IsMempcpy) const {
1130   CurrentFunctionDescription = "memory copy function";
1131 
1132   // See if the size argument is zero.
1133   const LocationContext *LCtx = C.getLocationContext();
1134   SVal sizeVal = state->getSVal(Size.Expression, LCtx);
1135   QualType sizeTy = Size.Expression->getType();
1136 
1137   ProgramStateRef stateZeroSize, stateNonZeroSize;
1138   std::tie(stateZeroSize, stateNonZeroSize) =
1139       assumeZero(C, state, sizeVal, sizeTy);
1140 
1141   // Get the value of the Dest.
1142   SVal destVal = state->getSVal(Dest.Expression, LCtx);
1143 
1144   // If the size is zero, there won't be any actual memory access, so
1145   // just bind the return value to the destination buffer and return.
1146   if (stateZeroSize && !stateNonZeroSize) {
1147     stateZeroSize = stateZeroSize->BindExpr(CE, LCtx, destVal);
1148     C.addTransition(stateZeroSize);
1149     return;
1150   }
1151 
1152   // If the size can be nonzero, we have to check the other arguments.
1153   if (stateNonZeroSize) {
1154     state = stateNonZeroSize;
1155 
1156     // Ensure the destination is not null. If it is NULL there will be a
1157     // NULL pointer dereference.
1158     state = checkNonNull(C, state, Dest, destVal);
1159     if (!state)
1160       return;
1161 
1162     // Get the value of the Src.
1163     SVal srcVal = state->getSVal(Source.Expression, LCtx);
1164 
1165     // Ensure the source is not null. If it is NULL there will be a
1166     // NULL pointer dereference.
1167     state = checkNonNull(C, state, Source, srcVal);
1168     if (!state)
1169       return;
1170 
1171     // Ensure the accesses are valid and that the buffers do not overlap.
1172     state = CheckBufferAccess(C, state, Dest, Size, AccessKind::write);
1173     state = CheckBufferAccess(C, state, Source, Size, AccessKind::read);
1174 
1175     if (Restricted)
1176       state = CheckOverlap(C, state, Size, Dest, Source);
1177 
1178     if (!state)
1179       return;
1180 
1181     // If this is mempcpy, get the byte after the last byte copied and
1182     // bind the expr.
1183     if (IsMempcpy) {
1184       // Get the byte after the last byte copied.
1185       SValBuilder &SvalBuilder = C.getSValBuilder();
1186       ASTContext &Ctx = SvalBuilder.getContext();
1187       QualType CharPtrTy = Ctx.getPointerType(Ctx.CharTy);
1188       SVal DestRegCharVal =
1189           SvalBuilder.evalCast(destVal, CharPtrTy, Dest.Expression->getType());
1190       SVal lastElement = C.getSValBuilder().evalBinOp(
1191           state, BO_Add, DestRegCharVal, sizeVal, Dest.Expression->getType());
1192       // If we don't know how much we copied, we can at least
1193       // conjure a return value for later.
1194       if (lastElement.isUnknown())
1195         lastElement = C.getSValBuilder().conjureSymbolVal(nullptr, CE, LCtx,
1196                                                           C.blockCount());
1197 
1198       // The byte after the last byte copied is the return value.
1199       state = state->BindExpr(CE, LCtx, lastElement);
1200     } else {
1201       // All other copies return the destination buffer.
1202       // (Well, bcopy() has a void return type, but this won't hurt.)
1203       state = state->BindExpr(CE, LCtx, destVal);
1204     }
1205 
1206     // Invalidate the destination (regular invalidation without pointer-escaping
1207     // the address of the top-level region).
1208     // FIXME: Even if we can't perfectly model the copy, we should see if we
1209     // can use LazyCompoundVals to copy the source values into the destination.
1210     // This would probably remove any existing bindings past the end of the
1211     // copied region, but that's still an improvement over blank invalidation.
1212     state =
1213         InvalidateBuffer(C, state, Dest.Expression, C.getSVal(Dest.Expression),
1214                          /*IsSourceBuffer*/ false, Size.Expression);
1215 
1216     // Invalidate the source (const-invalidation without const-pointer-escaping
1217     // the address of the top-level region).
1218     state = InvalidateBuffer(C, state, Source.Expression,
1219                              C.getSVal(Source.Expression),
1220                              /*IsSourceBuffer*/ true, nullptr);
1221 
1222     C.addTransition(state);
1223   }
1224 }
1225 
1226 void CStringChecker::evalMemcpy(CheckerContext &C, const CallExpr *CE) const {
1227   // void *memcpy(void *restrict dst, const void *restrict src, size_t n);
1228   // The return value is the address of the destination buffer.
1229   DestinationArgExpr Dest = {CE->getArg(0), 0};
1230   SourceArgExpr Src = {CE->getArg(1), 1};
1231   SizeArgExpr Size = {CE->getArg(2), 2};
1232 
1233   ProgramStateRef State = C.getState();
1234 
1235   constexpr bool IsRestricted = true;
1236   constexpr bool IsMempcpy = false;
1237   evalCopyCommon(C, CE, State, Size, Dest, Src, IsRestricted, IsMempcpy);
1238 }
1239 
1240 void CStringChecker::evalMempcpy(CheckerContext &C, const CallExpr *CE) const {
1241   // void *mempcpy(void *restrict dst, const void *restrict src, size_t n);
1242   // The return value is a pointer to the byte following the last written byte.
1243   DestinationArgExpr Dest = {CE->getArg(0), 0};
1244   SourceArgExpr Src = {CE->getArg(1), 1};
1245   SizeArgExpr Size = {CE->getArg(2), 2};
1246 
1247   constexpr bool IsRestricted = true;
1248   constexpr bool IsMempcpy = true;
1249   evalCopyCommon(C, CE, C.getState(), Size, Dest, Src, IsRestricted, IsMempcpy);
1250 }
1251 
1252 void CStringChecker::evalMemmove(CheckerContext &C, const CallExpr *CE) const {
1253   // void *memmove(void *dst, const void *src, size_t n);
1254   // The return value is the address of the destination buffer.
1255   DestinationArgExpr Dest = {CE->getArg(0), 0};
1256   SourceArgExpr Src = {CE->getArg(1), 1};
1257   SizeArgExpr Size = {CE->getArg(2), 2};
1258 
1259   constexpr bool IsRestricted = false;
1260   constexpr bool IsMempcpy = false;
1261   evalCopyCommon(C, CE, C.getState(), Size, Dest, Src, IsRestricted, IsMempcpy);
1262 }
1263 
1264 void CStringChecker::evalBcopy(CheckerContext &C, const CallExpr *CE) const {
1265   // void bcopy(const void *src, void *dst, size_t n);
1266   SourceArgExpr Src(CE->getArg(0), 0);
1267   DestinationArgExpr Dest = {CE->getArg(1), 1};
1268   SizeArgExpr Size = {CE->getArg(2), 2};
1269 
1270   constexpr bool IsRestricted = false;
1271   constexpr bool IsMempcpy = false;
1272   evalCopyCommon(C, CE, C.getState(), Size, Dest, Src, IsRestricted, IsMempcpy);
1273 }
1274 
1275 void CStringChecker::evalMemcmp(CheckerContext &C, const CallExpr *CE) const {
1276   // int memcmp(const void *s1, const void *s2, size_t n);
1277   CurrentFunctionDescription = "memory comparison function";
1278 
1279   AnyArgExpr Left = {CE->getArg(0), 0};
1280   AnyArgExpr Right = {CE->getArg(1), 1};
1281   SizeArgExpr Size = {CE->getArg(2), 2};
1282 
1283   ProgramStateRef State = C.getState();
1284   SValBuilder &Builder = C.getSValBuilder();
1285   const LocationContext *LCtx = C.getLocationContext();
1286 
1287   // See if the size argument is zero.
1288   SVal sizeVal = State->getSVal(Size.Expression, LCtx);
1289   QualType sizeTy = Size.Expression->getType();
1290 
1291   ProgramStateRef stateZeroSize, stateNonZeroSize;
1292   std::tie(stateZeroSize, stateNonZeroSize) =
1293       assumeZero(C, State, sizeVal, sizeTy);
1294 
1295   // If the size can be zero, the result will be 0 in that case, and we don't
1296   // have to check either of the buffers.
1297   if (stateZeroSize) {
1298     State = stateZeroSize;
1299     State = State->BindExpr(CE, LCtx, Builder.makeZeroVal(CE->getType()));
1300     C.addTransition(State);
1301   }
1302 
1303   // If the size can be nonzero, we have to check the other arguments.
1304   if (stateNonZeroSize) {
1305     State = stateNonZeroSize;
1306     // If we know the two buffers are the same, we know the result is 0.
1307     // First, get the two buffers' addresses. Another checker will have already
1308     // made sure they're not undefined.
1309     DefinedOrUnknownSVal LV =
1310         State->getSVal(Left.Expression, LCtx).castAs<DefinedOrUnknownSVal>();
1311     DefinedOrUnknownSVal RV =
1312         State->getSVal(Right.Expression, LCtx).castAs<DefinedOrUnknownSVal>();
1313 
1314     // See if they are the same.
1315     ProgramStateRef SameBuffer, NotSameBuffer;
1316     std::tie(SameBuffer, NotSameBuffer) =
1317         State->assume(Builder.evalEQ(State, LV, RV));
1318 
1319     // If the two arguments are the same buffer, we know the result is 0,
1320     // and we only need to check one size.
1321     if (SameBuffer && !NotSameBuffer) {
1322       State = SameBuffer;
1323       State = CheckBufferAccess(C, State, Left, Size, AccessKind::read);
1324       if (State) {
1325         State =
1326             SameBuffer->BindExpr(CE, LCtx, Builder.makeZeroVal(CE->getType()));
1327         C.addTransition(State);
1328       }
1329       return;
1330     }
1331 
1332     // If the two arguments might be different buffers, we have to check
1333     // the size of both of them.
1334     assert(NotSameBuffer);
1335     State = CheckBufferAccess(C, State, Right, Size, AccessKind::read);
1336     State = CheckBufferAccess(C, State, Left, Size, AccessKind::read);
1337     if (State) {
1338       // The return value is the comparison result, which we don't know.
1339       SVal CmpV = Builder.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount());
1340       State = State->BindExpr(CE, LCtx, CmpV);
1341       C.addTransition(State);
1342     }
1343   }
1344 }
1345 
1346 void CStringChecker::evalstrLength(CheckerContext &C,
1347                                    const CallExpr *CE) const {
1348   // size_t strlen(const char *s);
1349   evalstrLengthCommon(C, CE, /* IsStrnlen = */ false);
1350 }
1351 
1352 void CStringChecker::evalstrnLength(CheckerContext &C,
1353                                     const CallExpr *CE) const {
1354   // size_t strnlen(const char *s, size_t maxlen);
1355   evalstrLengthCommon(C, CE, /* IsStrnlen = */ true);
1356 }
1357 
1358 void CStringChecker::evalstrLengthCommon(CheckerContext &C, const CallExpr *CE,
1359                                          bool IsStrnlen) const {
1360   CurrentFunctionDescription = "string length function";
1361   ProgramStateRef state = C.getState();
1362   const LocationContext *LCtx = C.getLocationContext();
1363 
1364   if (IsStrnlen) {
1365     const Expr *maxlenExpr = CE->getArg(1);
1366     SVal maxlenVal = state->getSVal(maxlenExpr, LCtx);
1367 
1368     ProgramStateRef stateZeroSize, stateNonZeroSize;
1369     std::tie(stateZeroSize, stateNonZeroSize) =
1370       assumeZero(C, state, maxlenVal, maxlenExpr->getType());
1371 
1372     // If the size can be zero, the result will be 0 in that case, and we don't
1373     // have to check the string itself.
1374     if (stateZeroSize) {
1375       SVal zero = C.getSValBuilder().makeZeroVal(CE->getType());
1376       stateZeroSize = stateZeroSize->BindExpr(CE, LCtx, zero);
1377       C.addTransition(stateZeroSize);
1378     }
1379 
1380     // If the size is GUARANTEED to be zero, we're done!
1381     if (!stateNonZeroSize)
1382       return;
1383 
1384     // Otherwise, record the assumption that the size is nonzero.
1385     state = stateNonZeroSize;
1386   }
1387 
1388   // Check that the string argument is non-null.
1389   AnyArgExpr Arg = {CE->getArg(0), 0};
1390   SVal ArgVal = state->getSVal(Arg.Expression, LCtx);
1391   state = checkNonNull(C, state, Arg, ArgVal);
1392 
1393   if (!state)
1394     return;
1395 
1396   SVal strLength = getCStringLength(C, state, Arg.Expression, ArgVal);
1397 
1398   // If the argument isn't a valid C string, there's no valid state to
1399   // transition to.
1400   if (strLength.isUndef())
1401     return;
1402 
1403   DefinedOrUnknownSVal result = UnknownVal();
1404 
1405   // If the check is for strnlen() then bind the return value to no more than
1406   // the maxlen value.
1407   if (IsStrnlen) {
1408     QualType cmpTy = C.getSValBuilder().getConditionType();
1409 
1410     // It's a little unfortunate to be getting this again,
1411     // but it's not that expensive...
1412     const Expr *maxlenExpr = CE->getArg(1);
1413     SVal maxlenVal = state->getSVal(maxlenExpr, LCtx);
1414 
1415     Optional<NonLoc> strLengthNL = strLength.getAs<NonLoc>();
1416     Optional<NonLoc> maxlenValNL = maxlenVal.getAs<NonLoc>();
1417 
1418     if (strLengthNL && maxlenValNL) {
1419       ProgramStateRef stateStringTooLong, stateStringNotTooLong;
1420 
1421       // Check if the strLength is greater than the maxlen.
1422       std::tie(stateStringTooLong, stateStringNotTooLong) = state->assume(
1423           C.getSValBuilder()
1424               .evalBinOpNN(state, BO_GT, *strLengthNL, *maxlenValNL, cmpTy)
1425               .castAs<DefinedOrUnknownSVal>());
1426 
1427       if (stateStringTooLong && !stateStringNotTooLong) {
1428         // If the string is longer than maxlen, return maxlen.
1429         result = *maxlenValNL;
1430       } else if (stateStringNotTooLong && !stateStringTooLong) {
1431         // If the string is shorter than maxlen, return its length.
1432         result = *strLengthNL;
1433       }
1434     }
1435 
1436     if (result.isUnknown()) {
1437       // If we don't have enough information for a comparison, there's
1438       // no guarantee the full string length will actually be returned.
1439       // All we know is the return value is the min of the string length
1440       // and the limit. This is better than nothing.
1441       result = C.getSValBuilder().conjureSymbolVal(nullptr, CE, LCtx,
1442                                                    C.blockCount());
1443       NonLoc resultNL = result.castAs<NonLoc>();
1444 
1445       if (strLengthNL) {
1446         state = state->assume(C.getSValBuilder().evalBinOpNN(
1447                                   state, BO_LE, resultNL, *strLengthNL, cmpTy)
1448                                   .castAs<DefinedOrUnknownSVal>(), true);
1449       }
1450 
1451       if (maxlenValNL) {
1452         state = state->assume(C.getSValBuilder().evalBinOpNN(
1453                                   state, BO_LE, resultNL, *maxlenValNL, cmpTy)
1454                                   .castAs<DefinedOrUnknownSVal>(), true);
1455       }
1456     }
1457 
1458   } else {
1459     // This is a plain strlen(), not strnlen().
1460     result = strLength.castAs<DefinedOrUnknownSVal>();
1461 
1462     // If we don't know the length of the string, conjure a return
1463     // value, so it can be used in constraints, at least.
1464     if (result.isUnknown()) {
1465       result = C.getSValBuilder().conjureSymbolVal(nullptr, CE, LCtx,
1466                                                    C.blockCount());
1467     }
1468   }
1469 
1470   // Bind the return value.
1471   assert(!result.isUnknown() && "Should have conjured a value by now");
1472   state = state->BindExpr(CE, LCtx, result);
1473   C.addTransition(state);
1474 }
1475 
1476 void CStringChecker::evalStrcpy(CheckerContext &C, const CallExpr *CE) const {
1477   // char *strcpy(char *restrict dst, const char *restrict src);
1478   evalStrcpyCommon(C, CE,
1479                    /* ReturnEnd = */ false,
1480                    /* IsBounded = */ false,
1481                    /* appendK = */ ConcatFnKind::none);
1482 }
1483 
1484 void CStringChecker::evalStrncpy(CheckerContext &C, const CallExpr *CE) const {
1485   // char *strncpy(char *restrict dst, const char *restrict src, size_t n);
1486   evalStrcpyCommon(C, CE,
1487                    /* ReturnEnd = */ false,
1488                    /* IsBounded = */ true,
1489                    /* appendK = */ ConcatFnKind::none);
1490 }
1491 
1492 void CStringChecker::evalStpcpy(CheckerContext &C, const CallExpr *CE) const {
1493   // char *stpcpy(char *restrict dst, const char *restrict src);
1494   evalStrcpyCommon(C, CE,
1495                    /* ReturnEnd = */ true,
1496                    /* IsBounded = */ false,
1497                    /* appendK = */ ConcatFnKind::none);
1498 }
1499 
1500 void CStringChecker::evalStrlcpy(CheckerContext &C, const CallExpr *CE) const {
1501   // size_t strlcpy(char *dest, const char *src, size_t size);
1502   evalStrcpyCommon(C, CE,
1503                    /* ReturnEnd = */ true,
1504                    /* IsBounded = */ true,
1505                    /* appendK = */ ConcatFnKind::none,
1506                    /* returnPtr = */ false);
1507 }
1508 
1509 void CStringChecker::evalStrcat(CheckerContext &C, const CallExpr *CE) const {
1510   // char *strcat(char *restrict s1, const char *restrict s2);
1511   evalStrcpyCommon(C, CE,
1512                    /* ReturnEnd = */ false,
1513                    /* IsBounded = */ false,
1514                    /* appendK = */ ConcatFnKind::strcat);
1515 }
1516 
1517 void CStringChecker::evalStrncat(CheckerContext &C, const CallExpr *CE) const {
1518   // char *strncat(char *restrict s1, const char *restrict s2, size_t n);
1519   evalStrcpyCommon(C, CE,
1520                    /* ReturnEnd = */ false,
1521                    /* IsBounded = */ true,
1522                    /* appendK = */ ConcatFnKind::strcat);
1523 }
1524 
1525 void CStringChecker::evalStrlcat(CheckerContext &C, const CallExpr *CE) const {
1526   // size_t strlcat(char *dst, const char *src, size_t size);
1527   // It will append at most size - strlen(dst) - 1 bytes,
1528   // NULL-terminating the result.
1529   evalStrcpyCommon(C, CE,
1530                    /* ReturnEnd = */ false,
1531                    /* IsBounded = */ true,
1532                    /* appendK = */ ConcatFnKind::strlcat,
1533                    /* returnPtr = */ false);
1534 }
1535 
1536 void CStringChecker::evalStrcpyCommon(CheckerContext &C, const CallExpr *CE,
1537                                       bool ReturnEnd, bool IsBounded,
1538                                       ConcatFnKind appendK,
1539                                       bool returnPtr) const {
1540   if (appendK == ConcatFnKind::none)
1541     CurrentFunctionDescription = "string copy function";
1542   else
1543     CurrentFunctionDescription = "string concatenation function";
1544 
1545   ProgramStateRef state = C.getState();
1546   const LocationContext *LCtx = C.getLocationContext();
1547 
1548   // Check that the destination is non-null.
1549   DestinationArgExpr Dst = {CE->getArg(0), 0};
1550   SVal DstVal = state->getSVal(Dst.Expression, LCtx);
1551   state = checkNonNull(C, state, Dst, DstVal);
1552   if (!state)
1553     return;
1554 
1555   // Check that the source is non-null.
1556   SourceArgExpr srcExpr = {CE->getArg(1), 1};
1557   SVal srcVal = state->getSVal(srcExpr.Expression, LCtx);
1558   state = checkNonNull(C, state, srcExpr, srcVal);
1559   if (!state)
1560     return;
1561 
1562   // Get the string length of the source.
1563   SVal strLength = getCStringLength(C, state, srcExpr.Expression, srcVal);
1564   Optional<NonLoc> strLengthNL = strLength.getAs<NonLoc>();
1565 
1566   // Get the string length of the destination buffer.
1567   SVal dstStrLength = getCStringLength(C, state, Dst.Expression, DstVal);
1568   Optional<NonLoc> dstStrLengthNL = dstStrLength.getAs<NonLoc>();
1569 
1570   // If the source isn't a valid C string, give up.
1571   if (strLength.isUndef())
1572     return;
1573 
1574   SValBuilder &svalBuilder = C.getSValBuilder();
1575   QualType cmpTy = svalBuilder.getConditionType();
1576   QualType sizeTy = svalBuilder.getContext().getSizeType();
1577 
1578   // These two values allow checking two kinds of errors:
1579   // - actual overflows caused by a source that doesn't fit in the destination
1580   // - potential overflows caused by a bound that could exceed the destination
1581   SVal amountCopied = UnknownVal();
1582   SVal maxLastElementIndex = UnknownVal();
1583   const char *boundWarning = nullptr;
1584 
1585   // FIXME: Why do we choose the srcExpr if the access has no size?
1586   //  Note that the 3rd argument of the call would be the size parameter.
1587   SizeArgExpr SrcExprAsSizeDummy = {srcExpr.Expression, srcExpr.ArgumentIndex};
1588   state = CheckOverlap(
1589       C, state,
1590       (IsBounded ? SizeArgExpr{CE->getArg(2), 2} : SrcExprAsSizeDummy), Dst,
1591       srcExpr);
1592 
1593   if (!state)
1594     return;
1595 
1596   // If the function is strncpy, strncat, etc... it is bounded.
1597   if (IsBounded) {
1598     // Get the max number of characters to copy.
1599     SizeArgExpr lenExpr = {CE->getArg(2), 2};
1600     SVal lenVal = state->getSVal(lenExpr.Expression, LCtx);
1601 
1602     // Protect against misdeclared strncpy().
1603     lenVal =
1604         svalBuilder.evalCast(lenVal, sizeTy, lenExpr.Expression->getType());
1605 
1606     Optional<NonLoc> lenValNL = lenVal.getAs<NonLoc>();
1607 
1608     // If we know both values, we might be able to figure out how much
1609     // we're copying.
1610     if (strLengthNL && lenValNL) {
1611       switch (appendK) {
1612       case ConcatFnKind::none:
1613       case ConcatFnKind::strcat: {
1614         ProgramStateRef stateSourceTooLong, stateSourceNotTooLong;
1615         // Check if the max number to copy is less than the length of the src.
1616         // If the bound is equal to the source length, strncpy won't null-
1617         // terminate the result!
1618         std::tie(stateSourceTooLong, stateSourceNotTooLong) = state->assume(
1619             svalBuilder
1620                 .evalBinOpNN(state, BO_GE, *strLengthNL, *lenValNL, cmpTy)
1621                 .castAs<DefinedOrUnknownSVal>());
1622 
1623         if (stateSourceTooLong && !stateSourceNotTooLong) {
1624           // Max number to copy is less than the length of the src, so the
1625           // actual strLength copied is the max number arg.
1626           state = stateSourceTooLong;
1627           amountCopied = lenVal;
1628 
1629         } else if (!stateSourceTooLong && stateSourceNotTooLong) {
1630           // The source buffer entirely fits in the bound.
1631           state = stateSourceNotTooLong;
1632           amountCopied = strLength;
1633         }
1634         break;
1635       }
1636       case ConcatFnKind::strlcat:
1637         if (!dstStrLengthNL)
1638           return;
1639 
1640         // amountCopied = min (size - dstLen - 1 , srcLen)
1641         SVal freeSpace = svalBuilder.evalBinOpNN(state, BO_Sub, *lenValNL,
1642                                                  *dstStrLengthNL, sizeTy);
1643         if (!freeSpace.getAs<NonLoc>())
1644           return;
1645         freeSpace =
1646             svalBuilder.evalBinOp(state, BO_Sub, freeSpace,
1647                                   svalBuilder.makeIntVal(1, sizeTy), sizeTy);
1648         Optional<NonLoc> freeSpaceNL = freeSpace.getAs<NonLoc>();
1649 
1650         // While unlikely, it is possible that the subtraction is
1651         // too complex to compute, let's check whether it succeeded.
1652         if (!freeSpaceNL)
1653           return;
1654         SVal hasEnoughSpace = svalBuilder.evalBinOpNN(
1655             state, BO_LE, *strLengthNL, *freeSpaceNL, cmpTy);
1656 
1657         ProgramStateRef TrueState, FalseState;
1658         std::tie(TrueState, FalseState) =
1659             state->assume(hasEnoughSpace.castAs<DefinedOrUnknownSVal>());
1660 
1661         // srcStrLength <= size - dstStrLength -1
1662         if (TrueState && !FalseState) {
1663           amountCopied = strLength;
1664         }
1665 
1666         // srcStrLength > size - dstStrLength -1
1667         if (!TrueState && FalseState) {
1668           amountCopied = freeSpace;
1669         }
1670 
1671         if (TrueState && FalseState)
1672           amountCopied = UnknownVal();
1673         break;
1674       }
1675     }
1676     // We still want to know if the bound is known to be too large.
1677     if (lenValNL) {
1678       switch (appendK) {
1679       case ConcatFnKind::strcat:
1680         // For strncat, the check is strlen(dst) + lenVal < sizeof(dst)
1681 
1682         // Get the string length of the destination. If the destination is
1683         // memory that can't have a string length, we shouldn't be copying
1684         // into it anyway.
1685         if (dstStrLength.isUndef())
1686           return;
1687 
1688         if (dstStrLengthNL) {
1689           maxLastElementIndex = svalBuilder.evalBinOpNN(
1690               state, BO_Add, *lenValNL, *dstStrLengthNL, sizeTy);
1691 
1692           boundWarning = "Size argument is greater than the free space in the "
1693                          "destination buffer";
1694         }
1695         break;
1696       case ConcatFnKind::none:
1697       case ConcatFnKind::strlcat:
1698         // For strncpy and strlcat, this is just checking
1699         //  that lenVal <= sizeof(dst).
1700         // (Yes, strncpy and strncat differ in how they treat termination.
1701         // strncat ALWAYS terminates, but strncpy doesn't.)
1702 
1703         // We need a special case for when the copy size is zero, in which
1704         // case strncpy will do no work at all. Our bounds check uses n-1
1705         // as the last element accessed, so n == 0 is problematic.
1706         ProgramStateRef StateZeroSize, StateNonZeroSize;
1707         std::tie(StateZeroSize, StateNonZeroSize) =
1708             assumeZero(C, state, *lenValNL, sizeTy);
1709 
1710         // If the size is known to be zero, we're done.
1711         if (StateZeroSize && !StateNonZeroSize) {
1712           if (returnPtr) {
1713             StateZeroSize = StateZeroSize->BindExpr(CE, LCtx, DstVal);
1714           } else {
1715             if (appendK == ConcatFnKind::none) {
1716               // strlcpy returns strlen(src)
1717               StateZeroSize = StateZeroSize->BindExpr(CE, LCtx, strLength);
1718             } else {
1719               // strlcat returns strlen(src) + strlen(dst)
1720               SVal retSize = svalBuilder.evalBinOp(
1721                   state, BO_Add, strLength, dstStrLength, sizeTy);
1722               StateZeroSize = StateZeroSize->BindExpr(CE, LCtx, retSize);
1723             }
1724           }
1725           C.addTransition(StateZeroSize);
1726           return;
1727         }
1728 
1729         // Otherwise, go ahead and figure out the last element we'll touch.
1730         // We don't record the non-zero assumption here because we can't
1731         // be sure. We won't warn on a possible zero.
1732         NonLoc one = svalBuilder.makeIntVal(1, sizeTy).castAs<NonLoc>();
1733         maxLastElementIndex =
1734             svalBuilder.evalBinOpNN(state, BO_Sub, *lenValNL, one, sizeTy);
1735         boundWarning = "Size argument is greater than the length of the "
1736                        "destination buffer";
1737         break;
1738       }
1739     }
1740   } else {
1741     // The function isn't bounded. The amount copied should match the length
1742     // of the source buffer.
1743     amountCopied = strLength;
1744   }
1745 
1746   assert(state);
1747 
1748   // This represents the number of characters copied into the destination
1749   // buffer. (It may not actually be the strlen if the destination buffer
1750   // is not terminated.)
1751   SVal finalStrLength = UnknownVal();
1752   SVal strlRetVal = UnknownVal();
1753 
1754   if (appendK == ConcatFnKind::none && !returnPtr) {
1755     // strlcpy returns the sizeof(src)
1756     strlRetVal = strLength;
1757   }
1758 
1759   // If this is an appending function (strcat, strncat...) then set the
1760   // string length to strlen(src) + strlen(dst) since the buffer will
1761   // ultimately contain both.
1762   if (appendK != ConcatFnKind::none) {
1763     // Get the string length of the destination. If the destination is memory
1764     // that can't have a string length, we shouldn't be copying into it anyway.
1765     if (dstStrLength.isUndef())
1766       return;
1767 
1768     if (appendK == ConcatFnKind::strlcat && dstStrLengthNL && strLengthNL) {
1769       strlRetVal = svalBuilder.evalBinOpNN(state, BO_Add, *strLengthNL,
1770                                            *dstStrLengthNL, sizeTy);
1771     }
1772 
1773     Optional<NonLoc> amountCopiedNL = amountCopied.getAs<NonLoc>();
1774 
1775     // If we know both string lengths, we might know the final string length.
1776     if (amountCopiedNL && dstStrLengthNL) {
1777       // Make sure the two lengths together don't overflow a size_t.
1778       state = checkAdditionOverflow(C, state, *amountCopiedNL, *dstStrLengthNL);
1779       if (!state)
1780         return;
1781 
1782       finalStrLength = svalBuilder.evalBinOpNN(state, BO_Add, *amountCopiedNL,
1783                                                *dstStrLengthNL, sizeTy);
1784     }
1785 
1786     // If we couldn't get a single value for the final string length,
1787     // we can at least bound it by the individual lengths.
1788     if (finalStrLength.isUnknown()) {
1789       // Try to get a "hypothetical" string length symbol, which we can later
1790       // set as a real value if that turns out to be the case.
1791       finalStrLength = getCStringLength(C, state, CE, DstVal, true);
1792       assert(!finalStrLength.isUndef());
1793 
1794       if (Optional<NonLoc> finalStrLengthNL = finalStrLength.getAs<NonLoc>()) {
1795         if (amountCopiedNL && appendK == ConcatFnKind::none) {
1796           // we overwrite dst string with the src
1797           // finalStrLength >= srcStrLength
1798           SVal sourceInResult = svalBuilder.evalBinOpNN(
1799               state, BO_GE, *finalStrLengthNL, *amountCopiedNL, cmpTy);
1800           state = state->assume(sourceInResult.castAs<DefinedOrUnknownSVal>(),
1801                                 true);
1802           if (!state)
1803             return;
1804         }
1805 
1806         if (dstStrLengthNL && appendK != ConcatFnKind::none) {
1807           // we extend the dst string with the src
1808           // finalStrLength >= dstStrLength
1809           SVal destInResult = svalBuilder.evalBinOpNN(state, BO_GE,
1810                                                       *finalStrLengthNL,
1811                                                       *dstStrLengthNL,
1812                                                       cmpTy);
1813           state =
1814               state->assume(destInResult.castAs<DefinedOrUnknownSVal>(), true);
1815           if (!state)
1816             return;
1817         }
1818       }
1819     }
1820 
1821   } else {
1822     // Otherwise, this is a copy-over function (strcpy, strncpy, ...), and
1823     // the final string length will match the input string length.
1824     finalStrLength = amountCopied;
1825   }
1826 
1827   SVal Result;
1828 
1829   if (returnPtr) {
1830     // The final result of the function will either be a pointer past the last
1831     // copied element, or a pointer to the start of the destination buffer.
1832     Result = (ReturnEnd ? UnknownVal() : DstVal);
1833   } else {
1834     if (appendK == ConcatFnKind::strlcat || appendK == ConcatFnKind::none)
1835       //strlcpy, strlcat
1836       Result = strlRetVal;
1837     else
1838       Result = finalStrLength;
1839   }
1840 
1841   assert(state);
1842 
1843   // If the destination is a MemRegion, try to check for a buffer overflow and
1844   // record the new string length.
1845   if (Optional<loc::MemRegionVal> dstRegVal =
1846       DstVal.getAs<loc::MemRegionVal>()) {
1847     QualType ptrTy = Dst.Expression->getType();
1848 
1849     // If we have an exact value on a bounded copy, use that to check for
1850     // overflows, rather than our estimate about how much is actually copied.
1851     if (Optional<NonLoc> maxLastNL = maxLastElementIndex.getAs<NonLoc>()) {
1852       SVal maxLastElement =
1853           svalBuilder.evalBinOpLN(state, BO_Add, *dstRegVal, *maxLastNL, ptrTy);
1854 
1855       state = CheckLocation(C, state, Dst, maxLastElement, AccessKind::write);
1856       if (!state)
1857         return;
1858     }
1859 
1860     // Then, if the final length is known...
1861     if (Optional<NonLoc> knownStrLength = finalStrLength.getAs<NonLoc>()) {
1862       SVal lastElement = svalBuilder.evalBinOpLN(state, BO_Add, *dstRegVal,
1863           *knownStrLength, ptrTy);
1864 
1865       // ...and we haven't checked the bound, we'll check the actual copy.
1866       if (!boundWarning) {
1867         state = CheckLocation(C, state, Dst, lastElement, AccessKind::write);
1868         if (!state)
1869           return;
1870       }
1871 
1872       // If this is a stpcpy-style copy, the last element is the return value.
1873       if (returnPtr && ReturnEnd)
1874         Result = lastElement;
1875     }
1876 
1877     // Invalidate the destination (regular invalidation without pointer-escaping
1878     // the address of the top-level region). This must happen before we set the
1879     // C string length because invalidation will clear the length.
1880     // FIXME: Even if we can't perfectly model the copy, we should see if we
1881     // can use LazyCompoundVals to copy the source values into the destination.
1882     // This would probably remove any existing bindings past the end of the
1883     // string, but that's still an improvement over blank invalidation.
1884     state = InvalidateBuffer(C, state, Dst.Expression, *dstRegVal,
1885                              /*IsSourceBuffer*/ false, nullptr);
1886 
1887     // Invalidate the source (const-invalidation without const-pointer-escaping
1888     // the address of the top-level region).
1889     state = InvalidateBuffer(C, state, srcExpr.Expression, srcVal,
1890                              /*IsSourceBuffer*/ true, nullptr);
1891 
1892     // Set the C string length of the destination, if we know it.
1893     if (IsBounded && (appendK == ConcatFnKind::none)) {
1894       // strncpy is annoying in that it doesn't guarantee to null-terminate
1895       // the result string. If the original string didn't fit entirely inside
1896       // the bound (including the null-terminator), we don't know how long the
1897       // result is.
1898       if (amountCopied != strLength)
1899         finalStrLength = UnknownVal();
1900     }
1901     state = setCStringLength(state, dstRegVal->getRegion(), finalStrLength);
1902   }
1903 
1904   assert(state);
1905 
1906   if (returnPtr) {
1907     // If this is a stpcpy-style copy, but we were unable to check for a buffer
1908     // overflow, we still need a result. Conjure a return value.
1909     if (ReturnEnd && Result.isUnknown()) {
1910       Result = svalBuilder.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount());
1911     }
1912   }
1913   // Set the return value.
1914   state = state->BindExpr(CE, LCtx, Result);
1915   C.addTransition(state);
1916 }
1917 
1918 void CStringChecker::evalStrcmp(CheckerContext &C, const CallExpr *CE) const {
1919   //int strcmp(const char *s1, const char *s2);
1920   evalStrcmpCommon(C, CE, /* IsBounded = */ false, /* IgnoreCase = */ false);
1921 }
1922 
1923 void CStringChecker::evalStrncmp(CheckerContext &C, const CallExpr *CE) const {
1924   //int strncmp(const char *s1, const char *s2, size_t n);
1925   evalStrcmpCommon(C, CE, /* IsBounded = */ true, /* IgnoreCase = */ false);
1926 }
1927 
1928 void CStringChecker::evalStrcasecmp(CheckerContext &C,
1929     const CallExpr *CE) const {
1930   //int strcasecmp(const char *s1, const char *s2);
1931   evalStrcmpCommon(C, CE, /* IsBounded = */ false, /* IgnoreCase = */ true);
1932 }
1933 
1934 void CStringChecker::evalStrncasecmp(CheckerContext &C,
1935     const CallExpr *CE) const {
1936   //int strncasecmp(const char *s1, const char *s2, size_t n);
1937   evalStrcmpCommon(C, CE, /* IsBounded = */ true, /* IgnoreCase = */ true);
1938 }
1939 
1940 void CStringChecker::evalStrcmpCommon(CheckerContext &C, const CallExpr *CE,
1941     bool IsBounded, bool IgnoreCase) const {
1942   CurrentFunctionDescription = "string comparison function";
1943   ProgramStateRef state = C.getState();
1944   const LocationContext *LCtx = C.getLocationContext();
1945 
1946   // Check that the first string is non-null
1947   AnyArgExpr Left = {CE->getArg(0), 0};
1948   SVal LeftVal = state->getSVal(Left.Expression, LCtx);
1949   state = checkNonNull(C, state, Left, LeftVal);
1950   if (!state)
1951     return;
1952 
1953   // Check that the second string is non-null.
1954   AnyArgExpr Right = {CE->getArg(1), 1};
1955   SVal RightVal = state->getSVal(Right.Expression, LCtx);
1956   state = checkNonNull(C, state, Right, RightVal);
1957   if (!state)
1958     return;
1959 
1960   // Get the string length of the first string or give up.
1961   SVal LeftLength = getCStringLength(C, state, Left.Expression, LeftVal);
1962   if (LeftLength.isUndef())
1963     return;
1964 
1965   // Get the string length of the second string or give up.
1966   SVal RightLength = getCStringLength(C, state, Right.Expression, RightVal);
1967   if (RightLength.isUndef())
1968     return;
1969 
1970   // If we know the two buffers are the same, we know the result is 0.
1971   // First, get the two buffers' addresses. Another checker will have already
1972   // made sure they're not undefined.
1973   DefinedOrUnknownSVal LV = LeftVal.castAs<DefinedOrUnknownSVal>();
1974   DefinedOrUnknownSVal RV = RightVal.castAs<DefinedOrUnknownSVal>();
1975 
1976   // See if they are the same.
1977   SValBuilder &svalBuilder = C.getSValBuilder();
1978   DefinedOrUnknownSVal SameBuf = svalBuilder.evalEQ(state, LV, RV);
1979   ProgramStateRef StSameBuf, StNotSameBuf;
1980   std::tie(StSameBuf, StNotSameBuf) = state->assume(SameBuf);
1981 
1982   // If the two arguments might be the same buffer, we know the result is 0,
1983   // and we only need to check one size.
1984   if (StSameBuf) {
1985     StSameBuf = StSameBuf->BindExpr(CE, LCtx,
1986         svalBuilder.makeZeroVal(CE->getType()));
1987     C.addTransition(StSameBuf);
1988 
1989     // If the two arguments are GUARANTEED to be the same, we're done!
1990     if (!StNotSameBuf)
1991       return;
1992   }
1993 
1994   assert(StNotSameBuf);
1995   state = StNotSameBuf;
1996 
1997   // At this point we can go about comparing the two buffers.
1998   // For now, we only do this if they're both known string literals.
1999 
2000   // Attempt to extract string literals from both expressions.
2001   const StringLiteral *LeftStrLiteral =
2002       getCStringLiteral(C, state, Left.Expression, LeftVal);
2003   const StringLiteral *RightStrLiteral =
2004       getCStringLiteral(C, state, Right.Expression, RightVal);
2005   bool canComputeResult = false;
2006   SVal resultVal = svalBuilder.conjureSymbolVal(nullptr, CE, LCtx,
2007       C.blockCount());
2008 
2009   if (LeftStrLiteral && RightStrLiteral) {
2010     StringRef LeftStrRef = LeftStrLiteral->getString();
2011     StringRef RightStrRef = RightStrLiteral->getString();
2012 
2013     if (IsBounded) {
2014       // Get the max number of characters to compare.
2015       const Expr *lenExpr = CE->getArg(2);
2016       SVal lenVal = state->getSVal(lenExpr, LCtx);
2017 
2018       // If the length is known, we can get the right substrings.
2019       if (const llvm::APSInt *len = svalBuilder.getKnownValue(state, lenVal)) {
2020         // Create substrings of each to compare the prefix.
2021         LeftStrRef = LeftStrRef.substr(0, (size_t)len->getZExtValue());
2022         RightStrRef = RightStrRef.substr(0, (size_t)len->getZExtValue());
2023         canComputeResult = true;
2024       }
2025     } else {
2026       // This is a normal, unbounded strcmp.
2027       canComputeResult = true;
2028     }
2029 
2030     if (canComputeResult) {
2031       // Real strcmp stops at null characters.
2032       size_t s1Term = LeftStrRef.find('\0');
2033       if (s1Term != StringRef::npos)
2034         LeftStrRef = LeftStrRef.substr(0, s1Term);
2035 
2036       size_t s2Term = RightStrRef.find('\0');
2037       if (s2Term != StringRef::npos)
2038         RightStrRef = RightStrRef.substr(0, s2Term);
2039 
2040       // Use StringRef's comparison methods to compute the actual result.
2041       int compareRes = IgnoreCase ? LeftStrRef.compare_insensitive(RightStrRef)
2042                                   : LeftStrRef.compare(RightStrRef);
2043 
2044       // The strcmp function returns an integer greater than, equal to, or less
2045       // than zero, [c11, p7.24.4.2].
2046       if (compareRes == 0) {
2047         resultVal = svalBuilder.makeIntVal(compareRes, CE->getType());
2048       }
2049       else {
2050         DefinedSVal zeroVal = svalBuilder.makeIntVal(0, CE->getType());
2051         // Constrain strcmp's result range based on the result of StringRef's
2052         // comparison methods.
2053         BinaryOperatorKind op = (compareRes == 1) ? BO_GT : BO_LT;
2054         SVal compareWithZero =
2055           svalBuilder.evalBinOp(state, op, resultVal, zeroVal,
2056               svalBuilder.getConditionType());
2057         DefinedSVal compareWithZeroVal = compareWithZero.castAs<DefinedSVal>();
2058         state = state->assume(compareWithZeroVal, true);
2059       }
2060     }
2061   }
2062 
2063   state = state->BindExpr(CE, LCtx, resultVal);
2064 
2065   // Record this as a possible path.
2066   C.addTransition(state);
2067 }
2068 
2069 void CStringChecker::evalStrsep(CheckerContext &C, const CallExpr *CE) const {
2070   // char *strsep(char **stringp, const char *delim);
2071   // Verify whether the search string parameter matches the return type.
2072   SourceArgExpr SearchStrPtr = {CE->getArg(0), 0};
2073 
2074   QualType CharPtrTy = SearchStrPtr.Expression->getType()->getPointeeType();
2075   if (CharPtrTy.isNull() ||
2076       CE->getType().getUnqualifiedType() != CharPtrTy.getUnqualifiedType())
2077     return;
2078 
2079   CurrentFunctionDescription = "strsep()";
2080   ProgramStateRef State = C.getState();
2081   const LocationContext *LCtx = C.getLocationContext();
2082 
2083   // Check that the search string pointer is non-null (though it may point to
2084   // a null string).
2085   SVal SearchStrVal = State->getSVal(SearchStrPtr.Expression, LCtx);
2086   State = checkNonNull(C, State, SearchStrPtr, SearchStrVal);
2087   if (!State)
2088     return;
2089 
2090   // Check that the delimiter string is non-null.
2091   AnyArgExpr DelimStr = {CE->getArg(1), 1};
2092   SVal DelimStrVal = State->getSVal(DelimStr.Expression, LCtx);
2093   State = checkNonNull(C, State, DelimStr, DelimStrVal);
2094   if (!State)
2095     return;
2096 
2097   SValBuilder &SVB = C.getSValBuilder();
2098   SVal Result;
2099   if (Optional<Loc> SearchStrLoc = SearchStrVal.getAs<Loc>()) {
2100     // Get the current value of the search string pointer, as a char*.
2101     Result = State->getSVal(*SearchStrLoc, CharPtrTy);
2102 
2103     // Invalidate the search string, representing the change of one delimiter
2104     // character to NUL.
2105     State = InvalidateBuffer(C, State, SearchStrPtr.Expression, Result,
2106                              /*IsSourceBuffer*/ false, nullptr);
2107 
2108     // Overwrite the search string pointer. The new value is either an address
2109     // further along in the same string, or NULL if there are no more tokens.
2110     State = State->bindLoc(*SearchStrLoc,
2111         SVB.conjureSymbolVal(getTag(),
2112           CE,
2113           LCtx,
2114           CharPtrTy,
2115           C.blockCount()),
2116         LCtx);
2117   } else {
2118     assert(SearchStrVal.isUnknown());
2119     // Conjure a symbolic value. It's the best we can do.
2120     Result = SVB.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount());
2121   }
2122 
2123   // Set the return value, and finish.
2124   State = State->BindExpr(CE, LCtx, Result);
2125   C.addTransition(State);
2126 }
2127 
2128 // These should probably be moved into a C++ standard library checker.
2129 void CStringChecker::evalStdCopy(CheckerContext &C, const CallExpr *CE) const {
2130   evalStdCopyCommon(C, CE);
2131 }
2132 
2133 void CStringChecker::evalStdCopyBackward(CheckerContext &C,
2134     const CallExpr *CE) const {
2135   evalStdCopyCommon(C, CE);
2136 }
2137 
2138 void CStringChecker::evalStdCopyCommon(CheckerContext &C,
2139     const CallExpr *CE) const {
2140   if (!CE->getArg(2)->getType()->isPointerType())
2141     return;
2142 
2143   ProgramStateRef State = C.getState();
2144 
2145   const LocationContext *LCtx = C.getLocationContext();
2146 
2147   // template <class _InputIterator, class _OutputIterator>
2148   // _OutputIterator
2149   // copy(_InputIterator __first, _InputIterator __last,
2150   //        _OutputIterator __result)
2151 
2152   // Invalidate the destination buffer
2153   const Expr *Dst = CE->getArg(2);
2154   SVal DstVal = State->getSVal(Dst, LCtx);
2155   State = InvalidateBuffer(C, State, Dst, DstVal, /*IsSource=*/false,
2156       /*Size=*/nullptr);
2157 
2158   SValBuilder &SVB = C.getSValBuilder();
2159 
2160   SVal ResultVal = SVB.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount());
2161   State = State->BindExpr(CE, LCtx, ResultVal);
2162 
2163   C.addTransition(State);
2164 }
2165 
2166 void CStringChecker::evalMemset(CheckerContext &C, const CallExpr *CE) const {
2167   // void *memset(void *s, int c, size_t n);
2168   CurrentFunctionDescription = "memory set function";
2169 
2170   DestinationArgExpr Buffer = {CE->getArg(0), 0};
2171   AnyArgExpr CharE = {CE->getArg(1), 1};
2172   SizeArgExpr Size = {CE->getArg(2), 2};
2173 
2174   ProgramStateRef State = C.getState();
2175 
2176   // See if the size argument is zero.
2177   const LocationContext *LCtx = C.getLocationContext();
2178   SVal SizeVal = C.getSVal(Size.Expression);
2179   QualType SizeTy = Size.Expression->getType();
2180 
2181   ProgramStateRef ZeroSize, NonZeroSize;
2182   std::tie(ZeroSize, NonZeroSize) = assumeZero(C, State, SizeVal, SizeTy);
2183 
2184   // Get the value of the memory area.
2185   SVal BufferPtrVal = C.getSVal(Buffer.Expression);
2186 
2187   // If the size is zero, there won't be any actual memory access, so
2188   // just bind the return value to the buffer and return.
2189   if (ZeroSize && !NonZeroSize) {
2190     ZeroSize = ZeroSize->BindExpr(CE, LCtx, BufferPtrVal);
2191     C.addTransition(ZeroSize);
2192     return;
2193   }
2194 
2195   // Ensure the memory area is not null.
2196   // If it is NULL there will be a NULL pointer dereference.
2197   State = checkNonNull(C, NonZeroSize, Buffer, BufferPtrVal);
2198   if (!State)
2199     return;
2200 
2201   State = CheckBufferAccess(C, State, Buffer, Size, AccessKind::write);
2202   if (!State)
2203     return;
2204 
2205   // According to the values of the arguments, bind the value of the second
2206   // argument to the destination buffer and set string length, or just
2207   // invalidate the destination buffer.
2208   if (!memsetAux(Buffer.Expression, C.getSVal(CharE.Expression),
2209                  Size.Expression, C, State))
2210     return;
2211 
2212   State = State->BindExpr(CE, LCtx, BufferPtrVal);
2213   C.addTransition(State);
2214 }
2215 
2216 void CStringChecker::evalBzero(CheckerContext &C, const CallExpr *CE) const {
2217   CurrentFunctionDescription = "memory clearance function";
2218 
2219   DestinationArgExpr Buffer = {CE->getArg(0), 0};
2220   SizeArgExpr Size = {CE->getArg(1), 1};
2221   SVal Zero = C.getSValBuilder().makeZeroVal(C.getASTContext().IntTy);
2222 
2223   ProgramStateRef State = C.getState();
2224 
2225   // See if the size argument is zero.
2226   SVal SizeVal = C.getSVal(Size.Expression);
2227   QualType SizeTy = Size.Expression->getType();
2228 
2229   ProgramStateRef StateZeroSize, StateNonZeroSize;
2230   std::tie(StateZeroSize, StateNonZeroSize) =
2231     assumeZero(C, State, SizeVal, SizeTy);
2232 
2233   // If the size is zero, there won't be any actual memory access,
2234   // In this case we just return.
2235   if (StateZeroSize && !StateNonZeroSize) {
2236     C.addTransition(StateZeroSize);
2237     return;
2238   }
2239 
2240   // Get the value of the memory area.
2241   SVal MemVal = C.getSVal(Buffer.Expression);
2242 
2243   // Ensure the memory area is not null.
2244   // If it is NULL there will be a NULL pointer dereference.
2245   State = checkNonNull(C, StateNonZeroSize, Buffer, MemVal);
2246   if (!State)
2247     return;
2248 
2249   State = CheckBufferAccess(C, State, Buffer, Size, AccessKind::write);
2250   if (!State)
2251     return;
2252 
2253   if (!memsetAux(Buffer.Expression, Zero, Size.Expression, C, State))
2254     return;
2255 
2256   C.addTransition(State);
2257 }
2258 
2259 //===----------------------------------------------------------------------===//
2260 // The driver method, and other Checker callbacks.
2261 //===----------------------------------------------------------------------===//
2262 
2263 CStringChecker::FnCheck CStringChecker::identifyCall(const CallEvent &Call,
2264                                                      CheckerContext &C) const {
2265   const auto *CE = dyn_cast_or_null<CallExpr>(Call.getOriginExpr());
2266   if (!CE)
2267     return nullptr;
2268 
2269   const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(Call.getDecl());
2270   if (!FD)
2271     return nullptr;
2272 
2273   if (StdCopy.matches(Call))
2274     return &CStringChecker::evalStdCopy;
2275   if (StdCopyBackward.matches(Call))
2276     return &CStringChecker::evalStdCopyBackward;
2277 
2278   // Pro-actively check that argument types are safe to do arithmetic upon.
2279   // We do not want to crash if someone accidentally passes a structure
2280   // into, say, a C++ overload of any of these functions. We could not check
2281   // that for std::copy because they may have arguments of other types.
2282   for (auto I : CE->arguments()) {
2283     QualType T = I->getType();
2284     if (!T->isIntegralOrEnumerationType() && !T->isPointerType())
2285       return nullptr;
2286   }
2287 
2288   const FnCheck *Callback = Callbacks.lookup(Call);
2289   if (Callback)
2290     return *Callback;
2291 
2292   return nullptr;
2293 }
2294 
2295 bool CStringChecker::evalCall(const CallEvent &Call, CheckerContext &C) const {
2296   FnCheck Callback = identifyCall(Call, C);
2297 
2298   // If the callee isn't a string function, let another checker handle it.
2299   if (!Callback)
2300     return false;
2301 
2302   // Check and evaluate the call.
2303   const auto *CE = cast<CallExpr>(Call.getOriginExpr());
2304   (this->*Callback)(C, CE);
2305 
2306   // If the evaluate call resulted in no change, chain to the next eval call
2307   // handler.
2308   // Note, the custom CString evaluation calls assume that basic safety
2309   // properties are held. However, if the user chooses to turn off some of these
2310   // checks, we ignore the issues and leave the call evaluation to a generic
2311   // handler.
2312   return C.isDifferent();
2313 }
2314 
2315 void CStringChecker::checkPreStmt(const DeclStmt *DS, CheckerContext &C) const {
2316   // Record string length for char a[] = "abc";
2317   ProgramStateRef state = C.getState();
2318 
2319   for (const auto *I : DS->decls()) {
2320     const VarDecl *D = dyn_cast<VarDecl>(I);
2321     if (!D)
2322       continue;
2323 
2324     // FIXME: Handle array fields of structs.
2325     if (!D->getType()->isArrayType())
2326       continue;
2327 
2328     const Expr *Init = D->getInit();
2329     if (!Init)
2330       continue;
2331     if (!isa<StringLiteral>(Init))
2332       continue;
2333 
2334     Loc VarLoc = state->getLValue(D, C.getLocationContext());
2335     const MemRegion *MR = VarLoc.getAsRegion();
2336     if (!MR)
2337       continue;
2338 
2339     SVal StrVal = C.getSVal(Init);
2340     assert(StrVal.isValid() && "Initializer string is unknown or undefined");
2341     DefinedOrUnknownSVal strLength =
2342       getCStringLength(C, state, Init, StrVal).castAs<DefinedOrUnknownSVal>();
2343 
2344     state = state->set<CStringLength>(MR, strLength);
2345   }
2346 
2347   C.addTransition(state);
2348 }
2349 
2350 ProgramStateRef
2351 CStringChecker::checkRegionChanges(ProgramStateRef state,
2352     const InvalidatedSymbols *,
2353     ArrayRef<const MemRegion *> ExplicitRegions,
2354     ArrayRef<const MemRegion *> Regions,
2355     const LocationContext *LCtx,
2356     const CallEvent *Call) const {
2357   CStringLengthTy Entries = state->get<CStringLength>();
2358   if (Entries.isEmpty())
2359     return state;
2360 
2361   llvm::SmallPtrSet<const MemRegion *, 8> Invalidated;
2362   llvm::SmallPtrSet<const MemRegion *, 32> SuperRegions;
2363 
2364   // First build sets for the changed regions and their super-regions.
2365   for (ArrayRef<const MemRegion *>::iterator
2366       I = Regions.begin(), E = Regions.end(); I != E; ++I) {
2367     const MemRegion *MR = *I;
2368     Invalidated.insert(MR);
2369 
2370     SuperRegions.insert(MR);
2371     while (const SubRegion *SR = dyn_cast<SubRegion>(MR)) {
2372       MR = SR->getSuperRegion();
2373       SuperRegions.insert(MR);
2374     }
2375   }
2376 
2377   CStringLengthTy::Factory &F = state->get_context<CStringLength>();
2378 
2379   // Then loop over the entries in the current state.
2380   for (CStringLengthTy::iterator I = Entries.begin(),
2381       E = Entries.end(); I != E; ++I) {
2382     const MemRegion *MR = I.getKey();
2383 
2384     // Is this entry for a super-region of a changed region?
2385     if (SuperRegions.count(MR)) {
2386       Entries = F.remove(Entries, MR);
2387       continue;
2388     }
2389 
2390     // Is this entry for a sub-region of a changed region?
2391     const MemRegion *Super = MR;
2392     while (const SubRegion *SR = dyn_cast<SubRegion>(Super)) {
2393       Super = SR->getSuperRegion();
2394       if (Invalidated.count(Super)) {
2395         Entries = F.remove(Entries, MR);
2396         break;
2397       }
2398     }
2399   }
2400 
2401   return state->set<CStringLength>(Entries);
2402 }
2403 
2404 void CStringChecker::checkLiveSymbols(ProgramStateRef state,
2405     SymbolReaper &SR) const {
2406   // Mark all symbols in our string length map as valid.
2407   CStringLengthTy Entries = state->get<CStringLength>();
2408 
2409   for (CStringLengthTy::iterator I = Entries.begin(), E = Entries.end();
2410       I != E; ++I) {
2411     SVal Len = I.getData();
2412 
2413     for (SymExpr::symbol_iterator si = Len.symbol_begin(),
2414         se = Len.symbol_end(); si != se; ++si)
2415       SR.markInUse(*si);
2416   }
2417 }
2418 
2419 void CStringChecker::checkDeadSymbols(SymbolReaper &SR,
2420     CheckerContext &C) const {
2421   ProgramStateRef state = C.getState();
2422   CStringLengthTy Entries = state->get<CStringLength>();
2423   if (Entries.isEmpty())
2424     return;
2425 
2426   CStringLengthTy::Factory &F = state->get_context<CStringLength>();
2427   for (CStringLengthTy::iterator I = Entries.begin(), E = Entries.end();
2428       I != E; ++I) {
2429     SVal Len = I.getData();
2430     if (SymbolRef Sym = Len.getAsSymbol()) {
2431       if (SR.isDead(Sym))
2432         Entries = F.remove(Entries, I.getKey());
2433     }
2434   }
2435 
2436   state = state->set<CStringLength>(Entries);
2437   C.addTransition(state);
2438 }
2439 
2440 void ento::registerCStringModeling(CheckerManager &Mgr) {
2441   Mgr.registerChecker<CStringChecker>();
2442 }
2443 
2444 bool ento::shouldRegisterCStringModeling(const CheckerManager &mgr) {
2445   return true;
2446 }
2447 
2448 #define REGISTER_CHECKER(name)                                                 \
2449   void ento::register##name(CheckerManager &mgr) {                             \
2450     CStringChecker *checker = mgr.getChecker<CStringChecker>();                \
2451     checker->Filter.Check##name = true;                                        \
2452     checker->Filter.CheckName##name = mgr.getCurrentCheckerName();             \
2453   }                                                                            \
2454                                                                                \
2455   bool ento::shouldRegister##name(const CheckerManager &mgr) { return true; }
2456 
2457 REGISTER_CHECKER(CStringNullArg)
2458 REGISTER_CHECKER(CStringOutOfBounds)
2459 REGISTER_CHECKER(CStringBufferOverlap)
2460 REGISTER_CHECKER(CStringNotNullTerm)
2461