2011-10-12 00:49:54 +08:00
|
|
|
//= CStringChecker.cpp - Checks calls to C string functions --------*- C++ -*-//
|
2010-07-07 07:11:01 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-07-07 07:11:01 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This defines CStringChecker, which is an assortment of checks on calls
|
|
|
|
// to functions in <string.h>.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-02-18 06:35:31 +08:00
|
|
|
#include "InterCheckerAPI.h"
|
2013-02-09 18:09:43 +08:00
|
|
|
#include "clang/Basic/CharInfo.h"
|
2020-01-30 23:04:37 +08:00
|
|
|
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
2011-03-01 09:16:21 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
2011-02-18 05:39:17 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
2021-11-16 02:10:46 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h"
|
2019-06-20 07:33:42 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
|
2011-02-24 09:05:30 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
2021-04-06 01:20:43 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/DynamicExtent.h"
|
2011-08-16 06:09:50 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
|
2012-02-04 20:31:12 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2020-04-07 01:32:16 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2012-12-02 01:12:56 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-07-07 07:11:01 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2010-07-07 07:11:01 +08:00
|
|
|
|
|
|
|
namespace {
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
struct AnyArgExpr {
|
|
|
|
// FIXME: Remove constructor in C++17 to turn it into an aggregate.
|
|
|
|
AnyArgExpr(const Expr *Expression, unsigned ArgumentIndex)
|
|
|
|
: Expression{Expression}, ArgumentIndex{ArgumentIndex} {}
|
|
|
|
const Expr *Expression;
|
|
|
|
unsigned ArgumentIndex;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SourceArgExpr : AnyArgExpr {
|
|
|
|
using AnyArgExpr::AnyArgExpr; // FIXME: Remove using in C++17.
|
|
|
|
};
|
|
|
|
|
|
|
|
struct DestinationArgExpr : AnyArgExpr {
|
|
|
|
using AnyArgExpr::AnyArgExpr; // FIXME: Same.
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SizeArgExpr : AnyArgExpr {
|
|
|
|
using AnyArgExpr::AnyArgExpr; // FIXME: Same.
|
|
|
|
};
|
|
|
|
|
|
|
|
using ErrorMessage = SmallString<128>;
|
|
|
|
enum class AccessKind { write, read };
|
|
|
|
|
|
|
|
static ErrorMessage createOutOfBoundErrorMsg(StringRef FunctionDescription,
|
|
|
|
AccessKind Access) {
|
|
|
|
ErrorMessage Message;
|
|
|
|
llvm::raw_svector_ostream Os(Message);
|
|
|
|
|
|
|
|
// Function classification like: Memory copy function
|
|
|
|
Os << toUppercase(FunctionDescription.front())
|
|
|
|
<< &FunctionDescription.data()[1];
|
|
|
|
|
|
|
|
if (Access == AccessKind::write) {
|
|
|
|
Os << " overflows the destination buffer";
|
|
|
|
} else { // read access
|
|
|
|
Os << " accesses out-of-bound array element";
|
|
|
|
}
|
|
|
|
|
|
|
|
return Message;
|
|
|
|
}
|
|
|
|
|
2019-11-08 07:58:01 +08:00
|
|
|
enum class ConcatFnKind { none = 0, strcat = 1, strlcat = 2 };
|
2011-03-01 09:16:21 +08:00
|
|
|
class CStringChecker : public Checker< eval::Call,
|
2011-02-24 09:05:30 +08:00
|
|
|
check::PreStmt<DeclStmt>,
|
|
|
|
check::LiveSymbols,
|
|
|
|
check::DeadSymbols,
|
|
|
|
check::RegionChanges
|
|
|
|
> {
|
2014-03-08 04:03:18 +08:00
|
|
|
mutable std::unique_ptr<BugType> BT_Null, BT_Bounds, BT_Overlap,
|
2022-03-03 23:32:34 +08:00
|
|
|
BT_NotCString, BT_AdditionOverflow, BT_UninitRead;
|
2012-02-07 08:56:14 +08:00
|
|
|
|
2011-06-20 10:06:40 +08:00
|
|
|
mutable const char *CurrentFunctionDescription;
|
|
|
|
|
2010-07-07 07:11:01 +08:00
|
|
|
public:
|
2012-02-07 08:56:14 +08:00
|
|
|
/// The filter is used to filter out the diagnostics which are not enabled by
|
|
|
|
/// the user.
|
|
|
|
struct CStringChecksFilter {
|
2022-04-10 20:44:11 +08:00
|
|
|
bool CheckCStringNullArg = false;
|
|
|
|
bool CheckCStringOutOfBounds = false;
|
|
|
|
bool CheckCStringBufferOverlap = false;
|
|
|
|
bool CheckCStringNotNullTerm = false;
|
|
|
|
bool CheckCStringUninitializedRead = false;
|
2014-02-12 05:49:21 +08:00
|
|
|
|
2019-09-13 03:09:24 +08:00
|
|
|
CheckerNameRef CheckNameCStringNullArg;
|
|
|
|
CheckerNameRef CheckNameCStringOutOfBounds;
|
|
|
|
CheckerNameRef CheckNameCStringBufferOverlap;
|
|
|
|
CheckerNameRef CheckNameCStringNotNullTerm;
|
2022-03-03 23:32:34 +08:00
|
|
|
CheckerNameRef CheckNameCStringUninitializedRead;
|
2012-02-07 08:56:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
CStringChecksFilter Filter;
|
|
|
|
|
2010-07-07 07:11:01 +08:00
|
|
|
static void *getTag() { static int tag; return &tag; }
|
|
|
|
|
2019-06-20 07:33:42 +08:00
|
|
|
bool evalCall(const CallEvent &Call, CheckerContext &C) const;
|
2011-02-24 09:05:30 +08:00
|
|
|
void checkPreStmt(const DeclStmt *DS, CheckerContext &C) const;
|
2012-01-27 05:29:00 +08:00
|
|
|
void checkLiveSymbols(ProgramStateRef state, SymbolReaper &SR) const;
|
2011-02-24 09:05:30 +08:00
|
|
|
void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const;
|
2010-08-15 05:02:52 +08:00
|
|
|
|
2015-09-08 11:50:52 +08:00
|
|
|
ProgramStateRef
|
2012-01-27 05:29:00 +08:00
|
|
|
checkRegionChanges(ProgramStateRef state,
|
2012-12-20 08:38:25 +08:00
|
|
|
const InvalidatedSymbols *,
|
2011-08-28 06:51:26 +08:00
|
|
|
ArrayRef<const MemRegion *> ExplicitRegions,
|
2012-02-15 05:55:24 +08:00
|
|
|
ArrayRef<const MemRegion *> Regions,
|
2017-01-13 08:50:57 +08:00
|
|
|
const LocationContext *LCtx,
|
2012-07-03 03:27:35 +08:00
|
|
|
const CallEvent *Call) const;
|
2010-07-07 07:11:01 +08:00
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
typedef void (CStringChecker::*FnCheck)(CheckerContext &,
|
|
|
|
const CallExpr *) const;
|
2019-07-02 07:02:10 +08:00
|
|
|
CallDescriptionMap<FnCheck> Callbacks = {
|
|
|
|
{{CDF_MaybeBuiltin, "memcpy", 3}, &CStringChecker::evalMemcpy},
|
|
|
|
{{CDF_MaybeBuiltin, "mempcpy", 3}, &CStringChecker::evalMempcpy},
|
|
|
|
{{CDF_MaybeBuiltin, "memcmp", 3}, &CStringChecker::evalMemcmp},
|
|
|
|
{{CDF_MaybeBuiltin, "memmove", 3}, &CStringChecker::evalMemmove},
|
|
|
|
{{CDF_MaybeBuiltin, "memset", 3}, &CStringChecker::evalMemset},
|
|
|
|
{{CDF_MaybeBuiltin, "explicit_memset", 3}, &CStringChecker::evalMemset},
|
|
|
|
{{CDF_MaybeBuiltin, "strcpy", 2}, &CStringChecker::evalStrcpy},
|
|
|
|
{{CDF_MaybeBuiltin, "strncpy", 3}, &CStringChecker::evalStrncpy},
|
|
|
|
{{CDF_MaybeBuiltin, "stpcpy", 2}, &CStringChecker::evalStpcpy},
|
|
|
|
{{CDF_MaybeBuiltin, "strlcpy", 3}, &CStringChecker::evalStrlcpy},
|
|
|
|
{{CDF_MaybeBuiltin, "strcat", 2}, &CStringChecker::evalStrcat},
|
|
|
|
{{CDF_MaybeBuiltin, "strncat", 3}, &CStringChecker::evalStrncat},
|
|
|
|
{{CDF_MaybeBuiltin, "strlcat", 3}, &CStringChecker::evalStrlcat},
|
|
|
|
{{CDF_MaybeBuiltin, "strlen", 1}, &CStringChecker::evalstrLength},
|
|
|
|
{{CDF_MaybeBuiltin, "strnlen", 2}, &CStringChecker::evalstrnLength},
|
|
|
|
{{CDF_MaybeBuiltin, "strcmp", 2}, &CStringChecker::evalStrcmp},
|
|
|
|
{{CDF_MaybeBuiltin, "strncmp", 3}, &CStringChecker::evalStrncmp},
|
|
|
|
{{CDF_MaybeBuiltin, "strcasecmp", 2}, &CStringChecker::evalStrcasecmp},
|
|
|
|
{{CDF_MaybeBuiltin, "strncasecmp", 3}, &CStringChecker::evalStrncasecmp},
|
|
|
|
{{CDF_MaybeBuiltin, "strsep", 2}, &CStringChecker::evalStrsep},
|
|
|
|
{{CDF_MaybeBuiltin, "bcopy", 3}, &CStringChecker::evalBcopy},
|
|
|
|
{{CDF_MaybeBuiltin, "bcmp", 3}, &CStringChecker::evalMemcmp},
|
|
|
|
{{CDF_MaybeBuiltin, "bzero", 2}, &CStringChecker::evalBzero},
|
|
|
|
{{CDF_MaybeBuiltin, "explicit_bzero", 2}, &CStringChecker::evalBzero},
|
|
|
|
};
|
|
|
|
|
|
|
|
// These require a bit of special handling.
|
|
|
|
CallDescription StdCopy{{"std", "copy"}, 3},
|
|
|
|
StdCopyBackward{{"std", "copy_backward"}, 3};
|
2010-07-07 07:11:01 +08:00
|
|
|
|
2019-07-02 07:02:10 +08:00
|
|
|
FnCheck identifyCall(const CallEvent &Call, CheckerContext &C) const;
|
2011-02-24 09:05:30 +08:00
|
|
|
void evalMemcpy(CheckerContext &C, const CallExpr *CE) const;
|
2011-04-01 05:36:53 +08:00
|
|
|
void evalMempcpy(CheckerContext &C, const CallExpr *CE) const;
|
2011-02-24 09:05:30 +08:00
|
|
|
void evalMemmove(CheckerContext &C, const CallExpr *CE) const;
|
|
|
|
void evalBcopy(CheckerContext &C, const CallExpr *CE) const;
|
2011-04-01 05:36:53 +08:00
|
|
|
void evalCopyCommon(CheckerContext &C, const CallExpr *CE,
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
ProgramStateRef state, SizeArgExpr Size,
|
|
|
|
DestinationArgExpr Dest, SourceArgExpr Source,
|
|
|
|
bool Restricted, bool IsMempcpy) const;
|
2010-07-09 07:57:29 +08:00
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
void evalMemcmp(CheckerContext &C, const CallExpr *CE) const;
|
2010-07-07 07:11:01 +08:00
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
void evalstrLength(CheckerContext &C, const CallExpr *CE) const;
|
|
|
|
void evalstrnLength(CheckerContext &C, const CallExpr *CE) const;
|
2011-08-16 06:09:50 +08:00
|
|
|
void evalstrLengthCommon(CheckerContext &C,
|
2015-09-08 11:50:52 +08:00
|
|
|
const CallExpr *CE,
|
2011-02-24 09:05:30 +08:00
|
|
|
bool IsStrnlen = false) const;
|
2010-07-27 09:37:31 +08:00
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
void evalStrcpy(CheckerContext &C, const CallExpr *CE) const;
|
|
|
|
void evalStrncpy(CheckerContext &C, const CallExpr *CE) const;
|
|
|
|
void evalStpcpy(CheckerContext &C, const CallExpr *CE) const;
|
2018-05-15 06:32:24 +08:00
|
|
|
void evalStrlcpy(CheckerContext &C, const CallExpr *CE) const;
|
2019-11-08 07:58:01 +08:00
|
|
|
void evalStrcpyCommon(CheckerContext &C, const CallExpr *CE, bool ReturnEnd,
|
|
|
|
bool IsBounded, ConcatFnKind appendK,
|
2018-05-15 06:32:24 +08:00
|
|
|
bool returnPtr = true) const;
|
2011-04-09 23:12:58 +08:00
|
|
|
|
|
|
|
void evalStrcat(CheckerContext &C, const CallExpr *CE) const;
|
|
|
|
void evalStrncat(CheckerContext &C, const CallExpr *CE) const;
|
2018-05-15 06:32:24 +08:00
|
|
|
void evalStrlcat(CheckerContext &C, const CallExpr *CE) const;
|
2010-08-16 15:51:42 +08:00
|
|
|
|
2011-04-13 01:08:43 +08:00
|
|
|
void evalStrcmp(CheckerContext &C, const CallExpr *CE) const;
|
2011-04-26 06:21:00 +08:00
|
|
|
void evalStrncmp(CheckerContext &C, const CallExpr *CE) const;
|
2011-04-28 23:09:11 +08:00
|
|
|
void evalStrcasecmp(CheckerContext &C, const CallExpr *CE) const;
|
2011-05-03 03:05:49 +08:00
|
|
|
void evalStrncasecmp(CheckerContext &C, const CallExpr *CE) const;
|
2011-08-16 06:09:50 +08:00
|
|
|
void evalStrcmpCommon(CheckerContext &C,
|
|
|
|
const CallExpr *CE,
|
2019-11-08 07:58:01 +08:00
|
|
|
bool IsBounded = false,
|
|
|
|
bool IgnoreCase = false) const;
|
2011-04-13 01:08:43 +08:00
|
|
|
|
2013-04-23 07:18:42 +08:00
|
|
|
void evalStrsep(CheckerContext &C, const CallExpr *CE) const;
|
|
|
|
|
2016-02-08 00:55:44 +08:00
|
|
|
void evalStdCopy(CheckerContext &C, const CallExpr *CE) const;
|
|
|
|
void evalStdCopyBackward(CheckerContext &C, const CallExpr *CE) const;
|
|
|
|
void evalStdCopyCommon(CheckerContext &C, const CallExpr *CE) const;
|
2017-06-20 14:41:06 +08:00
|
|
|
void evalMemset(CheckerContext &C, const CallExpr *CE) const;
|
2018-12-12 02:57:07 +08:00
|
|
|
void evalBzero(CheckerContext &C, const CallExpr *CE) const;
|
2016-02-08 00:55:44 +08:00
|
|
|
|
2010-07-07 07:11:01 +08:00
|
|
|
// Utility methods
|
2012-01-27 05:29:00 +08:00
|
|
|
std::pair<ProgramStateRef , ProgramStateRef >
|
2011-02-24 09:05:30 +08:00
|
|
|
static assumeZero(CheckerContext &C,
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state, SVal V, QualType Ty);
|
2011-02-24 09:05:30 +08:00
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
static ProgramStateRef setCStringLength(ProgramStateRef state,
|
2011-08-16 06:09:50 +08:00
|
|
|
const MemRegion *MR,
|
|
|
|
SVal strLength);
|
2011-02-24 09:05:30 +08:00
|
|
|
static SVal getCStringLengthForRegion(CheckerContext &C,
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef &state,
|
2011-08-16 06:09:50 +08:00
|
|
|
const Expr *Ex,
|
|
|
|
const MemRegion *MR,
|
2011-06-15 13:52:56 +08:00
|
|
|
bool hypothetical);
|
2011-08-16 06:09:50 +08:00
|
|
|
SVal getCStringLength(CheckerContext &C,
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef &state,
|
2011-08-16 06:09:50 +08:00
|
|
|
const Expr *Ex,
|
|
|
|
SVal Buf,
|
2011-06-15 13:52:56 +08:00
|
|
|
bool hypothetical = false) const;
|
2010-07-27 09:37:31 +08:00
|
|
|
|
2015-09-08 11:50:52 +08:00
|
|
|
const StringLiteral *getCStringLiteral(CheckerContext &C,
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef &state,
|
2015-09-08 11:50:52 +08:00
|
|
|
const Expr *expr,
|
2011-04-13 01:08:43 +08:00
|
|
|
SVal val) const;
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
static ProgramStateRef InvalidateBuffer(CheckerContext &C,
|
2013-11-17 17:18:48 +08:00
|
|
|
ProgramStateRef state,
|
|
|
|
const Expr *Ex, SVal V,
|
2015-09-25 00:52:56 +08:00
|
|
|
bool IsSourceBuffer,
|
|
|
|
const Expr *Size);
|
2010-08-16 15:51:42 +08:00
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
static bool SummarizeRegion(raw_ostream &os, ASTContext &Ctx,
|
2011-02-24 09:05:30 +08:00
|
|
|
const MemRegion *MR);
|
2010-07-27 09:37:31 +08:00
|
|
|
|
2018-12-12 02:57:07 +08:00
|
|
|
static bool memsetAux(const Expr *DstBuffer, SVal CharE,
|
2018-05-16 20:37:53 +08:00
|
|
|
const Expr *Size, CheckerContext &C,
|
|
|
|
ProgramStateRef &State);
|
|
|
|
|
2010-07-27 09:37:31 +08:00
|
|
|
// Re-usable checks
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
ProgramStateRef checkNonNull(CheckerContext &C, ProgramStateRef State,
|
|
|
|
AnyArgExpr Arg, SVal l) const;
|
|
|
|
ProgramStateRef CheckLocation(CheckerContext &C, ProgramStateRef state,
|
|
|
|
AnyArgExpr Buffer, SVal Element,
|
|
|
|
AccessKind Access) const;
|
|
|
|
ProgramStateRef CheckBufferAccess(CheckerContext &C, ProgramStateRef State,
|
|
|
|
AnyArgExpr Buffer, SizeArgExpr Size,
|
|
|
|
AccessKind Access) const;
|
|
|
|
ProgramStateRef CheckOverlap(CheckerContext &C, ProgramStateRef state,
|
|
|
|
SizeArgExpr Size, AnyArgExpr First,
|
|
|
|
AnyArgExpr Second) const;
|
2011-08-16 06:09:50 +08:00
|
|
|
void emitOverlapBug(CheckerContext &C,
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state,
|
2011-08-16 06:09:50 +08:00
|
|
|
const Stmt *First,
|
|
|
|
const Stmt *Second) const;
|
|
|
|
|
2018-04-23 21:36:51 +08:00
|
|
|
void emitNullArgBug(CheckerContext &C, ProgramStateRef State, const Stmt *S,
|
|
|
|
StringRef WarningMsg) const;
|
|
|
|
void emitOutOfBoundsBug(CheckerContext &C, ProgramStateRef State,
|
|
|
|
const Stmt *S, StringRef WarningMsg) const;
|
|
|
|
void emitNotCStringBug(CheckerContext &C, ProgramStateRef State,
|
|
|
|
const Stmt *S, StringRef WarningMsg) const;
|
|
|
|
void emitAdditionOverflowBug(CheckerContext &C, ProgramStateRef State) const;
|
2022-03-03 23:32:34 +08:00
|
|
|
void emitUninitializedReadBug(CheckerContext &C, ProgramStateRef State,
|
|
|
|
const Expr *E) const;
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef checkAdditionOverflow(CheckerContext &C,
|
|
|
|
ProgramStateRef state,
|
2011-08-16 06:09:50 +08:00
|
|
|
NonLoc left,
|
|
|
|
NonLoc right) const;
|
2015-09-25 00:52:56 +08:00
|
|
|
|
|
|
|
// Return true if the destination buffer of the copy function may be in bound.
|
|
|
|
// Expects SVal of Size to be positive and unsigned.
|
|
|
|
// Expects SVal of FirstBuf to be a FieldRegion.
|
|
|
|
static bool IsFirstBufInBound(CheckerContext &C,
|
|
|
|
ProgramStateRef state,
|
|
|
|
const Expr *FirstBuf,
|
|
|
|
const Expr *Size);
|
2010-07-07 07:11:01 +08:00
|
|
|
};
|
2010-08-15 05:02:52 +08:00
|
|
|
|
2010-07-07 07:11:01 +08:00
|
|
|
} //end anonymous namespace
|
|
|
|
|
2012-11-02 09:54:06 +08:00
|
|
|
REGISTER_MAP_WITH_PROGRAMSTATE(CStringLength, const MemRegion *, SVal)
|
2010-08-15 05:02:52 +08:00
|
|
|
|
2010-07-09 07:57:29 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Individual checks and utility methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
std::pair<ProgramStateRef , ProgramStateRef >
|
|
|
|
CStringChecker::assumeZero(CheckerContext &C, ProgramStateRef state, SVal V,
|
2010-07-09 07:57:29 +08:00
|
|
|
QualType Ty) {
|
2013-02-21 06:23:23 +08:00
|
|
|
Optional<DefinedSVal> val = V.getAs<DefinedSVal>();
|
2010-12-02 15:49:45 +08:00
|
|
|
if (!val)
|
2012-01-27 05:29:00 +08:00
|
|
|
return std::pair<ProgramStateRef , ProgramStateRef >(state, state);
|
2010-07-07 15:48:06 +08:00
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
SValBuilder &svalBuilder = C.getSValBuilder();
|
|
|
|
DefinedOrUnknownSVal zero = svalBuilder.makeZeroVal(Ty);
|
|
|
|
return state->assume(svalBuilder.evalEQ(state, *val, zero));
|
2010-07-09 07:57:29 +08:00
|
|
|
}
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef CStringChecker::checkNonNull(CheckerContext &C,
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
ProgramStateRef State,
|
|
|
|
AnyArgExpr Arg, SVal l) const {
|
2010-07-09 07:57:29 +08:00
|
|
|
// If a previous check has failed, propagate the failure.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
if (!State)
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2010-07-07 15:48:06 +08:00
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef stateNull, stateNonNull;
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
std::tie(stateNull, stateNonNull) =
|
|
|
|
assumeZero(C, State, l, Arg.Expression->getType());
|
2010-07-07 15:48:06 +08:00
|
|
|
|
2010-07-09 07:57:29 +08:00
|
|
|
if (stateNull && !stateNonNull) {
|
2018-04-23 21:36:51 +08:00
|
|
|
if (Filter.CheckCStringNullArg) {
|
|
|
|
SmallString<80> buf;
|
2019-09-04 01:57:01 +08:00
|
|
|
llvm::raw_svector_ostream OS(buf);
|
2018-04-23 21:36:51 +08:00
|
|
|
assert(CurrentFunctionDescription);
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
OS << "Null pointer passed as " << (Arg.ArgumentIndex + 1)
|
|
|
|
<< llvm::getOrdinalSuffix(Arg.ArgumentIndex + 1) << " argument to "
|
2019-12-11 08:48:17 +08:00
|
|
|
<< CurrentFunctionDescription;
|
2010-07-07 15:48:06 +08:00
|
|
|
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
emitNullArgBug(C, stateNull, Arg.Expression, OS.str());
|
2018-04-23 21:36:51 +08:00
|
|
|
}
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2010-07-07 15:48:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// From here on, assume that the value is non-null.
|
2010-07-09 07:57:29 +08:00
|
|
|
assert(stateNonNull);
|
|
|
|
return stateNonNull;
|
2010-07-07 15:48:06 +08:00
|
|
|
}
|
|
|
|
|
2010-07-07 07:11:01 +08:00
|
|
|
// FIXME: This was originally copied from ArrayBoundChecker.cpp. Refactor?
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef CStringChecker::CheckLocation(CheckerContext &C,
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
ProgramStateRef state,
|
|
|
|
AnyArgExpr Buffer, SVal Element,
|
|
|
|
AccessKind Access) const {
|
|
|
|
|
2010-07-09 07:57:29 +08:00
|
|
|
// If a previous check has failed, propagate the failure.
|
|
|
|
if (!state)
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2010-07-09 07:57:29 +08:00
|
|
|
|
2010-07-07 07:11:01 +08:00
|
|
|
// Check for out of bound array element access.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
const MemRegion *R = Element.getAsRegion();
|
2010-07-07 07:11:01 +08:00
|
|
|
if (!R)
|
|
|
|
return state;
|
|
|
|
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
const auto *ER = dyn_cast<ElementRegion>(R);
|
2010-07-07 07:11:01 +08:00
|
|
|
if (!ER)
|
|
|
|
return state;
|
|
|
|
|
2017-11-07 18:51:15 +08:00
|
|
|
if (ER->getValueType() != C.getASTContext().CharTy)
|
|
|
|
return state;
|
2010-07-07 07:11:01 +08:00
|
|
|
|
|
|
|
// Get the size of the array.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
const auto *superReg = cast<SubRegion>(ER->getSuperRegion());
|
2020-01-30 23:04:37 +08:00
|
|
|
DefinedOrUnknownSVal Size =
|
2021-04-06 01:20:43 +08:00
|
|
|
getDynamicExtent(state, superReg, C.getSValBuilder());
|
2010-07-07 07:11:01 +08:00
|
|
|
|
|
|
|
// Get the index of the accessed element.
|
2013-02-22 06:23:56 +08:00
|
|
|
DefinedOrUnknownSVal Idx = ER->getIndex().castAs<DefinedOrUnknownSVal>();
|
2010-07-07 07:11:01 +08:00
|
|
|
|
2022-05-02 19:32:28 +08:00
|
|
|
ProgramStateRef StInBound, StOutBound;
|
|
|
|
std::tie(StInBound, StOutBound) = state->assumeInBoundDual(Idx, Size);
|
2010-07-07 07:11:01 +08:00
|
|
|
if (StOutBound && !StInBound) {
|
2018-01-21 07:11:17 +08:00
|
|
|
// These checks are either enabled by the CString out-of-bounds checker
|
2018-07-13 21:44:44 +08:00
|
|
|
// explicitly or implicitly by the Malloc checker.
|
|
|
|
// In the latter case we only do modeling but do not emit warning.
|
|
|
|
if (!Filter.CheckCStringOutOfBounds)
|
|
|
|
return nullptr;
|
2011-06-20 10:06:40 +08:00
|
|
|
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
// Emit a bug report.
|
|
|
|
ErrorMessage Message =
|
|
|
|
createOutOfBoundErrorMsg(CurrentFunctionDescription, Access);
|
|
|
|
emitOutOfBoundsBug(C, StOutBound, Buffer.Expression, Message);
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2010-07-07 07:11:01 +08:00
|
|
|
}
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2022-03-03 23:32:34 +08:00
|
|
|
// Ensure that we wouldn't read uninitialized value.
|
|
|
|
if (Access == AccessKind::read) {
|
|
|
|
if (Filter.CheckCStringUninitializedRead &&
|
|
|
|
StInBound->getSVal(ER).isUndef()) {
|
|
|
|
emitUninitializedReadBug(C, StInBound, Buffer.Expression);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-07 07:11:01 +08:00
|
|
|
// Array bound check succeeded. From this point forward the array bound
|
|
|
|
// should always succeed.
|
|
|
|
return StInBound;
|
|
|
|
}
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef CStringChecker::CheckBufferAccess(CheckerContext &C,
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
ProgramStateRef State,
|
|
|
|
AnyArgExpr Buffer,
|
|
|
|
SizeArgExpr Size,
|
|
|
|
AccessKind Access) const {
|
2010-07-09 07:57:29 +08:00
|
|
|
// If a previous check has failed, propagate the failure.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
if (!State)
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2010-07-09 07:57:29 +08:00
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
SValBuilder &svalBuilder = C.getSValBuilder();
|
2011-06-16 13:51:02 +08:00
|
|
|
ASTContext &Ctx = svalBuilder.getContext();
|
2010-07-07 07:11:01 +08:00
|
|
|
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
QualType SizeTy = Size.Expression->getType();
|
2010-07-07 07:11:01 +08:00
|
|
|
QualType PtrTy = Ctx.getPointerType(Ctx.CharTy);
|
|
|
|
|
2010-07-07 15:48:06 +08:00
|
|
|
// Check that the first buffer is non-null.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
SVal BufVal = C.getSVal(Buffer.Expression);
|
|
|
|
State = checkNonNull(C, State, Buffer, BufVal);
|
|
|
|
if (!State)
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2010-07-07 15:48:06 +08:00
|
|
|
|
2012-02-07 08:56:14 +08:00
|
|
|
// If out-of-bounds checking is turned off, skip the rest.
|
|
|
|
if (!Filter.CheckCStringOutOfBounds)
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
return State;
|
2012-02-07 08:56:14 +08:00
|
|
|
|
2010-07-09 07:57:29 +08:00
|
|
|
// Get the access length and make sure it is known.
|
2011-06-20 10:06:40 +08:00
|
|
|
// FIXME: This assumes the caller has already checked that the access length
|
|
|
|
// is positive. And that it's unsigned.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
SVal LengthVal = C.getSVal(Size.Expression);
|
2013-02-21 06:23:23 +08:00
|
|
|
Optional<NonLoc> Length = LengthVal.getAs<NonLoc>();
|
2010-07-09 07:57:29 +08:00
|
|
|
if (!Length)
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
return State;
|
2010-07-09 07:57:29 +08:00
|
|
|
|
2010-07-07 07:11:01 +08:00
|
|
|
// Compute the offset of the last element to be accessed: size-1.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
NonLoc One = svalBuilder.makeIntVal(1, SizeTy).castAs<NonLoc>();
|
|
|
|
SVal Offset = svalBuilder.evalBinOpNN(State, BO_Sub, *Length, One, SizeTy);
|
2018-03-31 09:20:08 +08:00
|
|
|
if (Offset.isUnknown())
|
|
|
|
return nullptr;
|
|
|
|
NonLoc LastOffset = Offset.castAs<NonLoc>();
|
2010-07-07 07:11:01 +08:00
|
|
|
|
2011-04-15 13:22:18 +08:00
|
|
|
// Check that the first buffer is sufficiently long.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
SVal BufStart =
|
|
|
|
svalBuilder.evalCast(BufVal, PtrTy, Buffer.Expression->getType());
|
2013-02-21 06:23:23 +08:00
|
|
|
if (Optional<Loc> BufLoc = BufStart.getAs<Loc>()) {
|
2011-06-20 11:49:16 +08:00
|
|
|
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
SVal BufEnd =
|
|
|
|
svalBuilder.evalBinOpLN(State, BO_Add, *BufLoc, LastOffset, PtrTy);
|
|
|
|
State = CheckLocation(C, State, Buffer, BufEnd, Access);
|
2010-07-07 07:11:01 +08:00
|
|
|
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
// If the buffer isn't large enough, abort.
|
|
|
|
if (!State)
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2010-07-07 07:11:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Large enough or not, return this state!
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
return State;
|
2010-07-07 07:11:01 +08:00
|
|
|
}
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef CStringChecker::CheckOverlap(CheckerContext &C,
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
ProgramStateRef state,
|
|
|
|
SizeArgExpr Size, AnyArgExpr First,
|
|
|
|
AnyArgExpr Second) const {
|
2012-02-07 08:56:14 +08:00
|
|
|
if (!Filter.CheckCStringBufferOverlap)
|
|
|
|
return state;
|
|
|
|
|
2010-07-07 07:11:01 +08:00
|
|
|
// Do a simple check for overlap: if the two arguments are from the same
|
|
|
|
// buffer, see if the end of the first is greater than the start of the second
|
|
|
|
// or vice versa.
|
|
|
|
|
2010-07-09 07:57:29 +08:00
|
|
|
// If a previous check has failed, propagate the failure.
|
|
|
|
if (!state)
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2010-07-09 07:57:29 +08:00
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef stateTrue, stateFalse;
|
2010-07-07 07:11:01 +08:00
|
|
|
|
2022-01-25 00:54:24 +08:00
|
|
|
// Assume different address spaces cannot overlap.
|
|
|
|
if (First.Expression->getType()->getPointeeType().getAddressSpace() !=
|
|
|
|
Second.Expression->getType()->getPointeeType().getAddressSpace())
|
|
|
|
return state;
|
|
|
|
|
2010-07-07 07:11:01 +08:00
|
|
|
// Get the buffer values and make sure they're known locations.
|
2012-01-07 06:09:28 +08:00
|
|
|
const LocationContext *LCtx = C.getLocationContext();
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
SVal firstVal = state->getSVal(First.Expression, LCtx);
|
|
|
|
SVal secondVal = state->getSVal(Second.Expression, LCtx);
|
2010-07-07 07:11:01 +08:00
|
|
|
|
2013-02-21 06:23:23 +08:00
|
|
|
Optional<Loc> firstLoc = firstVal.getAs<Loc>();
|
2010-12-02 15:49:45 +08:00
|
|
|
if (!firstLoc)
|
2010-07-07 07:11:01 +08:00
|
|
|
return state;
|
|
|
|
|
2013-02-21 06:23:23 +08:00
|
|
|
Optional<Loc> secondLoc = secondVal.getAs<Loc>();
|
2010-12-02 15:49:45 +08:00
|
|
|
if (!secondLoc)
|
2010-07-07 07:11:01 +08:00
|
|
|
return state;
|
|
|
|
|
|
|
|
// Are the two values the same?
|
2015-09-08 11:50:52 +08:00
|
|
|
SValBuilder &svalBuilder = C.getSValBuilder();
|
2014-03-02 21:01:17 +08:00
|
|
|
std::tie(stateTrue, stateFalse) =
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
state->assume(svalBuilder.evalEQ(state, *firstLoc, *secondLoc));
|
2010-07-07 07:11:01 +08:00
|
|
|
|
|
|
|
if (stateTrue && !stateFalse) {
|
|
|
|
// If the values are known to be equal, that's automatically an overlap.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
emitOverlapBug(C, stateTrue, First.Expression, Second.Expression);
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2010-07-07 07:11:01 +08:00
|
|
|
}
|
|
|
|
|
2010-12-02 06:16:56 +08:00
|
|
|
// assume the two expressions are not equal.
|
2010-07-07 07:11:01 +08:00
|
|
|
assert(stateFalse);
|
|
|
|
state = stateFalse;
|
|
|
|
|
|
|
|
// Which value comes first?
|
2011-06-16 13:56:50 +08:00
|
|
|
QualType cmpTy = svalBuilder.getConditionType();
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
SVal reverse =
|
|
|
|
svalBuilder.evalBinOpLL(state, BO_GT, *firstLoc, *secondLoc, cmpTy);
|
2013-02-21 06:23:23 +08:00
|
|
|
Optional<DefinedOrUnknownSVal> reverseTest =
|
2013-02-20 13:52:05 +08:00
|
|
|
reverse.getAs<DefinedOrUnknownSVal>();
|
2010-12-02 15:49:45 +08:00
|
|
|
if (!reverseTest)
|
2010-07-07 07:11:01 +08:00
|
|
|
return state;
|
|
|
|
|
2014-03-02 21:01:17 +08:00
|
|
|
std::tie(stateTrue, stateFalse) = state->assume(*reverseTest);
|
2010-07-07 07:11:01 +08:00
|
|
|
if (stateTrue) {
|
|
|
|
if (stateFalse) {
|
|
|
|
// If we don't know which one comes first, we can't perform this test.
|
|
|
|
return state;
|
|
|
|
} else {
|
2010-12-02 15:49:45 +08:00
|
|
|
// Switch the values so that firstVal is before secondVal.
|
2013-02-20 13:52:05 +08:00
|
|
|
std::swap(firstLoc, secondLoc);
|
2010-07-07 07:11:01 +08:00
|
|
|
|
|
|
|
// Switch the Exprs as well, so that they still correspond.
|
2013-02-20 13:52:05 +08:00
|
|
|
std::swap(First, Second);
|
2010-07-07 07:11:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the length, and make sure it too is known.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
SVal LengthVal = state->getSVal(Size.Expression, LCtx);
|
2013-02-21 06:23:23 +08:00
|
|
|
Optional<NonLoc> Length = LengthVal.getAs<NonLoc>();
|
2010-07-07 07:11:01 +08:00
|
|
|
if (!Length)
|
|
|
|
return state;
|
|
|
|
|
|
|
|
// Convert the first buffer's start address to char*.
|
|
|
|
// Bail out if the cast fails.
|
2011-06-16 13:56:50 +08:00
|
|
|
ASTContext &Ctx = svalBuilder.getContext();
|
2010-07-07 07:11:01 +08:00
|
|
|
QualType CharPtrTy = Ctx.getPointerType(Ctx.CharTy);
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
SVal FirstStart =
|
|
|
|
svalBuilder.evalCast(*firstLoc, CharPtrTy, First.Expression->getType());
|
2013-02-21 06:23:23 +08:00
|
|
|
Optional<Loc> FirstStartLoc = FirstStart.getAs<Loc>();
|
2010-07-07 07:11:01 +08:00
|
|
|
if (!FirstStartLoc)
|
|
|
|
return state;
|
|
|
|
|
|
|
|
// Compute the end of the first buffer. Bail out if THAT fails.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
SVal FirstEnd = svalBuilder.evalBinOpLN(state, BO_Add, *FirstStartLoc,
|
|
|
|
*Length, CharPtrTy);
|
2013-02-21 06:23:23 +08:00
|
|
|
Optional<Loc> FirstEndLoc = FirstEnd.getAs<Loc>();
|
2010-07-07 07:11:01 +08:00
|
|
|
if (!FirstEndLoc)
|
|
|
|
return state;
|
|
|
|
|
|
|
|
// Is the end of the first buffer past the start of the second buffer?
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
SVal Overlap =
|
|
|
|
svalBuilder.evalBinOpLL(state, BO_GT, *FirstEndLoc, *secondLoc, cmpTy);
|
2013-02-21 06:23:23 +08:00
|
|
|
Optional<DefinedOrUnknownSVal> OverlapTest =
|
2013-02-20 13:52:05 +08:00
|
|
|
Overlap.getAs<DefinedOrUnknownSVal>();
|
2010-07-07 07:11:01 +08:00
|
|
|
if (!OverlapTest)
|
|
|
|
return state;
|
|
|
|
|
2014-03-02 21:01:17 +08:00
|
|
|
std::tie(stateTrue, stateFalse) = state->assume(*OverlapTest);
|
2010-07-07 07:11:01 +08:00
|
|
|
|
|
|
|
if (stateTrue && !stateFalse) {
|
|
|
|
// Overlap!
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
emitOverlapBug(C, stateTrue, First.Expression, Second.Expression);
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2010-07-07 07:11:01 +08:00
|
|
|
}
|
|
|
|
|
2010-12-02 06:16:56 +08:00
|
|
|
// assume the two expressions don't overlap.
|
2010-07-07 07:11:01 +08:00
|
|
|
assert(stateFalse);
|
|
|
|
return stateFalse;
|
|
|
|
}
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
void CStringChecker::emitOverlapBug(CheckerContext &C, ProgramStateRef state,
|
2011-02-24 09:05:30 +08:00
|
|
|
const Stmt *First, const Stmt *Second) const {
|
2015-09-17 06:03:05 +08:00
|
|
|
ExplodedNode *N = C.generateErrorNode(state);
|
2010-07-07 07:11:01 +08:00
|
|
|
if (!N)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!BT_Overlap)
|
2014-02-12 05:49:21 +08:00
|
|
|
BT_Overlap.reset(new BugType(Filter.CheckNameCStringBufferOverlap,
|
|
|
|
categories::UnixAPI, "Improper arguments"));
|
2010-07-07 07:11:01 +08:00
|
|
|
|
|
|
|
// Generate a report for this bug.
|
2019-09-10 04:34:40 +08:00
|
|
|
auto report = std::make_unique<PathSensitiveBugReport>(
|
2015-06-23 21:15:32 +08:00
|
|
|
*BT_Overlap, "Arguments must not be overlapping buffers", N);
|
2010-07-07 07:11:01 +08:00
|
|
|
report->addRange(First->getSourceRange());
|
|
|
|
report->addRange(Second->getSourceRange());
|
|
|
|
|
2015-06-23 21:15:32 +08:00
|
|
|
C.emitReport(std::move(report));
|
2010-07-07 07:11:01 +08:00
|
|
|
}
|
|
|
|
|
2018-04-23 21:36:51 +08:00
|
|
|
void CStringChecker::emitNullArgBug(CheckerContext &C, ProgramStateRef State,
|
|
|
|
const Stmt *S, StringRef WarningMsg) const {
|
|
|
|
if (ExplodedNode *N = C.generateErrorNode(State)) {
|
|
|
|
if (!BT_Null)
|
|
|
|
BT_Null.reset(new BuiltinBug(
|
|
|
|
Filter.CheckNameCStringNullArg, categories::UnixAPI,
|
|
|
|
"Null pointer argument in call to byte string function"));
|
|
|
|
|
|
|
|
BuiltinBug *BT = static_cast<BuiltinBug *>(BT_Null.get());
|
2019-09-10 04:34:40 +08:00
|
|
|
auto Report = std::make_unique<PathSensitiveBugReport>(*BT, WarningMsg, N);
|
2018-07-31 07:44:37 +08:00
|
|
|
Report->addRange(S->getSourceRange());
|
2018-10-24 02:24:53 +08:00
|
|
|
if (const auto *Ex = dyn_cast<Expr>(S))
|
|
|
|
bugreporter::trackExpressionValue(N, Ex, *Report);
|
2018-04-23 21:36:51 +08:00
|
|
|
C.emitReport(std::move(Report));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-03 23:32:34 +08:00
|
|
|
void CStringChecker::emitUninitializedReadBug(CheckerContext &C,
|
|
|
|
ProgramStateRef State,
|
|
|
|
const Expr *E) const {
|
|
|
|
if (ExplodedNode *N = C.generateErrorNode(State)) {
|
|
|
|
const char *Msg =
|
|
|
|
"Bytes string function accesses uninitialized/garbage values";
|
|
|
|
if (!BT_UninitRead)
|
|
|
|
BT_UninitRead.reset(
|
|
|
|
new BuiltinBug(Filter.CheckNameCStringUninitializedRead,
|
|
|
|
"Accessing unitialized/garbage values", Msg));
|
|
|
|
|
|
|
|
BuiltinBug *BT = static_cast<BuiltinBug *>(BT_UninitRead.get());
|
|
|
|
|
|
|
|
auto Report = std::make_unique<PathSensitiveBugReport>(*BT, Msg, N);
|
|
|
|
Report->addRange(E->getSourceRange());
|
|
|
|
bugreporter::trackExpressionValue(N, E, *Report);
|
|
|
|
C.emitReport(std::move(Report));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-23 21:36:51 +08:00
|
|
|
void CStringChecker::emitOutOfBoundsBug(CheckerContext &C,
|
|
|
|
ProgramStateRef State, const Stmt *S,
|
|
|
|
StringRef WarningMsg) const {
|
|
|
|
if (ExplodedNode *N = C.generateErrorNode(State)) {
|
|
|
|
if (!BT_Bounds)
|
|
|
|
BT_Bounds.reset(new BuiltinBug(
|
|
|
|
Filter.CheckCStringOutOfBounds ? Filter.CheckNameCStringOutOfBounds
|
|
|
|
: Filter.CheckNameCStringNullArg,
|
|
|
|
"Out-of-bound array access",
|
|
|
|
"Byte string function accesses out-of-bound array element"));
|
|
|
|
|
|
|
|
BuiltinBug *BT = static_cast<BuiltinBug *>(BT_Bounds.get());
|
|
|
|
|
|
|
|
// FIXME: It would be nice to eventually make this diagnostic more clear,
|
|
|
|
// e.g., by referencing the original declaration or by saying *why* this
|
|
|
|
// reference is outside the range.
|
2019-09-10 04:34:40 +08:00
|
|
|
auto Report = std::make_unique<PathSensitiveBugReport>(*BT, WarningMsg, N);
|
2018-04-23 21:36:51 +08:00
|
|
|
Report->addRange(S->getSourceRange());
|
|
|
|
C.emitReport(std::move(Report));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStringChecker::emitNotCStringBug(CheckerContext &C, ProgramStateRef State,
|
|
|
|
const Stmt *S,
|
|
|
|
StringRef WarningMsg) const {
|
|
|
|
if (ExplodedNode *N = C.generateNonFatalErrorNode(State)) {
|
|
|
|
if (!BT_NotCString)
|
|
|
|
BT_NotCString.reset(new BuiltinBug(
|
|
|
|
Filter.CheckNameCStringNotNullTerm, categories::UnixAPI,
|
|
|
|
"Argument is not a null-terminated string."));
|
|
|
|
|
2019-09-10 04:34:40 +08:00
|
|
|
auto Report =
|
|
|
|
std::make_unique<PathSensitiveBugReport>(*BT_NotCString, WarningMsg, N);
|
2018-04-23 21:36:51 +08:00
|
|
|
|
|
|
|
Report->addRange(S->getSourceRange());
|
|
|
|
C.emitReport(std::move(Report));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStringChecker::emitAdditionOverflowBug(CheckerContext &C,
|
|
|
|
ProgramStateRef State) const {
|
|
|
|
if (ExplodedNode *N = C.generateErrorNode(State)) {
|
2022-02-14 23:24:21 +08:00
|
|
|
if (!BT_AdditionOverflow)
|
|
|
|
BT_AdditionOverflow.reset(
|
2018-04-23 21:36:51 +08:00
|
|
|
new BuiltinBug(Filter.CheckNameCStringOutOfBounds, "API",
|
|
|
|
"Sum of expressions causes overflow."));
|
|
|
|
|
|
|
|
// This isn't a great error message, but this should never occur in real
|
|
|
|
// code anyway -- you'd have to create a buffer longer than a size_t can
|
|
|
|
// represent, which is sort of a contradiction.
|
|
|
|
const char *WarningMsg =
|
|
|
|
"This expression will create a string whose length is too big to "
|
|
|
|
"be represented as a size_t";
|
|
|
|
|
2022-02-14 23:24:21 +08:00
|
|
|
auto Report = std::make_unique<PathSensitiveBugReport>(*BT_AdditionOverflow,
|
|
|
|
WarningMsg, N);
|
2018-04-23 21:36:51 +08:00
|
|
|
C.emitReport(std::move(Report));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef CStringChecker::checkAdditionOverflow(CheckerContext &C,
|
|
|
|
ProgramStateRef state,
|
2011-06-15 13:52:56 +08:00
|
|
|
NonLoc left,
|
|
|
|
NonLoc right) const {
|
2012-02-07 08:56:14 +08:00
|
|
|
// If out-of-bounds checking is turned off, skip the rest.
|
|
|
|
if (!Filter.CheckCStringOutOfBounds)
|
|
|
|
return state;
|
|
|
|
|
2011-06-15 13:52:56 +08:00
|
|
|
// If a previous check has failed, propagate the failure.
|
|
|
|
if (!state)
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2011-06-15 13:52:56 +08:00
|
|
|
|
|
|
|
SValBuilder &svalBuilder = C.getSValBuilder();
|
|
|
|
BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
|
|
|
|
|
|
|
|
QualType sizeTy = svalBuilder.getContext().getSizeType();
|
|
|
|
const llvm::APSInt &maxValInt = BVF.getMaxValue(sizeTy);
|
|
|
|
NonLoc maxVal = svalBuilder.makeIntVal(maxValInt);
|
|
|
|
|
2011-12-12 02:43:40 +08:00
|
|
|
SVal maxMinusRight;
|
2022-06-15 22:58:08 +08:00
|
|
|
if (isa<nonloc::ConcreteInt>(right)) {
|
2011-12-12 02:43:40 +08:00
|
|
|
maxMinusRight = svalBuilder.evalBinOpNN(state, BO_Sub, maxVal, right,
|
|
|
|
sizeTy);
|
|
|
|
} else {
|
2011-06-15 13:52:56 +08:00
|
|
|
// Try switching the operands. (The order of these two assignments is
|
|
|
|
// important!)
|
2015-09-08 11:50:52 +08:00
|
|
|
maxMinusRight = svalBuilder.evalBinOpNN(state, BO_Sub, maxVal, left,
|
2011-06-15 13:52:56 +08:00
|
|
|
sizeTy);
|
|
|
|
left = right;
|
|
|
|
}
|
|
|
|
|
2013-02-21 06:23:23 +08:00
|
|
|
if (Optional<NonLoc> maxMinusRightNL = maxMinusRight.getAs<NonLoc>()) {
|
2011-06-15 13:52:56 +08:00
|
|
|
QualType cmpTy = svalBuilder.getConditionType();
|
|
|
|
// If left > max - right, we have an overflow.
|
|
|
|
SVal willOverflow = svalBuilder.evalBinOpNN(state, BO_GT, left,
|
|
|
|
*maxMinusRightNL, cmpTy);
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef stateOverflow, stateOkay;
|
2014-03-02 21:01:17 +08:00
|
|
|
std::tie(stateOverflow, stateOkay) =
|
2013-02-20 13:52:05 +08:00
|
|
|
state->assume(willOverflow.castAs<DefinedOrUnknownSVal>());
|
2011-06-15 13:52:56 +08:00
|
|
|
|
|
|
|
if (stateOverflow && !stateOkay) {
|
|
|
|
// We have an overflow. Emit a bug report.
|
2018-04-23 21:36:51 +08:00
|
|
|
emitAdditionOverflowBug(C, stateOverflow);
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2011-06-15 13:52:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// From now on, assume an overflow didn't occur.
|
|
|
|
assert(stateOkay);
|
|
|
|
state = stateOkay;
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef CStringChecker::setCStringLength(ProgramStateRef state,
|
2010-08-16 15:51:42 +08:00
|
|
|
const MemRegion *MR,
|
2010-12-02 15:49:45 +08:00
|
|
|
SVal strLength) {
|
|
|
|
assert(!strLength.isUndef() && "Attempt to set an undefined string length");
|
2010-08-16 15:51:42 +08:00
|
|
|
|
|
|
|
MR = MR->StripCasts();
|
|
|
|
|
|
|
|
switch (MR->getKind()) {
|
|
|
|
case MemRegion::StringRegionKind:
|
|
|
|
// FIXME: This can happen if we strcpy() into a string region. This is
|
|
|
|
// undefined [C99 6.4.5p6], but we should still warn about it.
|
|
|
|
return state;
|
|
|
|
|
|
|
|
case MemRegion::SymbolicRegionKind:
|
|
|
|
case MemRegion::AllocaRegionKind:
|
2020-05-11 21:00:42 +08:00
|
|
|
case MemRegion::NonParamVarRegionKind:
|
|
|
|
case MemRegion::ParamVarRegionKind:
|
2010-08-16 15:51:42 +08:00
|
|
|
case MemRegion::FieldRegionKind:
|
|
|
|
case MemRegion::ObjCIvarRegionKind:
|
2011-06-15 13:14:03 +08:00
|
|
|
// These are the types we can currently track string lengths for.
|
|
|
|
break;
|
2010-08-16 15:51:42 +08:00
|
|
|
|
|
|
|
case MemRegion::ElementRegionKind:
|
|
|
|
// FIXME: Handle element regions by upper-bounding the parent region's
|
|
|
|
// string length.
|
|
|
|
return state;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Other regions (mostly non-data) can't have a reliable C string length.
|
|
|
|
// For now, just ignore the change.
|
|
|
|
// FIXME: These are rare but not impossible. We should output some kind of
|
|
|
|
// warning for things like strcpy((char[]){'a', 0}, "b");
|
|
|
|
return state;
|
|
|
|
}
|
2011-06-15 13:14:03 +08:00
|
|
|
|
|
|
|
if (strLength.isUnknown())
|
|
|
|
return state->remove<CStringLength>(MR);
|
|
|
|
|
|
|
|
return state->set<CStringLength>(MR, strLength);
|
2010-08-16 15:51:42 +08:00
|
|
|
}
|
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
SVal CStringChecker::getCStringLengthForRegion(CheckerContext &C,
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef &state,
|
2010-08-15 05:02:52 +08:00
|
|
|
const Expr *Ex,
|
2011-06-15 13:52:56 +08:00
|
|
|
const MemRegion *MR,
|
|
|
|
bool hypothetical) {
|
|
|
|
if (!hypothetical) {
|
|
|
|
// If there's a recorded length, go ahead and return it.
|
|
|
|
const SVal *Recorded = state->get<CStringLength>(MR);
|
|
|
|
if (Recorded)
|
|
|
|
return *Recorded;
|
|
|
|
}
|
2013-08-20 00:27:34 +08:00
|
|
|
|
2010-08-15 05:02:52 +08:00
|
|
|
// Otherwise, get a new symbol and update the state.
|
2010-12-02 15:49:45 +08:00
|
|
|
SValBuilder &svalBuilder = C.getSValBuilder();
|
|
|
|
QualType sizeTy = svalBuilder.getContext().getSizeType();
|
2011-02-24 09:05:30 +08:00
|
|
|
SVal strLength = svalBuilder.getMetadataSymbolVal(CStringChecker::getTag(),
|
2012-08-22 14:26:15 +08:00
|
|
|
MR, Ex, sizeTy,
|
2016-08-17 23:37:52 +08:00
|
|
|
C.getLocationContext(),
|
2012-08-22 14:26:15 +08:00
|
|
|
C.blockCount());
|
2011-06-15 13:52:56 +08:00
|
|
|
|
2013-08-20 00:27:34 +08:00
|
|
|
if (!hypothetical) {
|
|
|
|
if (Optional<NonLoc> strLn = strLength.getAs<NonLoc>()) {
|
|
|
|
// In case of unbounded calls strlen etc bound the range to SIZE_MAX/4
|
|
|
|
BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
|
|
|
|
const llvm::APSInt &maxValInt = BVF.getMaxValue(sizeTy);
|
|
|
|
llvm::APSInt fourInt = APSIntType(maxValInt).getValue(4);
|
|
|
|
const llvm::APSInt *maxLengthInt = BVF.evalAPSInt(BO_Div, maxValInt,
|
|
|
|
fourInt);
|
|
|
|
NonLoc maxLength = svalBuilder.makeIntVal(*maxLengthInt);
|
|
|
|
SVal evalLength = svalBuilder.evalBinOpNN(state, BO_LE, *strLn,
|
|
|
|
maxLength, sizeTy);
|
|
|
|
state = state->assume(evalLength.castAs<DefinedOrUnknownSVal>(), true);
|
|
|
|
}
|
2011-06-15 13:52:56 +08:00
|
|
|
state = state->set<CStringLength>(MR, strLength);
|
2013-08-20 00:27:34 +08:00
|
|
|
}
|
2011-06-15 13:52:56 +08:00
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
return strLength;
|
2010-08-15 05:02:52 +08:00
|
|
|
}
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
SVal CStringChecker::getCStringLength(CheckerContext &C, ProgramStateRef &state,
|
2011-06-15 13:52:56 +08:00
|
|
|
const Expr *Ex, SVal Buf,
|
|
|
|
bool hypothetical) const {
|
2010-07-27 09:37:31 +08:00
|
|
|
const MemRegion *MR = Buf.getAsRegion();
|
|
|
|
if (!MR) {
|
|
|
|
// If we can't get a region, see if it's something we /know/ isn't a
|
|
|
|
// C string. In the context of locations, the only time we can issue such
|
|
|
|
// a warning is for labels.
|
2013-02-21 06:23:23 +08:00
|
|
|
if (Optional<loc::GotoLabel> Label = Buf.getAs<loc::GotoLabel>()) {
|
2018-04-23 21:36:51 +08:00
|
|
|
if (Filter.CheckCStringNotNullTerm) {
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<120> buf;
|
2010-07-27 09:37:31 +08:00
|
|
|
llvm::raw_svector_ostream os(buf);
|
2011-06-20 10:06:40 +08:00
|
|
|
assert(CurrentFunctionDescription);
|
|
|
|
os << "Argument to " << CurrentFunctionDescription
|
|
|
|
<< " is the address of the label '" << Label->getLabel()->getName()
|
2010-07-27 09:37:31 +08:00
|
|
|
<< "', which is not a null-terminated string";
|
|
|
|
|
2018-04-23 21:36:51 +08:00
|
|
|
emitNotCStringBug(C, state, Ex, os.str());
|
2010-07-27 09:37:31 +08:00
|
|
|
}
|
|
|
|
return UndefinedVal();
|
|
|
|
}
|
|
|
|
|
2010-08-15 05:02:52 +08:00
|
|
|
// If it's not a region and not a label, give up.
|
|
|
|
return UnknownVal();
|
|
|
|
}
|
2010-07-27 09:37:31 +08:00
|
|
|
|
2010-08-15 05:02:52 +08:00
|
|
|
// If we have a region, strip casts from it and see if we can figure out
|
|
|
|
// its length. For anything we can't figure out, just return UnknownVal.
|
|
|
|
MR = MR->StripCasts();
|
|
|
|
|
|
|
|
switch (MR->getKind()) {
|
|
|
|
case MemRegion::StringRegionKind: {
|
|
|
|
// Modifying the contents of string regions is undefined [C99 6.4.5p6],
|
|
|
|
// so we can assume that the byte length is the correct C string length.
|
2010-12-02 15:49:45 +08:00
|
|
|
SValBuilder &svalBuilder = C.getSValBuilder();
|
|
|
|
QualType sizeTy = svalBuilder.getContext().getSizeType();
|
|
|
|
const StringLiteral *strLit = cast<StringRegion>(MR)->getStringLiteral();
|
|
|
|
return svalBuilder.makeIntVal(strLit->getByteLength(), sizeTy);
|
2010-08-15 05:02:52 +08:00
|
|
|
}
|
|
|
|
case MemRegion::SymbolicRegionKind:
|
|
|
|
case MemRegion::AllocaRegionKind:
|
2020-05-11 21:00:42 +08:00
|
|
|
case MemRegion::NonParamVarRegionKind:
|
|
|
|
case MemRegion::ParamVarRegionKind:
|
2010-08-15 05:02:52 +08:00
|
|
|
case MemRegion::FieldRegionKind:
|
|
|
|
case MemRegion::ObjCIvarRegionKind:
|
2011-06-15 13:52:56 +08:00
|
|
|
return getCStringLengthForRegion(C, state, Ex, MR, hypothetical);
|
2010-08-15 05:02:52 +08:00
|
|
|
case MemRegion::CompoundLiteralRegionKind:
|
|
|
|
// FIXME: Can we track this? Is it necessary?
|
|
|
|
return UnknownVal();
|
|
|
|
case MemRegion::ElementRegionKind:
|
|
|
|
// FIXME: How can we handle this? It's not good enough to subtract the
|
|
|
|
// offset from the base string length; consider "123\x00567" and &a[5].
|
|
|
|
return UnknownVal();
|
|
|
|
default:
|
|
|
|
// Other regions (mostly non-data) can't have a reliable C string length.
|
|
|
|
// In this case, an error is emitted and UndefinedVal is returned.
|
|
|
|
// The caller should always be prepared to handle this case.
|
2018-04-23 21:36:51 +08:00
|
|
|
if (Filter.CheckCStringNotNullTerm) {
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<120> buf;
|
2010-08-15 05:02:52 +08:00
|
|
|
llvm::raw_svector_ostream os(buf);
|
|
|
|
|
2011-06-20 10:06:40 +08:00
|
|
|
assert(CurrentFunctionDescription);
|
|
|
|
os << "Argument to " << CurrentFunctionDescription << " is ";
|
2010-08-15 05:02:52 +08:00
|
|
|
|
|
|
|
if (SummarizeRegion(os, C.getASTContext(), MR))
|
|
|
|
os << ", which is not a null-terminated string";
|
|
|
|
else
|
|
|
|
os << "not a null-terminated string";
|
|
|
|
|
2018-04-23 21:36:51 +08:00
|
|
|
emitNotCStringBug(C, state, Ex, os.str());
|
2010-07-27 09:37:31 +08:00
|
|
|
}
|
2010-08-15 05:02:52 +08:00
|
|
|
return UndefinedVal();
|
2010-07-27 09:37:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-13 01:08:43 +08:00
|
|
|
const StringLiteral *CStringChecker::getCStringLiteral(CheckerContext &C,
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef &state, const Expr *expr, SVal val) const {
|
2011-04-13 01:08:43 +08:00
|
|
|
|
|
|
|
// Get the memory region pointed to by the val.
|
|
|
|
const MemRegion *bufRegion = val.getAsRegion();
|
|
|
|
if (!bufRegion)
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2011-04-13 01:08:43 +08:00
|
|
|
|
|
|
|
// Strip casts off the memory region.
|
|
|
|
bufRegion = bufRegion->StripCasts();
|
|
|
|
|
|
|
|
// Cast the memory region to a string region.
|
|
|
|
const StringRegion *strRegion= dyn_cast<StringRegion>(bufRegion);
|
|
|
|
if (!strRegion)
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2011-04-13 01:08:43 +08:00
|
|
|
|
|
|
|
// Return the actual string in the string region.
|
|
|
|
return strRegion->getStringLiteral();
|
|
|
|
}
|
|
|
|
|
2015-09-25 00:52:56 +08:00
|
|
|
bool CStringChecker::IsFirstBufInBound(CheckerContext &C,
|
|
|
|
ProgramStateRef state,
|
|
|
|
const Expr *FirstBuf,
|
|
|
|
const Expr *Size) {
|
|
|
|
// If we do not know that the buffer is long enough we return 'true'.
|
|
|
|
// Otherwise the parent region of this field region would also get
|
|
|
|
// invalidated, which would lead to warnings based on an unknown state.
|
|
|
|
|
|
|
|
// Originally copied from CheckBufferAccess and CheckLocation.
|
|
|
|
SValBuilder &svalBuilder = C.getSValBuilder();
|
|
|
|
ASTContext &Ctx = svalBuilder.getContext();
|
|
|
|
const LocationContext *LCtx = C.getLocationContext();
|
|
|
|
|
|
|
|
QualType sizeTy = Size->getType();
|
|
|
|
QualType PtrTy = Ctx.getPointerType(Ctx.CharTy);
|
|
|
|
SVal BufVal = state->getSVal(FirstBuf, LCtx);
|
|
|
|
|
|
|
|
SVal LengthVal = state->getSVal(Size, LCtx);
|
|
|
|
Optional<NonLoc> Length = LengthVal.getAs<NonLoc>();
|
|
|
|
if (!Length)
|
|
|
|
return true; // cf top comment.
|
|
|
|
|
|
|
|
// Compute the offset of the last element to be accessed: size-1.
|
|
|
|
NonLoc One = svalBuilder.makeIntVal(1, sizeTy).castAs<NonLoc>();
|
2018-03-31 09:20:08 +08:00
|
|
|
SVal Offset = svalBuilder.evalBinOpNN(state, BO_Sub, *Length, One, sizeTy);
|
|
|
|
if (Offset.isUnknown())
|
|
|
|
return true; // cf top comment
|
|
|
|
NonLoc LastOffset = Offset.castAs<NonLoc>();
|
2015-09-25 00:52:56 +08:00
|
|
|
|
|
|
|
// Check that the first buffer is sufficiently long.
|
|
|
|
SVal BufStart = svalBuilder.evalCast(BufVal, PtrTy, FirstBuf->getType());
|
|
|
|
Optional<Loc> BufLoc = BufStart.getAs<Loc>();
|
|
|
|
if (!BufLoc)
|
|
|
|
return true; // cf top comment.
|
|
|
|
|
|
|
|
SVal BufEnd =
|
|
|
|
svalBuilder.evalBinOpLN(state, BO_Add, *BufLoc, LastOffset, PtrTy);
|
|
|
|
|
|
|
|
// Check for out of bound array element access.
|
|
|
|
const MemRegion *R = BufEnd.getAsRegion();
|
|
|
|
if (!R)
|
|
|
|
return true; // cf top comment.
|
|
|
|
|
|
|
|
const ElementRegion *ER = dyn_cast<ElementRegion>(R);
|
|
|
|
if (!ER)
|
|
|
|
return true; // cf top comment.
|
|
|
|
|
2017-11-07 18:51:15 +08:00
|
|
|
// FIXME: Does this crash when a non-standard definition
|
|
|
|
// of a library function is encountered?
|
2015-09-25 00:52:56 +08:00
|
|
|
assert(ER->getValueType() == C.getASTContext().CharTy &&
|
|
|
|
"IsFirstBufInBound should only be called with char* ElementRegions");
|
|
|
|
|
|
|
|
// Get the size of the array.
|
|
|
|
const SubRegion *superReg = cast<SubRegion>(ER->getSuperRegion());
|
2021-04-06 01:20:43 +08:00
|
|
|
DefinedOrUnknownSVal SizeDV = getDynamicExtent(state, superReg, svalBuilder);
|
2015-09-25 00:52:56 +08:00
|
|
|
|
|
|
|
// Get the index of the accessed element.
|
|
|
|
DefinedOrUnknownSVal Idx = ER->getIndex().castAs<DefinedOrUnknownSVal>();
|
|
|
|
|
2020-01-30 23:04:37 +08:00
|
|
|
ProgramStateRef StInBound = state->assumeInBound(Idx, SizeDV, true);
|
2015-09-25 00:52:56 +08:00
|
|
|
|
|
|
|
return static_cast<bool>(StInBound);
|
|
|
|
}
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef CStringChecker::InvalidateBuffer(CheckerContext &C,
|
2013-11-17 17:18:48 +08:00
|
|
|
ProgramStateRef state,
|
|
|
|
const Expr *E, SVal V,
|
2015-09-25 00:52:56 +08:00
|
|
|
bool IsSourceBuffer,
|
|
|
|
const Expr *Size) {
|
2013-02-21 06:23:23 +08:00
|
|
|
Optional<Loc> L = V.getAs<Loc>();
|
2010-08-16 15:51:42 +08:00
|
|
|
if (!L)
|
|
|
|
return state;
|
|
|
|
|
|
|
|
// FIXME: This is a simplified version of what's in CFRefCount.cpp -- it makes
|
|
|
|
// some assumptions about the value that CFRefCount can't. Even so, it should
|
|
|
|
// probably be refactored.
|
2013-02-21 06:23:23 +08:00
|
|
|
if (Optional<loc::MemRegionVal> MR = L->getAs<loc::MemRegionVal>()) {
|
2010-08-16 15:51:42 +08:00
|
|
|
const MemRegion *R = MR->getRegion()->StripCasts();
|
|
|
|
|
|
|
|
// Are we dealing with an ElementRegion? If so, we should be invalidating
|
|
|
|
// the super-region.
|
|
|
|
if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
|
|
|
|
R = ER->getSuperRegion();
|
|
|
|
// FIXME: What about layers of ElementRegions?
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invalidate this region.
|
2012-02-18 07:13:45 +08:00
|
|
|
const LocationContext *LCtx = C.getPredecessor()->getLocationContext();
|
2013-11-17 17:18:48 +08:00
|
|
|
|
|
|
|
bool CausesPointerEscape = false;
|
|
|
|
RegionAndSymbolInvalidationTraits ITraits;
|
|
|
|
// Invalidate and escape only indirect regions accessible through the source
|
|
|
|
// buffer.
|
|
|
|
if (IsSourceBuffer) {
|
2016-04-25 22:44:25 +08:00
|
|
|
ITraits.setTrait(R->getBaseRegion(),
|
2013-11-17 17:18:48 +08:00
|
|
|
RegionAndSymbolInvalidationTraits::TK_PreserveContents);
|
|
|
|
ITraits.setTrait(R, RegionAndSymbolInvalidationTraits::TK_SuppressEscape);
|
|
|
|
CausesPointerEscape = true;
|
2015-09-25 00:52:56 +08:00
|
|
|
} else {
|
|
|
|
const MemRegion::Kind& K = R->getKind();
|
|
|
|
if (K == MemRegion::FieldRegionKind)
|
|
|
|
if (Size && IsFirstBufInBound(C, state, E, Size)) {
|
|
|
|
// If destination buffer is a field region and access is in bound,
|
|
|
|
// do not invalidate its super region.
|
|
|
|
ITraits.setTrait(
|
|
|
|
R,
|
|
|
|
RegionAndSymbolInvalidationTraits::TK_DoNotInvalidateSuperRegion);
|
|
|
|
}
|
2013-11-17 17:18:48 +08:00
|
|
|
}
|
|
|
|
|
2015-09-08 11:50:52 +08:00
|
|
|
return state->invalidateRegions(R, E, C.blockCount(), LCtx,
|
2014-05-27 10:45:47 +08:00
|
|
|
CausesPointerEscape, nullptr, nullptr,
|
|
|
|
&ITraits);
|
2010-08-16 15:51:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we have a non-region value by chance, just remove the binding.
|
|
|
|
// FIXME: is this necessary or correct? This handles the non-Region
|
|
|
|
// cases. Is it ever valid to store to these?
|
2012-08-22 14:37:46 +08:00
|
|
|
return state->killBinding(*L);
|
2010-08-16 15:51:42 +08:00
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
bool CStringChecker::SummarizeRegion(raw_ostream &os, ASTContext &Ctx,
|
2010-07-27 09:37:31 +08:00
|
|
|
const MemRegion *MR) {
|
2011-08-13 05:41:07 +08:00
|
|
|
switch (MR->getKind()) {
|
2016-01-13 21:49:29 +08:00
|
|
|
case MemRegion::FunctionCodeRegionKind: {
|
2020-01-12 20:57:01 +08:00
|
|
|
if (const auto *FD = cast<FunctionCodeRegion>(MR)->getDecl())
|
2011-10-15 02:45:37 +08:00
|
|
|
os << "the address of the function '" << *FD << '\'';
|
2010-07-27 09:37:31 +08:00
|
|
|
else
|
|
|
|
os << "the address of a function";
|
|
|
|
return true;
|
|
|
|
}
|
2016-01-13 21:49:29 +08:00
|
|
|
case MemRegion::BlockCodeRegionKind:
|
2010-07-27 09:37:31 +08:00
|
|
|
os << "block text";
|
|
|
|
return true;
|
|
|
|
case MemRegion::BlockDataRegionKind:
|
|
|
|
os << "a block";
|
|
|
|
return true;
|
|
|
|
case MemRegion::CXXThisRegionKind:
|
2010-11-26 16:52:48 +08:00
|
|
|
case MemRegion::CXXTempObjectRegionKind:
|
2020-01-12 20:57:01 +08:00
|
|
|
os << "a C++ temp object of type "
|
2022-04-21 05:09:03 +08:00
|
|
|
<< cast<TypedValueRegion>(MR)->getValueType();
|
2010-07-27 09:37:31 +08:00
|
|
|
return true;
|
2020-05-11 21:00:42 +08:00
|
|
|
case MemRegion::NonParamVarRegionKind:
|
2022-04-21 05:09:03 +08:00
|
|
|
os << "a variable of type" << cast<TypedValueRegion>(MR)->getValueType();
|
2010-07-27 09:37:31 +08:00
|
|
|
return true;
|
2020-05-11 21:00:42 +08:00
|
|
|
case MemRegion::ParamVarRegionKind:
|
2022-04-21 05:09:03 +08:00
|
|
|
os << "a parameter of type" << cast<TypedValueRegion>(MR)->getValueType();
|
2020-05-11 21:00:42 +08:00
|
|
|
return true;
|
2010-07-27 09:37:31 +08:00
|
|
|
case MemRegion::FieldRegionKind:
|
2022-04-21 05:09:03 +08:00
|
|
|
os << "a field of type " << cast<TypedValueRegion>(MR)->getValueType();
|
2010-07-27 09:37:31 +08:00
|
|
|
return true;
|
|
|
|
case MemRegion::ObjCIvarRegionKind:
|
2020-01-12 20:57:01 +08:00
|
|
|
os << "an instance variable of type "
|
2022-04-21 05:09:03 +08:00
|
|
|
<< cast<TypedValueRegion>(MR)->getValueType();
|
2010-07-27 09:37:31 +08:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-12 02:57:07 +08:00
|
|
|
bool CStringChecker::memsetAux(const Expr *DstBuffer, SVal CharVal,
|
2018-05-16 20:37:53 +08:00
|
|
|
const Expr *Size, CheckerContext &C,
|
|
|
|
ProgramStateRef &State) {
|
|
|
|
SVal MemVal = C.getSVal(DstBuffer);
|
|
|
|
SVal SizeVal = C.getSVal(Size);
|
|
|
|
const MemRegion *MR = MemVal.getAsRegion();
|
|
|
|
if (!MR)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// We're about to model memset by producing a "default binding" in the Store.
|
|
|
|
// Our current implementation - RegionStore - doesn't support default bindings
|
|
|
|
// that don't cover the whole base region. So we should first get the offset
|
|
|
|
// and the base region to figure out whether the offset of buffer is 0.
|
|
|
|
RegionOffset Offset = MR->getAsOffset();
|
|
|
|
const MemRegion *BR = Offset.getRegion();
|
|
|
|
|
|
|
|
Optional<NonLoc> SizeNL = SizeVal.getAs<NonLoc>();
|
|
|
|
if (!SizeNL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SValBuilder &svalBuilder = C.getSValBuilder();
|
|
|
|
ASTContext &Ctx = C.getASTContext();
|
|
|
|
|
|
|
|
// void *memset(void *dest, int ch, size_t count);
|
|
|
|
// For now we can only handle the case of offset is 0 and concrete char value.
|
|
|
|
if (Offset.isValid() && !Offset.hasSymbolicOffset() &&
|
|
|
|
Offset.getOffset() == 0) {
|
2020-01-30 23:04:37 +08:00
|
|
|
// Get the base region's size.
|
2021-04-06 01:20:43 +08:00
|
|
|
DefinedOrUnknownSVal SizeDV = getDynamicExtent(State, BR, svalBuilder);
|
2018-05-16 20:37:53 +08:00
|
|
|
|
|
|
|
ProgramStateRef StateWholeReg, StateNotWholeReg;
|
|
|
|
std::tie(StateWholeReg, StateNotWholeReg) =
|
2020-01-30 23:04:37 +08:00
|
|
|
State->assume(svalBuilder.evalEQ(State, SizeDV, *SizeNL));
|
2018-05-16 20:37:53 +08:00
|
|
|
|
2018-07-13 21:44:44 +08:00
|
|
|
// With the semantic of 'memset()', we should convert the CharVal to
|
2018-05-16 20:37:53 +08:00
|
|
|
// unsigned char.
|
|
|
|
CharVal = svalBuilder.evalCast(CharVal, Ctx.UnsignedCharTy, Ctx.IntTy);
|
|
|
|
|
|
|
|
ProgramStateRef StateNullChar, StateNonNullChar;
|
|
|
|
std::tie(StateNullChar, StateNonNullChar) =
|
|
|
|
assumeZero(C, State, CharVal, Ctx.UnsignedCharTy);
|
|
|
|
|
|
|
|
if (StateWholeReg && !StateNotWholeReg && StateNullChar &&
|
|
|
|
!StateNonNullChar) {
|
|
|
|
// If the 'memset()' acts on the whole region of destination buffer and
|
|
|
|
// the value of the second argument of 'memset()' is zero, bind the second
|
|
|
|
// argument's value to the destination buffer with 'default binding'.
|
|
|
|
// FIXME: Since there is no perfect way to bind the non-zero character, we
|
|
|
|
// can only deal with zero value here. In the future, we need to deal with
|
|
|
|
// the binding of non-zero value in the case of whole region.
|
|
|
|
State = State->bindDefaultZero(svalBuilder.makeLoc(BR),
|
|
|
|
C.getLocationContext());
|
|
|
|
} else {
|
|
|
|
// If the destination buffer's extent is not equal to the value of
|
|
|
|
// third argument, just invalidate buffer.
|
|
|
|
State = InvalidateBuffer(C, State, DstBuffer, MemVal,
|
|
|
|
/*IsSourceBuffer*/ false, Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (StateNullChar && !StateNonNullChar) {
|
|
|
|
// If the value of the second argument of 'memset()' is zero, set the
|
|
|
|
// string length of destination buffer to 0 directly.
|
|
|
|
State = setCStringLength(State, MR,
|
|
|
|
svalBuilder.makeZeroVal(Ctx.getSizeType()));
|
|
|
|
} else if (!StateNullChar && StateNonNullChar) {
|
|
|
|
SVal NewStrLen = svalBuilder.getMetadataSymbolVal(
|
|
|
|
CStringChecker::getTag(), MR, DstBuffer, Ctx.getSizeType(),
|
|
|
|
C.getLocationContext(), C.blockCount());
|
|
|
|
|
|
|
|
// If the value of second argument is not zero, then the string length
|
|
|
|
// is at least the size argument.
|
|
|
|
SVal NewStrLenGESize = svalBuilder.evalBinOp(
|
|
|
|
State, BO_GE, NewStrLen, SizeVal, svalBuilder.getConditionType());
|
|
|
|
|
|
|
|
State = setCStringLength(
|
|
|
|
State->assume(NewStrLenGESize.castAs<DefinedOrUnknownSVal>(), true),
|
|
|
|
MR, NewStrLen);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If the offset is not zero and char value is not concrete, we can do
|
|
|
|
// nothing but invalidate the buffer.
|
|
|
|
State = InvalidateBuffer(C, State, DstBuffer, MemVal,
|
|
|
|
/*IsSourceBuffer*/ false, Size);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-07-09 07:57:29 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2010-12-02 05:57:22 +08:00
|
|
|
// evaluation of individual function calls.
|
2010-07-09 07:57:29 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2010-07-07 07:11:01 +08:00
|
|
|
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
void CStringChecker::evalCopyCommon(CheckerContext &C, const CallExpr *CE,
|
|
|
|
ProgramStateRef state, SizeArgExpr Size,
|
|
|
|
DestinationArgExpr Dest,
|
|
|
|
SourceArgExpr Source, bool Restricted,
|
2011-04-01 05:36:53 +08:00
|
|
|
bool IsMempcpy) const {
|
2011-06-20 10:06:40 +08:00
|
|
|
CurrentFunctionDescription = "memory copy function";
|
|
|
|
|
2010-07-09 07:57:29 +08:00
|
|
|
// See if the size argument is zero.
|
2012-01-07 06:09:28 +08:00
|
|
|
const LocationContext *LCtx = C.getLocationContext();
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
SVal sizeVal = state->getSVal(Size.Expression, LCtx);
|
|
|
|
QualType sizeTy = Size.Expression->getType();
|
2010-07-09 07:57:29 +08:00
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef stateZeroSize, stateNonZeroSize;
|
2014-03-02 21:01:17 +08:00
|
|
|
std::tie(stateZeroSize, stateNonZeroSize) =
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
assumeZero(C, state, sizeVal, sizeTy);
|
2010-07-09 07:57:29 +08:00
|
|
|
|
2011-04-01 05:36:53 +08:00
|
|
|
// Get the value of the Dest.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
SVal destVal = state->getSVal(Dest.Expression, LCtx);
|
2011-04-01 05:36:53 +08:00
|
|
|
|
|
|
|
// If the size is zero, there won't be any actual memory access, so
|
|
|
|
// just bind the return value to the destination buffer and return.
|
2012-05-04 02:21:28 +08:00
|
|
|
if (stateZeroSize && !stateNonZeroSize) {
|
2012-01-07 06:09:28 +08:00
|
|
|
stateZeroSize = stateZeroSize->BindExpr(CE, LCtx, destVal);
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(stateZeroSize);
|
2012-05-04 02:21:28 +08:00
|
|
|
return;
|
2011-04-01 05:36:53 +08:00
|
|
|
}
|
2010-07-09 07:57:29 +08:00
|
|
|
|
|
|
|
// If the size can be nonzero, we have to check the other arguments.
|
2010-12-02 15:49:45 +08:00
|
|
|
if (stateNonZeroSize) {
|
2011-06-04 08:04:22 +08:00
|
|
|
state = stateNonZeroSize;
|
2011-04-01 05:36:53 +08:00
|
|
|
|
2018-03-21 08:57:37 +08:00
|
|
|
// Ensure the destination is not null. If it is NULL there will be a
|
|
|
|
// NULL pointer dereference.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
state = checkNonNull(C, state, Dest, destVal);
|
2018-03-21 08:57:37 +08:00
|
|
|
if (!state)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Get the value of the Src.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
SVal srcVal = state->getSVal(Source.Expression, LCtx);
|
2018-03-21 08:57:37 +08:00
|
|
|
|
|
|
|
// Ensure the source is not null. If it is NULL there will be a
|
|
|
|
// NULL pointer dereference.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
state = checkNonNull(C, state, Source, srcVal);
|
2018-03-21 08:57:37 +08:00
|
|
|
if (!state)
|
|
|
|
return;
|
|
|
|
|
2011-06-04 09:50:25 +08:00
|
|
|
// Ensure the accesses are valid and that the buffers do not overlap.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
state = CheckBufferAccess(C, state, Dest, Size, AccessKind::write);
|
|
|
|
state = CheckBufferAccess(C, state, Source, Size, AccessKind::read);
|
|
|
|
|
2010-07-09 07:57:29 +08:00
|
|
|
if (Restricted)
|
|
|
|
state = CheckOverlap(C, state, Size, Dest, Source);
|
2010-08-16 15:51:42 +08:00
|
|
|
|
2011-06-04 09:50:25 +08:00
|
|
|
if (!state)
|
|
|
|
return;
|
2011-06-04 09:47:27 +08:00
|
|
|
|
2015-09-08 11:50:52 +08:00
|
|
|
// If this is mempcpy, get the byte after the last byte copied and
|
2011-06-04 09:50:25 +08:00
|
|
|
// bind the expr.
|
|
|
|
if (IsMempcpy) {
|
2017-10-14 04:11:00 +08:00
|
|
|
// Get the byte after the last byte copied.
|
|
|
|
SValBuilder &SvalBuilder = C.getSValBuilder();
|
|
|
|
ASTContext &Ctx = SvalBuilder.getContext();
|
|
|
|
QualType CharPtrTy = Ctx.getPointerType(Ctx.CharTy);
|
|
|
|
SVal DestRegCharVal =
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
SvalBuilder.evalCast(destVal, CharPtrTy, Dest.Expression->getType());
|
2017-10-14 04:11:00 +08:00
|
|
|
SVal lastElement = C.getSValBuilder().evalBinOp(
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
state, BO_Add, DestRegCharVal, sizeVal, Dest.Expression->getType());
|
2017-10-14 04:11:00 +08:00
|
|
|
// If we don't know how much we copied, we can at least
|
|
|
|
// conjure a return value for later.
|
|
|
|
if (lastElement.isUnknown())
|
|
|
|
lastElement = C.getSValBuilder().conjureSymbolVal(nullptr, CE, LCtx,
|
2012-08-22 14:26:15 +08:00
|
|
|
C.blockCount());
|
2011-04-01 05:36:53 +08:00
|
|
|
|
2017-10-14 04:11:00 +08:00
|
|
|
// The byte after the last byte copied is the return value.
|
|
|
|
state = state->BindExpr(CE, LCtx, lastElement);
|
2011-06-04 09:50:25 +08:00
|
|
|
} else {
|
|
|
|
// All other copies return the destination buffer.
|
|
|
|
// (Well, bcopy() has a void return type, but this won't hurt.)
|
2012-01-07 06:09:28 +08:00
|
|
|
state = state->BindExpr(CE, LCtx, destVal);
|
2010-08-16 15:51:42 +08:00
|
|
|
}
|
2011-06-04 09:50:25 +08:00
|
|
|
|
2013-11-17 17:18:48 +08:00
|
|
|
// Invalidate the destination (regular invalidation without pointer-escaping
|
|
|
|
// the address of the top-level region).
|
2011-06-04 09:50:25 +08:00
|
|
|
// FIXME: Even if we can't perfectly model the copy, we should see if we
|
|
|
|
// can use LazyCompoundVals to copy the source values into the destination.
|
|
|
|
// This would probably remove any existing bindings past the end of the
|
|
|
|
// copied region, but that's still an improvement over blank invalidation.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
state =
|
|
|
|
InvalidateBuffer(C, state, Dest.Expression, C.getSVal(Dest.Expression),
|
|
|
|
/*IsSourceBuffer*/ false, Size.Expression);
|
2013-11-17 17:18:48 +08:00
|
|
|
|
|
|
|
// Invalidate the source (const-invalidation without const-pointer-escaping
|
|
|
|
// the address of the top-level region).
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
state = InvalidateBuffer(C, state, Source.Expression,
|
|
|
|
C.getSVal(Source.Expression),
|
|
|
|
/*IsSourceBuffer*/ true, nullptr);
|
2013-11-17 17:18:48 +08:00
|
|
|
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(state);
|
2010-07-09 07:57:29 +08:00
|
|
|
}
|
2010-07-07 07:11:01 +08:00
|
|
|
}
|
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
void CStringChecker::evalMemcpy(CheckerContext &C, const CallExpr *CE) const {
|
2010-07-09 07:57:29 +08:00
|
|
|
// void *memcpy(void *restrict dst, const void *restrict src, size_t n);
|
|
|
|
// The return value is the address of the destination buffer.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
DestinationArgExpr Dest = {CE->getArg(0), 0};
|
|
|
|
SourceArgExpr Src = {CE->getArg(1), 1};
|
|
|
|
SizeArgExpr Size = {CE->getArg(2), 2};
|
2011-06-04 09:47:27 +08:00
|
|
|
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
ProgramStateRef State = C.getState();
|
|
|
|
|
|
|
|
constexpr bool IsRestricted = true;
|
|
|
|
constexpr bool IsMempcpy = false;
|
|
|
|
evalCopyCommon(C, CE, State, Size, Dest, Src, IsRestricted, IsMempcpy);
|
2011-04-01 05:36:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CStringChecker::evalMempcpy(CheckerContext &C, const CallExpr *CE) const {
|
|
|
|
// void *mempcpy(void *restrict dst, const void *restrict src, size_t n);
|
|
|
|
// The return value is a pointer to the byte following the last written byte.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
DestinationArgExpr Dest = {CE->getArg(0), 0};
|
|
|
|
SourceArgExpr Src = {CE->getArg(1), 1};
|
|
|
|
SizeArgExpr Size = {CE->getArg(2), 2};
|
2015-09-08 11:50:52 +08:00
|
|
|
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
constexpr bool IsRestricted = true;
|
|
|
|
constexpr bool IsMempcpy = true;
|
|
|
|
evalCopyCommon(C, CE, C.getState(), Size, Dest, Src, IsRestricted, IsMempcpy);
|
2010-07-09 07:57:29 +08:00
|
|
|
}
|
2010-07-07 07:11:01 +08:00
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
void CStringChecker::evalMemmove(CheckerContext &C, const CallExpr *CE) const {
|
2010-07-09 07:57:29 +08:00
|
|
|
// void *memmove(void *dst, const void *src, size_t n);
|
2010-07-07 07:11:01 +08:00
|
|
|
// The return value is the address of the destination buffer.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
DestinationArgExpr Dest = {CE->getArg(0), 0};
|
|
|
|
SourceArgExpr Src = {CE->getArg(1), 1};
|
|
|
|
SizeArgExpr Size = {CE->getArg(2), 2};
|
2011-06-04 09:47:27 +08:00
|
|
|
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
constexpr bool IsRestricted = false;
|
|
|
|
constexpr bool IsMempcpy = false;
|
|
|
|
evalCopyCommon(C, CE, C.getState(), Size, Dest, Src, IsRestricted, IsMempcpy);
|
2010-07-07 07:11:01 +08:00
|
|
|
}
|
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
void CStringChecker::evalBcopy(CheckerContext &C, const CallExpr *CE) const {
|
2010-07-09 07:57:29 +08:00
|
|
|
// void bcopy(const void *src, void *dst, size_t n);
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
SourceArgExpr Src(CE->getArg(0), 0);
|
|
|
|
DestinationArgExpr Dest = {CE->getArg(1), 1};
|
|
|
|
SizeArgExpr Size = {CE->getArg(2), 2};
|
|
|
|
|
|
|
|
constexpr bool IsRestricted = false;
|
|
|
|
constexpr bool IsMempcpy = false;
|
|
|
|
evalCopyCommon(C, CE, C.getState(), Size, Dest, Src, IsRestricted, IsMempcpy);
|
2010-07-09 07:57:29 +08:00
|
|
|
}
|
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
void CStringChecker::evalMemcmp(CheckerContext &C, const CallExpr *CE) const {
|
2010-07-07 16:15:01 +08:00
|
|
|
// int memcmp(const void *s1, const void *s2, size_t n);
|
2011-06-20 10:06:40 +08:00
|
|
|
CurrentFunctionDescription = "memory comparison function";
|
|
|
|
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
AnyArgExpr Left = {CE->getArg(0), 0};
|
|
|
|
AnyArgExpr Right = {CE->getArg(1), 1};
|
|
|
|
SizeArgExpr Size = {CE->getArg(2), 2};
|
2010-07-07 16:15:01 +08:00
|
|
|
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
ProgramStateRef State = C.getState();
|
|
|
|
SValBuilder &Builder = C.getSValBuilder();
|
|
|
|
const LocationContext *LCtx = C.getLocationContext();
|
2010-07-07 16:15:01 +08:00
|
|
|
|
2010-07-09 07:57:29 +08:00
|
|
|
// See if the size argument is zero.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
SVal sizeVal = State->getSVal(Size.Expression, LCtx);
|
|
|
|
QualType sizeTy = Size.Expression->getType();
|
2010-07-07 16:15:01 +08:00
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef stateZeroSize, stateNonZeroSize;
|
2014-03-02 21:01:17 +08:00
|
|
|
std::tie(stateZeroSize, stateNonZeroSize) =
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
assumeZero(C, State, sizeVal, sizeTy);
|
2010-07-07 16:15:01 +08:00
|
|
|
|
2010-07-09 07:57:29 +08:00
|
|
|
// If the size can be zero, the result will be 0 in that case, and we don't
|
|
|
|
// have to check either of the buffers.
|
2010-12-02 15:49:45 +08:00
|
|
|
if (stateZeroSize) {
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
State = stateZeroSize;
|
|
|
|
State = State->BindExpr(CE, LCtx, Builder.makeZeroVal(CE->getType()));
|
|
|
|
C.addTransition(State);
|
2010-07-07 16:15:01 +08:00
|
|
|
}
|
|
|
|
|
2010-07-09 07:57:29 +08:00
|
|
|
// If the size can be nonzero, we have to check the other arguments.
|
2010-12-02 15:49:45 +08:00
|
|
|
if (stateNonZeroSize) {
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
State = stateNonZeroSize;
|
2010-07-09 07:57:29 +08:00
|
|
|
// If we know the two buffers are the same, we know the result is 0.
|
|
|
|
// First, get the two buffers' addresses. Another checker will have already
|
|
|
|
// made sure they're not undefined.
|
2012-01-07 06:09:28 +08:00
|
|
|
DefinedOrUnknownSVal LV =
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
State->getSVal(Left.Expression, LCtx).castAs<DefinedOrUnknownSVal>();
|
2012-01-07 06:09:28 +08:00
|
|
|
DefinedOrUnknownSVal RV =
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
State->getSVal(Right.Expression, LCtx).castAs<DefinedOrUnknownSVal>();
|
2010-07-09 07:57:29 +08:00
|
|
|
|
|
|
|
// See if they are the same.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
ProgramStateRef SameBuffer, NotSameBuffer;
|
|
|
|
std::tie(SameBuffer, NotSameBuffer) =
|
|
|
|
State->assume(Builder.evalEQ(State, LV, RV));
|
2010-07-09 07:57:29 +08:00
|
|
|
|
2019-12-11 10:23:39 +08:00
|
|
|
// If the two arguments are the same buffer, we know the result is 0,
|
2010-07-09 07:57:29 +08:00
|
|
|
// and we only need to check one size.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
if (SameBuffer && !NotSameBuffer) {
|
|
|
|
State = SameBuffer;
|
|
|
|
State = CheckBufferAccess(C, State, Left, Size, AccessKind::read);
|
|
|
|
if (State) {
|
|
|
|
State =
|
|
|
|
SameBuffer->BindExpr(CE, LCtx, Builder.makeZeroVal(CE->getType()));
|
|
|
|
C.addTransition(State);
|
2010-07-09 07:57:29 +08:00
|
|
|
}
|
2019-12-11 10:23:39 +08:00
|
|
|
return;
|
2010-07-09 07:57:29 +08:00
|
|
|
}
|
2010-07-07 16:15:01 +08:00
|
|
|
|
2019-12-11 10:23:39 +08:00
|
|
|
// If the two arguments might be different buffers, we have to check
|
|
|
|
// the size of both of them.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
assert(NotSameBuffer);
|
|
|
|
State = CheckBufferAccess(C, State, Right, Size, AccessKind::read);
|
|
|
|
State = CheckBufferAccess(C, State, Left, Size, AccessKind::read);
|
|
|
|
if (State) {
|
2019-12-11 10:23:39 +08:00
|
|
|
// The return value is the comparison result, which we don't know.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
SVal CmpV = Builder.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount());
|
|
|
|
State = State->BindExpr(CE, LCtx, CmpV);
|
|
|
|
C.addTransition(State);
|
2010-07-09 07:57:29 +08:00
|
|
|
}
|
|
|
|
}
|
2010-07-07 16:15:01 +08:00
|
|
|
}
|
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
void CStringChecker::evalstrLength(CheckerContext &C,
|
|
|
|
const CallExpr *CE) const {
|
2010-07-27 09:37:31 +08:00
|
|
|
// size_t strlen(const char *s);
|
2011-02-22 12:55:05 +08:00
|
|
|
evalstrLengthCommon(C, CE, /* IsStrnlen = */ false);
|
|
|
|
}
|
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
void CStringChecker::evalstrnLength(CheckerContext &C,
|
|
|
|
const CallExpr *CE) const {
|
2011-02-22 12:55:05 +08:00
|
|
|
// size_t strnlen(const char *s, size_t maxlen);
|
|
|
|
evalstrLengthCommon(C, CE, /* IsStrnlen = */ true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStringChecker::evalstrLengthCommon(CheckerContext &C, const CallExpr *CE,
|
2011-02-24 09:05:30 +08:00
|
|
|
bool IsStrnlen) const {
|
2011-06-20 10:06:40 +08:00
|
|
|
CurrentFunctionDescription = "string length function";
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state = C.getState();
|
2012-01-07 06:09:28 +08:00
|
|
|
const LocationContext *LCtx = C.getLocationContext();
|
2011-06-14 09:15:31 +08:00
|
|
|
|
|
|
|
if (IsStrnlen) {
|
|
|
|
const Expr *maxlenExpr = CE->getArg(1);
|
2012-01-07 06:09:28 +08:00
|
|
|
SVal maxlenVal = state->getSVal(maxlenExpr, LCtx);
|
2011-06-14 09:15:31 +08:00
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef stateZeroSize, stateNonZeroSize;
|
2014-03-02 21:01:17 +08:00
|
|
|
std::tie(stateZeroSize, stateNonZeroSize) =
|
2011-06-14 09:15:31 +08:00
|
|
|
assumeZero(C, state, maxlenVal, maxlenExpr->getType());
|
|
|
|
|
|
|
|
// If the size can be zero, the result will be 0 in that case, and we don't
|
|
|
|
// have to check the string itself.
|
|
|
|
if (stateZeroSize) {
|
|
|
|
SVal zero = C.getSValBuilder().makeZeroVal(CE->getType());
|
2012-01-07 06:09:28 +08:00
|
|
|
stateZeroSize = stateZeroSize->BindExpr(CE, LCtx, zero);
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(stateZeroSize);
|
2011-06-14 09:15:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the size is GUARANTEED to be zero, we're done!
|
|
|
|
if (!stateNonZeroSize)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Otherwise, record the assumption that the size is nonzero.
|
|
|
|
state = stateNonZeroSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the string argument is non-null.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
AnyArgExpr Arg = {CE->getArg(0), 0};
|
|
|
|
SVal ArgVal = state->getSVal(Arg.Expression, LCtx);
|
|
|
|
state = checkNonNull(C, state, Arg, ArgVal);
|
2010-07-27 09:37:31 +08:00
|
|
|
|
2011-06-14 09:26:48 +08:00
|
|
|
if (!state)
|
|
|
|
return;
|
2010-08-15 05:02:52 +08:00
|
|
|
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
SVal strLength = getCStringLength(C, state, Arg.Expression, ArgVal);
|
2010-08-15 05:02:52 +08:00
|
|
|
|
2011-06-14 09:26:48 +08:00
|
|
|
// If the argument isn't a valid C string, there's no valid state to
|
|
|
|
// transition to.
|
|
|
|
if (strLength.isUndef())
|
|
|
|
return;
|
2011-02-22 12:55:05 +08:00
|
|
|
|
2011-06-14 09:26:48 +08:00
|
|
|
DefinedOrUnknownSVal result = UnknownVal();
|
2011-06-14 09:15:31 +08:00
|
|
|
|
2011-06-14 09:26:48 +08:00
|
|
|
// If the check is for strnlen() then bind the return value to no more than
|
|
|
|
// the maxlen value.
|
|
|
|
if (IsStrnlen) {
|
2011-06-16 13:56:50 +08:00
|
|
|
QualType cmpTy = C.getSValBuilder().getConditionType();
|
2011-06-14 09:15:31 +08:00
|
|
|
|
2011-06-14 09:26:48 +08:00
|
|
|
// It's a little unfortunate to be getting this again,
|
|
|
|
// but it's not that expensive...
|
|
|
|
const Expr *maxlenExpr = CE->getArg(1);
|
2012-01-07 06:09:28 +08:00
|
|
|
SVal maxlenVal = state->getSVal(maxlenExpr, LCtx);
|
2011-06-14 09:26:48 +08:00
|
|
|
|
2013-02-21 06:23:23 +08:00
|
|
|
Optional<NonLoc> strLengthNL = strLength.getAs<NonLoc>();
|
|
|
|
Optional<NonLoc> maxlenValNL = maxlenVal.getAs<NonLoc>();
|
2011-06-14 09:26:48 +08:00
|
|
|
|
|
|
|
if (strLengthNL && maxlenValNL) {
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef stateStringTooLong, stateStringNotTooLong;
|
2011-06-14 09:26:48 +08:00
|
|
|
|
|
|
|
// Check if the strLength is greater than the maxlen.
|
2014-03-02 21:01:17 +08:00
|
|
|
std::tie(stateStringTooLong, stateStringNotTooLong) = state->assume(
|
|
|
|
C.getSValBuilder()
|
|
|
|
.evalBinOpNN(state, BO_GT, *strLengthNL, *maxlenValNL, cmpTy)
|
|
|
|
.castAs<DefinedOrUnknownSVal>());
|
2011-06-14 09:26:48 +08:00
|
|
|
|
|
|
|
if (stateStringTooLong && !stateStringNotTooLong) {
|
|
|
|
// If the string is longer than maxlen, return maxlen.
|
|
|
|
result = *maxlenValNL;
|
|
|
|
} else if (stateStringNotTooLong && !stateStringTooLong) {
|
|
|
|
// If the string is shorter than maxlen, return its length.
|
|
|
|
result = *strLengthNL;
|
2011-06-14 09:15:31 +08:00
|
|
|
}
|
2010-07-27 09:37:31 +08:00
|
|
|
}
|
2010-08-15 05:02:52 +08:00
|
|
|
|
2011-06-14 09:26:48 +08:00
|
|
|
if (result.isUnknown()) {
|
|
|
|
// If we don't have enough information for a comparison, there's
|
|
|
|
// no guarantee the full string length will actually be returned.
|
|
|
|
// All we know is the return value is the min of the string length
|
|
|
|
// and the limit. This is better than nothing.
|
2014-05-27 10:45:47 +08:00
|
|
|
result = C.getSValBuilder().conjureSymbolVal(nullptr, CE, LCtx,
|
|
|
|
C.blockCount());
|
2013-02-20 13:52:05 +08:00
|
|
|
NonLoc resultNL = result.castAs<NonLoc>();
|
2011-06-14 09:26:48 +08:00
|
|
|
|
|
|
|
if (strLengthNL) {
|
2013-02-20 13:52:05 +08:00
|
|
|
state = state->assume(C.getSValBuilder().evalBinOpNN(
|
|
|
|
state, BO_LE, resultNL, *strLengthNL, cmpTy)
|
|
|
|
.castAs<DefinedOrUnknownSVal>(), true);
|
2011-06-14 09:26:48 +08:00
|
|
|
}
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2011-06-14 09:26:48 +08:00
|
|
|
if (maxlenValNL) {
|
2013-02-20 13:52:05 +08:00
|
|
|
state = state->assume(C.getSValBuilder().evalBinOpNN(
|
|
|
|
state, BO_LE, resultNL, *maxlenValNL, cmpTy)
|
|
|
|
.castAs<DefinedOrUnknownSVal>(), true);
|
2011-06-14 09:26:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// This is a plain strlen(), not strnlen().
|
2013-02-20 13:52:05 +08:00
|
|
|
result = strLength.castAs<DefinedOrUnknownSVal>();
|
2011-06-14 09:26:48 +08:00
|
|
|
|
|
|
|
// If we don't know the length of the string, conjure a return
|
|
|
|
// value, so it can be used in constraints, at least.
|
|
|
|
if (result.isUnknown()) {
|
2014-05-27 10:45:47 +08:00
|
|
|
result = C.getSValBuilder().conjureSymbolVal(nullptr, CE, LCtx,
|
|
|
|
C.blockCount());
|
2011-06-14 09:26:48 +08:00
|
|
|
}
|
2010-07-27 09:37:31 +08:00
|
|
|
}
|
2011-06-14 09:26:48 +08:00
|
|
|
|
|
|
|
// Bind the return value.
|
|
|
|
assert(!result.isUnknown() && "Should have conjured a value by now");
|
2012-01-07 06:09:28 +08:00
|
|
|
state = state->BindExpr(CE, LCtx, result);
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(state);
|
2010-07-27 09:37:31 +08:00
|
|
|
}
|
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
void CStringChecker::evalStrcpy(CheckerContext &C, const CallExpr *CE) const {
|
2010-08-16 15:51:42 +08:00
|
|
|
// char *strcpy(char *restrict dst, const char *restrict src);
|
2015-09-08 11:50:52 +08:00
|
|
|
evalStrcpyCommon(C, CE,
|
2019-11-08 07:58:01 +08:00
|
|
|
/* ReturnEnd = */ false,
|
|
|
|
/* IsBounded = */ false,
|
|
|
|
/* appendK = */ ConcatFnKind::none);
|
2011-02-22 12:58:34 +08:00
|
|
|
}
|
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
void CStringChecker::evalStrncpy(CheckerContext &C, const CallExpr *CE) const {
|
2011-06-04 08:05:23 +08:00
|
|
|
// char *strncpy(char *restrict dst, const char *restrict src, size_t n);
|
2015-09-08 11:50:52 +08:00
|
|
|
evalStrcpyCommon(C, CE,
|
2019-11-08 07:58:01 +08:00
|
|
|
/* ReturnEnd = */ false,
|
|
|
|
/* IsBounded = */ true,
|
|
|
|
/* appendK = */ ConcatFnKind::none);
|
2010-08-16 15:51:42 +08:00
|
|
|
}
|
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
void CStringChecker::evalStpcpy(CheckerContext &C, const CallExpr *CE) const {
|
2010-08-16 15:51:42 +08:00
|
|
|
// char *stpcpy(char *restrict dst, const char *restrict src);
|
2015-09-08 11:50:52 +08:00
|
|
|
evalStrcpyCommon(C, CE,
|
2019-11-08 07:58:01 +08:00
|
|
|
/* ReturnEnd = */ true,
|
|
|
|
/* IsBounded = */ false,
|
|
|
|
/* appendK = */ ConcatFnKind::none);
|
2011-04-09 23:12:58 +08:00
|
|
|
}
|
|
|
|
|
2018-05-15 06:32:24 +08:00
|
|
|
void CStringChecker::evalStrlcpy(CheckerContext &C, const CallExpr *CE) const {
|
2019-11-08 07:58:01 +08:00
|
|
|
// size_t strlcpy(char *dest, const char *src, size_t size);
|
2018-05-15 06:32:24 +08:00
|
|
|
evalStrcpyCommon(C, CE,
|
2019-11-08 07:58:01 +08:00
|
|
|
/* ReturnEnd = */ true,
|
|
|
|
/* IsBounded = */ true,
|
|
|
|
/* appendK = */ ConcatFnKind::none,
|
2018-05-15 06:32:24 +08:00
|
|
|
/* returnPtr = */ false);
|
|
|
|
}
|
|
|
|
|
2011-04-09 23:12:58 +08:00
|
|
|
void CStringChecker::evalStrcat(CheckerContext &C, const CallExpr *CE) const {
|
2019-11-08 07:58:01 +08:00
|
|
|
// char *strcat(char *restrict s1, const char *restrict s2);
|
2015-09-08 11:50:52 +08:00
|
|
|
evalStrcpyCommon(C, CE,
|
2019-11-08 07:58:01 +08:00
|
|
|
/* ReturnEnd = */ false,
|
|
|
|
/* IsBounded = */ false,
|
|
|
|
/* appendK = */ ConcatFnKind::strcat);
|
2011-04-09 23:12:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CStringChecker::evalStrncat(CheckerContext &C, const CallExpr *CE) const {
|
2021-11-20 03:50:09 +08:00
|
|
|
// char *strncat(char *restrict s1, const char *restrict s2, size_t n);
|
2015-09-08 11:50:52 +08:00
|
|
|
evalStrcpyCommon(C, CE,
|
2019-11-08 07:58:01 +08:00
|
|
|
/* ReturnEnd = */ false,
|
|
|
|
/* IsBounded = */ true,
|
|
|
|
/* appendK = */ ConcatFnKind::strcat);
|
2010-08-16 15:51:42 +08:00
|
|
|
}
|
|
|
|
|
2018-05-15 06:32:24 +08:00
|
|
|
void CStringChecker::evalStrlcat(CheckerContext &C, const CallExpr *CE) const {
|
2019-11-08 07:58:01 +08:00
|
|
|
// size_t strlcat(char *dst, const char *src, size_t size);
|
|
|
|
// It will append at most size - strlen(dst) - 1 bytes,
|
|
|
|
// NULL-terminating the result.
|
2018-05-15 06:32:24 +08:00
|
|
|
evalStrcpyCommon(C, CE,
|
2019-11-08 07:58:01 +08:00
|
|
|
/* ReturnEnd = */ false,
|
|
|
|
/* IsBounded = */ true,
|
|
|
|
/* appendK = */ ConcatFnKind::strlcat,
|
2018-05-15 06:32:24 +08:00
|
|
|
/* returnPtr = */ false);
|
|
|
|
}
|
|
|
|
|
2010-12-02 05:57:22 +08:00
|
|
|
void CStringChecker::evalStrcpyCommon(CheckerContext &C, const CallExpr *CE,
|
2019-11-08 07:58:01 +08:00
|
|
|
bool ReturnEnd, bool IsBounded,
|
|
|
|
ConcatFnKind appendK,
|
|
|
|
bool returnPtr) const {
|
2019-12-11 08:48:17 +08:00
|
|
|
if (appendK == ConcatFnKind::none)
|
|
|
|
CurrentFunctionDescription = "string copy function";
|
|
|
|
else
|
|
|
|
CurrentFunctionDescription = "string concatenation function";
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state = C.getState();
|
2012-01-07 06:09:28 +08:00
|
|
|
const LocationContext *LCtx = C.getLocationContext();
|
2010-08-16 15:51:42 +08:00
|
|
|
|
2011-04-09 23:12:58 +08:00
|
|
|
// Check that the destination is non-null.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
DestinationArgExpr Dst = {CE->getArg(0), 0};
|
|
|
|
SVal DstVal = state->getSVal(Dst.Expression, LCtx);
|
|
|
|
state = checkNonNull(C, state, Dst, DstVal);
|
2010-08-16 15:51:42 +08:00
|
|
|
if (!state)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Check that the source is non-null.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
SourceArgExpr srcExpr = {CE->getArg(1), 1};
|
|
|
|
SVal srcVal = state->getSVal(srcExpr.Expression, LCtx);
|
|
|
|
state = checkNonNull(C, state, srcExpr, srcVal);
|
2010-08-16 15:51:42 +08:00
|
|
|
if (!state)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Get the string length of the source.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
SVal strLength = getCStringLength(C, state, srcExpr.Expression, srcVal);
|
2019-11-08 07:58:01 +08:00
|
|
|
Optional<NonLoc> strLengthNL = strLength.getAs<NonLoc>();
|
|
|
|
|
|
|
|
// Get the string length of the destination buffer.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
SVal dstStrLength = getCStringLength(C, state, Dst.Expression, DstVal);
|
2019-11-08 07:58:01 +08:00
|
|
|
Optional<NonLoc> dstStrLengthNL = dstStrLength.getAs<NonLoc>();
|
2010-08-16 15:51:42 +08:00
|
|
|
|
|
|
|
// If the source isn't a valid C string, give up.
|
2010-12-02 15:49:45 +08:00
|
|
|
if (strLength.isUndef())
|
2010-08-16 15:51:42 +08:00
|
|
|
return;
|
|
|
|
|
2011-06-15 13:52:56 +08:00
|
|
|
SValBuilder &svalBuilder = C.getSValBuilder();
|
|
|
|
QualType cmpTy = svalBuilder.getConditionType();
|
2011-06-21 05:55:40 +08:00
|
|
|
QualType sizeTy = svalBuilder.getContext().getSizeType();
|
2011-06-15 13:52:56 +08:00
|
|
|
|
2011-06-21 05:55:40 +08:00
|
|
|
// These two values allow checking two kinds of errors:
|
|
|
|
// - actual overflows caused by a source that doesn't fit in the destination
|
|
|
|
// - potential overflows caused by a bound that could exceed the destination
|
2011-06-15 13:52:56 +08:00
|
|
|
SVal amountCopied = UnknownVal();
|
2011-06-21 05:55:40 +08:00
|
|
|
SVal maxLastElementIndex = UnknownVal();
|
2014-05-27 10:45:47 +08:00
|
|
|
const char *boundWarning = nullptr;
|
2011-06-15 13:52:56 +08:00
|
|
|
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
// FIXME: Why do we choose the srcExpr if the access has no size?
|
|
|
|
// Note that the 3rd argument of the call would be the size parameter.
|
|
|
|
SizeArgExpr SrcExprAsSizeDummy = {srcExpr.Expression, srcExpr.ArgumentIndex};
|
|
|
|
state = CheckOverlap(
|
|
|
|
C, state,
|
|
|
|
(IsBounded ? SizeArgExpr{CE->getArg(2), 2} : SrcExprAsSizeDummy), Dst,
|
|
|
|
srcExpr);
|
2018-05-15 06:32:24 +08:00
|
|
|
|
|
|
|
if (!state)
|
|
|
|
return;
|
|
|
|
|
2011-04-09 23:12:58 +08:00
|
|
|
// If the function is strncpy, strncat, etc... it is bounded.
|
2019-11-08 07:58:01 +08:00
|
|
|
if (IsBounded) {
|
2011-04-09 23:12:58 +08:00
|
|
|
// Get the max number of characters to copy.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
SizeArgExpr lenExpr = {CE->getArg(2), 2};
|
|
|
|
SVal lenVal = state->getSVal(lenExpr.Expression, LCtx);
|
2011-02-22 12:58:34 +08:00
|
|
|
|
2011-06-15 13:52:56 +08:00
|
|
|
// Protect against misdeclared strncpy().
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
lenVal =
|
|
|
|
svalBuilder.evalCast(lenVal, sizeTy, lenExpr.Expression->getType());
|
2011-04-29 02:59:43 +08:00
|
|
|
|
2013-02-21 06:23:23 +08:00
|
|
|
Optional<NonLoc> lenValNL = lenVal.getAs<NonLoc>();
|
2011-02-22 12:58:34 +08:00
|
|
|
|
2011-06-15 13:52:56 +08:00
|
|
|
// If we know both values, we might be able to figure out how much
|
|
|
|
// we're copying.
|
|
|
|
if (strLengthNL && lenValNL) {
|
2019-11-08 07:58:01 +08:00
|
|
|
switch (appendK) {
|
|
|
|
case ConcatFnKind::none:
|
|
|
|
case ConcatFnKind::strcat: {
|
|
|
|
ProgramStateRef stateSourceTooLong, stateSourceNotTooLong;
|
|
|
|
// Check if the max number to copy is less than the length of the src.
|
|
|
|
// If the bound is equal to the source length, strncpy won't null-
|
|
|
|
// terminate the result!
|
|
|
|
std::tie(stateSourceTooLong, stateSourceNotTooLong) = state->assume(
|
|
|
|
svalBuilder
|
|
|
|
.evalBinOpNN(state, BO_GE, *strLengthNL, *lenValNL, cmpTy)
|
|
|
|
.castAs<DefinedOrUnknownSVal>());
|
|
|
|
|
|
|
|
if (stateSourceTooLong && !stateSourceNotTooLong) {
|
|
|
|
// Max number to copy is less than the length of the src, so the
|
|
|
|
// actual strLength copied is the max number arg.
|
|
|
|
state = stateSourceTooLong;
|
|
|
|
amountCopied = lenVal;
|
|
|
|
|
|
|
|
} else if (!stateSourceTooLong && stateSourceNotTooLong) {
|
|
|
|
// The source buffer entirely fits in the bound.
|
|
|
|
state = stateSourceNotTooLong;
|
|
|
|
amountCopied = strLength;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ConcatFnKind::strlcat:
|
|
|
|
if (!dstStrLengthNL)
|
|
|
|
return;
|
2011-06-15 13:52:56 +08:00
|
|
|
|
2019-11-08 07:58:01 +08:00
|
|
|
// amountCopied = min (size - dstLen - 1 , srcLen)
|
|
|
|
SVal freeSpace = svalBuilder.evalBinOpNN(state, BO_Sub, *lenValNL,
|
|
|
|
*dstStrLengthNL, sizeTy);
|
2022-06-15 22:58:08 +08:00
|
|
|
if (!isa<NonLoc>(freeSpace))
|
2019-11-08 07:58:01 +08:00
|
|
|
return;
|
|
|
|
freeSpace =
|
|
|
|
svalBuilder.evalBinOp(state, BO_Sub, freeSpace,
|
|
|
|
svalBuilder.makeIntVal(1, sizeTy), sizeTy);
|
|
|
|
Optional<NonLoc> freeSpaceNL = freeSpace.getAs<NonLoc>();
|
|
|
|
|
|
|
|
// While unlikely, it is possible that the subtraction is
|
|
|
|
// too complex to compute, let's check whether it succeeded.
|
|
|
|
if (!freeSpaceNL)
|
|
|
|
return;
|
|
|
|
SVal hasEnoughSpace = svalBuilder.evalBinOpNN(
|
|
|
|
state, BO_LE, *strLengthNL, *freeSpaceNL, cmpTy);
|
2011-06-15 13:52:56 +08:00
|
|
|
|
2019-11-08 07:58:01 +08:00
|
|
|
ProgramStateRef TrueState, FalseState;
|
|
|
|
std::tie(TrueState, FalseState) =
|
|
|
|
state->assume(hasEnoughSpace.castAs<DefinedOrUnknownSVal>());
|
2011-06-15 13:52:56 +08:00
|
|
|
|
2019-11-08 07:58:01 +08:00
|
|
|
// srcStrLength <= size - dstStrLength -1
|
|
|
|
if (TrueState && !FalseState) {
|
|
|
|
amountCopied = strLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
// srcStrLength > size - dstStrLength -1
|
|
|
|
if (!TrueState && FalseState) {
|
|
|
|
amountCopied = freeSpace;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TrueState && FalseState)
|
|
|
|
amountCopied = UnknownVal();
|
|
|
|
break;
|
2011-06-15 13:52:56 +08:00
|
|
|
}
|
|
|
|
}
|
2011-06-20 11:49:16 +08:00
|
|
|
// We still want to know if the bound is known to be too large.
|
2011-06-21 05:55:40 +08:00
|
|
|
if (lenValNL) {
|
2019-11-08 07:58:01 +08:00
|
|
|
switch (appendK) {
|
|
|
|
case ConcatFnKind::strcat:
|
2011-06-21 05:55:40 +08:00
|
|
|
// For strncat, the check is strlen(dst) + lenVal < sizeof(dst)
|
|
|
|
|
|
|
|
// Get the string length of the destination. If the destination is
|
|
|
|
// memory that can't have a string length, we shouldn't be copying
|
|
|
|
// into it anyway.
|
|
|
|
if (dstStrLength.isUndef())
|
|
|
|
return;
|
|
|
|
|
2019-11-08 07:58:01 +08:00
|
|
|
if (dstStrLengthNL) {
|
|
|
|
maxLastElementIndex = svalBuilder.evalBinOpNN(
|
|
|
|
state, BO_Add, *lenValNL, *dstStrLengthNL, sizeTy);
|
|
|
|
|
2011-06-21 05:55:40 +08:00
|
|
|
boundWarning = "Size argument is greater than the free space in the "
|
|
|
|
"destination buffer";
|
|
|
|
}
|
2019-11-08 07:58:01 +08:00
|
|
|
break;
|
|
|
|
case ConcatFnKind::none:
|
|
|
|
case ConcatFnKind::strlcat:
|
|
|
|
// For strncpy and strlcat, this is just checking
|
|
|
|
// that lenVal <= sizeof(dst).
|
2011-06-21 05:55:40 +08:00
|
|
|
// (Yes, strncpy and strncat differ in how they treat termination.
|
|
|
|
// strncat ALWAYS terminates, but strncpy doesn't.)
|
2012-05-15 01:58:35 +08:00
|
|
|
|
|
|
|
// We need a special case for when the copy size is zero, in which
|
|
|
|
// case strncpy will do no work at all. Our bounds check uses n-1
|
|
|
|
// as the last element accessed, so n == 0 is problematic.
|
|
|
|
ProgramStateRef StateZeroSize, StateNonZeroSize;
|
2014-03-02 21:01:17 +08:00
|
|
|
std::tie(StateZeroSize, StateNonZeroSize) =
|
2019-11-08 07:58:01 +08:00
|
|
|
assumeZero(C, state, *lenValNL, sizeTy);
|
2012-05-15 01:58:35 +08:00
|
|
|
|
|
|
|
// If the size is known to be zero, we're done.
|
|
|
|
if (StateZeroSize && !StateNonZeroSize) {
|
2018-05-23 12:38:25 +08:00
|
|
|
if (returnPtr) {
|
|
|
|
StateZeroSize = StateZeroSize->BindExpr(CE, LCtx, DstVal);
|
|
|
|
} else {
|
2019-11-08 07:58:01 +08:00
|
|
|
if (appendK == ConcatFnKind::none) {
|
|
|
|
// strlcpy returns strlen(src)
|
2019-12-14 09:59:36 +08:00
|
|
|
StateZeroSize = StateZeroSize->BindExpr(CE, LCtx, strLength);
|
|
|
|
} else {
|
2019-11-08 07:58:01 +08:00
|
|
|
// strlcat returns strlen(src) + strlen(dst)
|
2019-12-14 09:59:36 +08:00
|
|
|
SVal retSize = svalBuilder.evalBinOp(
|
|
|
|
state, BO_Add, strLength, dstStrLength, sizeTy);
|
|
|
|
StateZeroSize = StateZeroSize->BindExpr(CE, LCtx, retSize);
|
2019-11-08 07:58:01 +08:00
|
|
|
}
|
2018-05-23 12:38:25 +08:00
|
|
|
}
|
2012-05-15 01:58:35 +08:00
|
|
|
C.addTransition(StateZeroSize);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, go ahead and figure out the last element we'll touch.
|
|
|
|
// We don't record the non-zero assumption here because we can't
|
|
|
|
// be sure. We won't warn on a possible zero.
|
2013-02-20 13:52:05 +08:00
|
|
|
NonLoc one = svalBuilder.makeIntVal(1, sizeTy).castAs<NonLoc>();
|
2019-11-08 07:58:01 +08:00
|
|
|
maxLastElementIndex =
|
|
|
|
svalBuilder.evalBinOpNN(state, BO_Sub, *lenValNL, one, sizeTy);
|
2011-06-21 05:55:40 +08:00
|
|
|
boundWarning = "Size argument is greater than the length of the "
|
|
|
|
"destination buffer";
|
2019-11-08 07:58:01 +08:00
|
|
|
break;
|
2011-06-21 05:55:40 +08:00
|
|
|
}
|
|
|
|
}
|
2011-06-15 13:52:56 +08:00
|
|
|
} else {
|
|
|
|
// The function isn't bounded. The amount copied should match the length
|
|
|
|
// of the source buffer.
|
|
|
|
amountCopied = strLength;
|
2011-02-22 12:58:34 +08:00
|
|
|
}
|
|
|
|
|
2011-06-15 13:52:56 +08:00
|
|
|
assert(state);
|
|
|
|
|
|
|
|
// This represents the number of characters copied into the destination
|
|
|
|
// buffer. (It may not actually be the strlen if the destination buffer
|
|
|
|
// is not terminated.)
|
|
|
|
SVal finalStrLength = UnknownVal();
|
2019-11-08 07:58:01 +08:00
|
|
|
SVal strlRetVal = UnknownVal();
|
|
|
|
|
|
|
|
if (appendK == ConcatFnKind::none && !returnPtr) {
|
|
|
|
// strlcpy returns the sizeof(src)
|
|
|
|
strlRetVal = strLength;
|
|
|
|
}
|
2011-06-15 13:52:56 +08:00
|
|
|
|
2011-04-09 23:12:58 +08:00
|
|
|
// If this is an appending function (strcat, strncat...) then set the
|
|
|
|
// string length to strlen(src) + strlen(dst) since the buffer will
|
|
|
|
// ultimately contain both.
|
2019-11-08 07:58:01 +08:00
|
|
|
if (appendK != ConcatFnKind::none) {
|
2011-06-16 13:51:02 +08:00
|
|
|
// Get the string length of the destination. If the destination is memory
|
|
|
|
// that can't have a string length, we shouldn't be copying into it anyway.
|
2011-04-09 23:12:58 +08:00
|
|
|
if (dstStrLength.isUndef())
|
|
|
|
return;
|
|
|
|
|
2019-11-08 07:58:01 +08:00
|
|
|
if (appendK == ConcatFnKind::strlcat && dstStrLengthNL && strLengthNL) {
|
|
|
|
strlRetVal = svalBuilder.evalBinOpNN(state, BO_Add, *strLengthNL,
|
|
|
|
*dstStrLengthNL, sizeTy);
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional<NonLoc> amountCopiedNL = amountCopied.getAs<NonLoc>();
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2011-06-15 13:52:56 +08:00
|
|
|
// If we know both string lengths, we might know the final string length.
|
2019-11-08 07:58:01 +08:00
|
|
|
if (amountCopiedNL && dstStrLengthNL) {
|
2011-06-15 13:52:56 +08:00
|
|
|
// Make sure the two lengths together don't overflow a size_t.
|
2019-11-08 07:58:01 +08:00
|
|
|
state = checkAdditionOverflow(C, state, *amountCopiedNL, *dstStrLengthNL);
|
2011-06-15 13:52:56 +08:00
|
|
|
if (!state)
|
|
|
|
return;
|
|
|
|
|
2019-11-08 07:58:01 +08:00
|
|
|
finalStrLength = svalBuilder.evalBinOpNN(state, BO_Add, *amountCopiedNL,
|
2011-06-15 13:52:56 +08:00
|
|
|
*dstStrLengthNL, sizeTy);
|
|
|
|
}
|
2011-04-09 23:12:58 +08:00
|
|
|
|
2011-06-15 13:52:56 +08:00
|
|
|
// If we couldn't get a single value for the final string length,
|
|
|
|
// we can at least bound it by the individual lengths.
|
|
|
|
if (finalStrLength.isUnknown()) {
|
|
|
|
// Try to get a "hypothetical" string length symbol, which we can later
|
|
|
|
// set as a real value if that turns out to be the case.
|
|
|
|
finalStrLength = getCStringLength(C, state, CE, DstVal, true);
|
|
|
|
assert(!finalStrLength.isUndef());
|
|
|
|
|
2013-02-21 06:23:23 +08:00
|
|
|
if (Optional<NonLoc> finalStrLengthNL = finalStrLength.getAs<NonLoc>()) {
|
2019-11-08 07:58:01 +08:00
|
|
|
if (amountCopiedNL && appendK == ConcatFnKind::none) {
|
|
|
|
// we overwrite dst string with the src
|
2011-06-15 13:52:56 +08:00
|
|
|
// finalStrLength >= srcStrLength
|
2019-11-08 07:58:01 +08:00
|
|
|
SVal sourceInResult = svalBuilder.evalBinOpNN(
|
|
|
|
state, BO_GE, *finalStrLengthNL, *amountCopiedNL, cmpTy);
|
2013-02-20 13:52:05 +08:00
|
|
|
state = state->assume(sourceInResult.castAs<DefinedOrUnknownSVal>(),
|
2011-06-15 13:52:56 +08:00
|
|
|
true);
|
|
|
|
if (!state)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-08 07:58:01 +08:00
|
|
|
if (dstStrLengthNL && appendK != ConcatFnKind::none) {
|
|
|
|
// we extend the dst string with the src
|
2011-06-15 13:52:56 +08:00
|
|
|
// finalStrLength >= dstStrLength
|
|
|
|
SVal destInResult = svalBuilder.evalBinOpNN(state, BO_GE,
|
|
|
|
*finalStrLengthNL,
|
|
|
|
*dstStrLengthNL,
|
|
|
|
cmpTy);
|
2013-02-20 13:52:05 +08:00
|
|
|
state =
|
|
|
|
state->assume(destInResult.castAs<DefinedOrUnknownSVal>(), true);
|
2011-06-15 13:52:56 +08:00
|
|
|
if (!state)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-09 23:12:58 +08:00
|
|
|
|
2011-06-15 13:52:56 +08:00
|
|
|
} else {
|
|
|
|
// Otherwise, this is a copy-over function (strcpy, strncpy, ...), and
|
|
|
|
// the final string length will match the input string length.
|
|
|
|
finalStrLength = amountCopied;
|
2011-04-09 23:12:58 +08:00
|
|
|
}
|
|
|
|
|
2018-05-15 06:32:24 +08:00
|
|
|
SVal Result;
|
|
|
|
|
|
|
|
if (returnPtr) {
|
|
|
|
// The final result of the function will either be a pointer past the last
|
|
|
|
// copied element, or a pointer to the start of the destination buffer.
|
2019-11-08 07:58:01 +08:00
|
|
|
Result = (ReturnEnd ? UnknownVal() : DstVal);
|
2018-05-15 06:32:24 +08:00
|
|
|
} else {
|
2019-11-08 07:58:01 +08:00
|
|
|
if (appendK == ConcatFnKind::strlcat || appendK == ConcatFnKind::none)
|
|
|
|
//strlcpy, strlcat
|
|
|
|
Result = strlRetVal;
|
|
|
|
else
|
|
|
|
Result = finalStrLength;
|
2018-05-15 06:32:24 +08:00
|
|
|
}
|
2010-08-16 15:51:42 +08:00
|
|
|
|
2011-06-15 13:52:56 +08:00
|
|
|
assert(state);
|
|
|
|
|
2010-08-16 15:51:42 +08:00
|
|
|
// If the destination is a MemRegion, try to check for a buffer overflow and
|
|
|
|
// record the new string length.
|
2013-02-21 06:23:23 +08:00
|
|
|
if (Optional<loc::MemRegionVal> dstRegVal =
|
2018-05-15 06:32:24 +08:00
|
|
|
DstVal.getAs<loc::MemRegionVal>()) {
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
QualType ptrTy = Dst.Expression->getType();
|
2011-06-21 05:55:40 +08:00
|
|
|
|
|
|
|
// If we have an exact value on a bounded copy, use that to check for
|
|
|
|
// overflows, rather than our estimate about how much is actually copied.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
if (Optional<NonLoc> maxLastNL = maxLastElementIndex.getAs<NonLoc>()) {
|
|
|
|
SVal maxLastElement =
|
|
|
|
svalBuilder.evalBinOpLN(state, BO_Add, *dstRegVal, *maxLastNL, ptrTy);
|
|
|
|
|
|
|
|
state = CheckLocation(C, state, Dst, maxLastElement, AccessKind::write);
|
|
|
|
if (!state)
|
|
|
|
return;
|
2011-06-21 05:55:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Then, if the final length is known...
|
2013-02-21 06:23:23 +08:00
|
|
|
if (Optional<NonLoc> knownStrLength = finalStrLength.getAs<NonLoc>()) {
|
2011-06-15 13:52:56 +08:00
|
|
|
SVal lastElement = svalBuilder.evalBinOpLN(state, BO_Add, *dstRegVal,
|
2018-05-15 06:32:24 +08:00
|
|
|
*knownStrLength, ptrTy);
|
2011-06-21 05:55:40 +08:00
|
|
|
|
|
|
|
// ...and we haven't checked the bound, we'll check the actual copy.
|
|
|
|
if (!boundWarning) {
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
state = CheckLocation(C, state, Dst, lastElement, AccessKind::write);
|
2011-06-21 05:55:40 +08:00
|
|
|
if (!state)
|
|
|
|
return;
|
|
|
|
}
|
2010-08-16 15:51:42 +08:00
|
|
|
|
|
|
|
// If this is a stpcpy-style copy, the last element is the return value.
|
2019-11-08 07:58:01 +08:00
|
|
|
if (returnPtr && ReturnEnd)
|
2010-12-02 15:49:45 +08:00
|
|
|
Result = lastElement;
|
2010-08-16 15:51:42 +08:00
|
|
|
}
|
|
|
|
|
2013-11-17 17:18:48 +08:00
|
|
|
// Invalidate the destination (regular invalidation without pointer-escaping
|
|
|
|
// the address of the top-level region). This must happen before we set the
|
|
|
|
// C string length because invalidation will clear the length.
|
2010-08-16 15:51:42 +08:00
|
|
|
// FIXME: Even if we can't perfectly model the copy, we should see if we
|
|
|
|
// can use LazyCompoundVals to copy the source values into the destination.
|
|
|
|
// This would probably remove any existing bindings past the end of the
|
|
|
|
// string, but that's still an improvement over blank invalidation.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
state = InvalidateBuffer(C, state, Dst.Expression, *dstRegVal,
|
|
|
|
/*IsSourceBuffer*/ false, nullptr);
|
2013-11-17 17:18:48 +08:00
|
|
|
|
|
|
|
// Invalidate the source (const-invalidation without const-pointer-escaping
|
|
|
|
// the address of the top-level region).
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
state = InvalidateBuffer(C, state, srcExpr.Expression, srcVal,
|
|
|
|
/*IsSourceBuffer*/ true, nullptr);
|
2010-08-16 15:51:42 +08:00
|
|
|
|
2011-06-20 11:49:16 +08:00
|
|
|
// Set the C string length of the destination, if we know it.
|
2019-11-08 07:58:01 +08:00
|
|
|
if (IsBounded && (appendK == ConcatFnKind::none)) {
|
2011-06-21 05:55:40 +08:00
|
|
|
// strncpy is annoying in that it doesn't guarantee to null-terminate
|
|
|
|
// the result string. If the original string didn't fit entirely inside
|
|
|
|
// the bound (including the null-terminator), we don't know how long the
|
|
|
|
// result is.
|
|
|
|
if (amountCopied != strLength)
|
|
|
|
finalStrLength = UnknownVal();
|
|
|
|
}
|
|
|
|
state = setCStringLength(state, dstRegVal->getRegion(), finalStrLength);
|
2010-08-16 15:51:42 +08:00
|
|
|
}
|
|
|
|
|
2011-06-15 13:52:56 +08:00
|
|
|
assert(state);
|
|
|
|
|
2018-05-15 06:32:24 +08:00
|
|
|
if (returnPtr) {
|
|
|
|
// If this is a stpcpy-style copy, but we were unable to check for a buffer
|
|
|
|
// overflow, we still need a result. Conjure a return value.
|
2019-11-08 07:58:01 +08:00
|
|
|
if (ReturnEnd && Result.isUnknown()) {
|
2018-05-15 06:32:24 +08:00
|
|
|
Result = svalBuilder.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount());
|
|
|
|
}
|
2010-08-16 15:51:42 +08:00
|
|
|
}
|
|
|
|
// Set the return value.
|
2012-01-07 06:09:28 +08:00
|
|
|
state = state->BindExpr(CE, LCtx, Result);
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(state);
|
2010-08-16 15:51:42 +08:00
|
|
|
}
|
|
|
|
|
2011-04-13 01:08:43 +08:00
|
|
|
void CStringChecker::evalStrcmp(CheckerContext &C, const CallExpr *CE) const {
|
2011-06-16 15:13:34 +08:00
|
|
|
//int strcmp(const char *s1, const char *s2);
|
2019-11-08 07:58:01 +08:00
|
|
|
evalStrcmpCommon(C, CE, /* IsBounded = */ false, /* IgnoreCase = */ false);
|
2011-04-26 06:21:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CStringChecker::evalStrncmp(CheckerContext &C, const CallExpr *CE) const {
|
2011-06-16 15:13:34 +08:00
|
|
|
//int strncmp(const char *s1, const char *s2, size_t n);
|
2019-11-08 07:58:01 +08:00
|
|
|
evalStrcmpCommon(C, CE, /* IsBounded = */ true, /* IgnoreCase = */ false);
|
2011-04-28 23:09:11 +08:00
|
|
|
}
|
|
|
|
|
2015-09-08 11:50:52 +08:00
|
|
|
void CStringChecker::evalStrcasecmp(CheckerContext &C,
|
2018-05-15 06:32:24 +08:00
|
|
|
const CallExpr *CE) const {
|
2011-06-16 15:13:34 +08:00
|
|
|
//int strcasecmp(const char *s1, const char *s2);
|
2019-11-08 07:58:01 +08:00
|
|
|
evalStrcmpCommon(C, CE, /* IsBounded = */ false, /* IgnoreCase = */ true);
|
2011-04-26 06:21:00 +08:00
|
|
|
}
|
2011-04-13 01:08:43 +08:00
|
|
|
|
2015-09-08 11:50:52 +08:00
|
|
|
void CStringChecker::evalStrncasecmp(CheckerContext &C,
|
2018-05-15 06:32:24 +08:00
|
|
|
const CallExpr *CE) const {
|
2011-06-16 15:13:34 +08:00
|
|
|
//int strncasecmp(const char *s1, const char *s2, size_t n);
|
2019-11-08 07:58:01 +08:00
|
|
|
evalStrcmpCommon(C, CE, /* IsBounded = */ true, /* IgnoreCase = */ true);
|
2011-05-03 03:05:49 +08:00
|
|
|
}
|
|
|
|
|
2011-04-26 06:21:00 +08:00
|
|
|
void CStringChecker::evalStrcmpCommon(CheckerContext &C, const CallExpr *CE,
|
2019-11-08 07:58:01 +08:00
|
|
|
bool IsBounded, bool IgnoreCase) const {
|
2011-06-20 10:06:40 +08:00
|
|
|
CurrentFunctionDescription = "string comparison function";
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state = C.getState();
|
2012-01-07 06:09:28 +08:00
|
|
|
const LocationContext *LCtx = C.getLocationContext();
|
2011-04-13 01:08:43 +08:00
|
|
|
|
|
|
|
// Check that the first string is non-null
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
AnyArgExpr Left = {CE->getArg(0), 0};
|
|
|
|
SVal LeftVal = state->getSVal(Left.Expression, LCtx);
|
|
|
|
state = checkNonNull(C, state, Left, LeftVal);
|
2011-04-13 01:08:43 +08:00
|
|
|
if (!state)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Check that the second string is non-null.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
AnyArgExpr Right = {CE->getArg(1), 1};
|
|
|
|
SVal RightVal = state->getSVal(Right.Expression, LCtx);
|
|
|
|
state = checkNonNull(C, state, Right, RightVal);
|
2011-04-13 01:08:43 +08:00
|
|
|
if (!state)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Get the string length of the first string or give up.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
SVal LeftLength = getCStringLength(C, state, Left.Expression, LeftVal);
|
|
|
|
if (LeftLength.isUndef())
|
2011-04-13 01:08:43 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Get the string length of the second string or give up.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
SVal RightLength = getCStringLength(C, state, Right.Expression, RightVal);
|
|
|
|
if (RightLength.isUndef())
|
2011-04-13 01:08:43 +08:00
|
|
|
return;
|
|
|
|
|
2011-06-16 15:13:34 +08:00
|
|
|
// If we know the two buffers are the same, we know the result is 0.
|
|
|
|
// First, get the two buffers' addresses. Another checker will have already
|
|
|
|
// made sure they're not undefined.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
DefinedOrUnknownSVal LV = LeftVal.castAs<DefinedOrUnknownSVal>();
|
|
|
|
DefinedOrUnknownSVal RV = RightVal.castAs<DefinedOrUnknownSVal>();
|
2011-06-16 15:13:34 +08:00
|
|
|
|
|
|
|
// See if they are the same.
|
|
|
|
SValBuilder &svalBuilder = C.getSValBuilder();
|
|
|
|
DefinedOrUnknownSVal SameBuf = svalBuilder.evalEQ(state, LV, RV);
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef StSameBuf, StNotSameBuf;
|
2014-03-02 21:01:17 +08:00
|
|
|
std::tie(StSameBuf, StNotSameBuf) = state->assume(SameBuf);
|
2011-06-16 15:13:34 +08:00
|
|
|
|
|
|
|
// If the two arguments might be the same buffer, we know the result is 0,
|
|
|
|
// and we only need to check one size.
|
|
|
|
if (StSameBuf) {
|
2012-01-07 06:09:28 +08:00
|
|
|
StSameBuf = StSameBuf->BindExpr(CE, LCtx,
|
2018-05-15 06:32:24 +08:00
|
|
|
svalBuilder.makeZeroVal(CE->getType()));
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(StSameBuf);
|
2011-06-16 15:13:34 +08:00
|
|
|
|
|
|
|
// If the two arguments are GUARANTEED to be the same, we're done!
|
|
|
|
if (!StNotSameBuf)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(StNotSameBuf);
|
|
|
|
state = StNotSameBuf;
|
2011-04-13 01:08:43 +08:00
|
|
|
|
2011-06-16 15:13:34 +08:00
|
|
|
// At this point we can go about comparing the two buffers.
|
|
|
|
// For now, we only do this if they're both known string literals.
|
|
|
|
|
|
|
|
// Attempt to extract string literals from both expressions.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
const StringLiteral *LeftStrLiteral =
|
|
|
|
getCStringLiteral(C, state, Left.Expression, LeftVal);
|
|
|
|
const StringLiteral *RightStrLiteral =
|
|
|
|
getCStringLiteral(C, state, Right.Expression, RightVal);
|
2011-06-16 15:13:34 +08:00
|
|
|
bool canComputeResult = false;
|
2016-05-20 07:03:49 +08:00
|
|
|
SVal resultVal = svalBuilder.conjureSymbolVal(nullptr, CE, LCtx,
|
2018-05-15 06:32:24 +08:00
|
|
|
C.blockCount());
|
2011-06-16 15:13:34 +08:00
|
|
|
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
if (LeftStrLiteral && RightStrLiteral) {
|
|
|
|
StringRef LeftStrRef = LeftStrLiteral->getString();
|
|
|
|
StringRef RightStrRef = RightStrLiteral->getString();
|
2011-06-16 15:13:34 +08:00
|
|
|
|
2019-11-08 07:58:01 +08:00
|
|
|
if (IsBounded) {
|
2011-06-16 15:13:34 +08:00
|
|
|
// Get the max number of characters to compare.
|
|
|
|
const Expr *lenExpr = CE->getArg(2);
|
2012-01-07 06:09:28 +08:00
|
|
|
SVal lenVal = state->getSVal(lenExpr, LCtx);
|
2011-06-16 15:13:34 +08:00
|
|
|
|
|
|
|
// If the length is known, we can get the right substrings.
|
|
|
|
if (const llvm::APSInt *len = svalBuilder.getKnownValue(state, lenVal)) {
|
|
|
|
// Create substrings of each to compare the prefix.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
LeftStrRef = LeftStrRef.substr(0, (size_t)len->getZExtValue());
|
|
|
|
RightStrRef = RightStrRef.substr(0, (size_t)len->getZExtValue());
|
2011-06-16 15:13:34 +08:00
|
|
|
canComputeResult = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// This is a normal, unbounded strcmp.
|
|
|
|
canComputeResult = true;
|
|
|
|
}
|
2011-04-13 01:08:43 +08:00
|
|
|
|
2011-06-16 15:13:34 +08:00
|
|
|
if (canComputeResult) {
|
|
|
|
// Real strcmp stops at null characters.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
size_t s1Term = LeftStrRef.find('\0');
|
2011-07-23 18:55:15 +08:00
|
|
|
if (s1Term != StringRef::npos)
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
LeftStrRef = LeftStrRef.substr(0, s1Term);
|
2011-04-26 06:21:00 +08:00
|
|
|
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
size_t s2Term = RightStrRef.find('\0');
|
2011-07-23 18:55:15 +08:00
|
|
|
if (s2Term != StringRef::npos)
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
RightStrRef = RightStrRef.substr(0, s2Term);
|
2011-06-16 15:13:34 +08:00
|
|
|
|
|
|
|
// Use StringRef's comparison methods to compute the actual result.
|
2021-06-23 19:52:36 +08:00
|
|
|
int compareRes = IgnoreCase ? LeftStrRef.compare_insensitive(RightStrRef)
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
: LeftStrRef.compare(RightStrRef);
|
2011-06-16 15:13:34 +08:00
|
|
|
|
2016-05-20 07:03:49 +08:00
|
|
|
// The strcmp function returns an integer greater than, equal to, or less
|
|
|
|
// than zero, [c11, p7.24.4.2].
|
|
|
|
if (compareRes == 0) {
|
|
|
|
resultVal = svalBuilder.makeIntVal(compareRes, CE->getType());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
DefinedSVal zeroVal = svalBuilder.makeIntVal(0, CE->getType());
|
|
|
|
// Constrain strcmp's result range based on the result of StringRef's
|
|
|
|
// comparison methods.
|
|
|
|
BinaryOperatorKind op = (compareRes == 1) ? BO_GT : BO_LT;
|
|
|
|
SVal compareWithZero =
|
|
|
|
svalBuilder.evalBinOp(state, op, resultVal, zeroVal,
|
2018-05-15 06:32:24 +08:00
|
|
|
svalBuilder.getConditionType());
|
2016-05-20 07:03:49 +08:00
|
|
|
DefinedSVal compareWithZeroVal = compareWithZero.castAs<DefinedSVal>();
|
|
|
|
state = state->assume(compareWithZeroVal, true);
|
2011-06-16 15:13:34 +08:00
|
|
|
}
|
|
|
|
}
|
2011-05-03 03:05:49 +08:00
|
|
|
}
|
2011-04-28 23:09:11 +08:00
|
|
|
|
2016-05-20 07:03:49 +08:00
|
|
|
state = state->BindExpr(CE, LCtx, resultVal);
|
2011-04-13 01:08:43 +08:00
|
|
|
|
2011-06-16 15:13:34 +08:00
|
|
|
// Record this as a possible path.
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(state);
|
2011-04-13 01:08:43 +08:00
|
|
|
}
|
|
|
|
|
2013-04-23 07:18:42 +08:00
|
|
|
void CStringChecker::evalStrsep(CheckerContext &C, const CallExpr *CE) const {
|
2021-11-20 03:50:09 +08:00
|
|
|
// char *strsep(char **stringp, const char *delim);
|
|
|
|
// Verify whether the search string parameter matches the return type.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
SourceArgExpr SearchStrPtr = {CE->getArg(0), 0};
|
|
|
|
|
|
|
|
QualType CharPtrTy = SearchStrPtr.Expression->getType()->getPointeeType();
|
2013-04-23 07:18:42 +08:00
|
|
|
if (CharPtrTy.isNull() ||
|
|
|
|
CE->getType().getUnqualifiedType() != CharPtrTy.getUnqualifiedType())
|
|
|
|
return;
|
|
|
|
|
|
|
|
CurrentFunctionDescription = "strsep()";
|
|
|
|
ProgramStateRef State = C.getState();
|
|
|
|
const LocationContext *LCtx = C.getLocationContext();
|
|
|
|
|
|
|
|
// Check that the search string pointer is non-null (though it may point to
|
|
|
|
// a null string).
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
SVal SearchStrVal = State->getSVal(SearchStrPtr.Expression, LCtx);
|
|
|
|
State = checkNonNull(C, State, SearchStrPtr, SearchStrVal);
|
2013-04-23 07:18:42 +08:00
|
|
|
if (!State)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Check that the delimiter string is non-null.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
AnyArgExpr DelimStr = {CE->getArg(1), 1};
|
|
|
|
SVal DelimStrVal = State->getSVal(DelimStr.Expression, LCtx);
|
|
|
|
State = checkNonNull(C, State, DelimStr, DelimStrVal);
|
2013-04-23 07:18:42 +08:00
|
|
|
if (!State)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SValBuilder &SVB = C.getSValBuilder();
|
|
|
|
SVal Result;
|
|
|
|
if (Optional<Loc> SearchStrLoc = SearchStrVal.getAs<Loc>()) {
|
|
|
|
// Get the current value of the search string pointer, as a char*.
|
|
|
|
Result = State->getSVal(*SearchStrLoc, CharPtrTy);
|
|
|
|
|
|
|
|
// Invalidate the search string, representing the change of one delimiter
|
|
|
|
// character to NUL.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
State = InvalidateBuffer(C, State, SearchStrPtr.Expression, Result,
|
|
|
|
/*IsSourceBuffer*/ false, nullptr);
|
2013-04-23 07:18:42 +08:00
|
|
|
|
|
|
|
// Overwrite the search string pointer. The new value is either an address
|
|
|
|
// further along in the same string, or NULL if there are no more tokens.
|
|
|
|
State = State->bindLoc(*SearchStrLoc,
|
2018-05-15 06:32:24 +08:00
|
|
|
SVB.conjureSymbolVal(getTag(),
|
|
|
|
CE,
|
|
|
|
LCtx,
|
|
|
|
CharPtrTy,
|
|
|
|
C.blockCount()),
|
|
|
|
LCtx);
|
2013-04-23 07:18:42 +08:00
|
|
|
} else {
|
|
|
|
assert(SearchStrVal.isUnknown());
|
|
|
|
// Conjure a symbolic value. It's the best we can do.
|
2014-05-27 10:45:47 +08:00
|
|
|
Result = SVB.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount());
|
2013-04-23 07:18:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the return value, and finish.
|
|
|
|
State = State->BindExpr(CE, LCtx, Result);
|
|
|
|
C.addTransition(State);
|
|
|
|
}
|
|
|
|
|
2016-02-08 00:55:44 +08:00
|
|
|
// These should probably be moved into a C++ standard library checker.
|
|
|
|
void CStringChecker::evalStdCopy(CheckerContext &C, const CallExpr *CE) const {
|
|
|
|
evalStdCopyCommon(C, CE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStringChecker::evalStdCopyBackward(CheckerContext &C,
|
2018-05-15 06:32:24 +08:00
|
|
|
const CallExpr *CE) const {
|
2016-02-08 00:55:44 +08:00
|
|
|
evalStdCopyCommon(C, CE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStringChecker::evalStdCopyCommon(CheckerContext &C,
|
2018-05-15 06:32:24 +08:00
|
|
|
const CallExpr *CE) const {
|
2019-07-02 07:02:10 +08:00
|
|
|
if (!CE->getArg(2)->getType()->isPointerType())
|
2016-02-08 00:55:44 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
ProgramStateRef State = C.getState();
|
|
|
|
|
|
|
|
const LocationContext *LCtx = C.getLocationContext();
|
|
|
|
|
|
|
|
// template <class _InputIterator, class _OutputIterator>
|
|
|
|
// _OutputIterator
|
|
|
|
// copy(_InputIterator __first, _InputIterator __last,
|
|
|
|
// _OutputIterator __result)
|
|
|
|
|
|
|
|
// Invalidate the destination buffer
|
|
|
|
const Expr *Dst = CE->getArg(2);
|
|
|
|
SVal DstVal = State->getSVal(Dst, LCtx);
|
|
|
|
State = InvalidateBuffer(C, State, Dst, DstVal, /*IsSource=*/false,
|
2018-05-15 06:32:24 +08:00
|
|
|
/*Size=*/nullptr);
|
2016-02-08 00:55:44 +08:00
|
|
|
|
|
|
|
SValBuilder &SVB = C.getSValBuilder();
|
|
|
|
|
|
|
|
SVal ResultVal = SVB.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount());
|
|
|
|
State = State->BindExpr(CE, LCtx, ResultVal);
|
|
|
|
|
|
|
|
C.addTransition(State);
|
|
|
|
}
|
|
|
|
|
2017-06-20 14:41:06 +08:00
|
|
|
void CStringChecker::evalMemset(CheckerContext &C, const CallExpr *CE) const {
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
// void *memset(void *s, int c, size_t n);
|
2017-06-20 14:41:06 +08:00
|
|
|
CurrentFunctionDescription = "memory set function";
|
|
|
|
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
DestinationArgExpr Buffer = {CE->getArg(0), 0};
|
|
|
|
AnyArgExpr CharE = {CE->getArg(1), 1};
|
|
|
|
SizeArgExpr Size = {CE->getArg(2), 2};
|
|
|
|
|
2017-06-20 14:41:06 +08:00
|
|
|
ProgramStateRef State = C.getState();
|
|
|
|
|
|
|
|
// See if the size argument is zero.
|
|
|
|
const LocationContext *LCtx = C.getLocationContext();
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
SVal SizeVal = C.getSVal(Size.Expression);
|
|
|
|
QualType SizeTy = Size.Expression->getType();
|
2017-06-20 14:41:06 +08:00
|
|
|
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
ProgramStateRef ZeroSize, NonZeroSize;
|
|
|
|
std::tie(ZeroSize, NonZeroSize) = assumeZero(C, State, SizeVal, SizeTy);
|
2017-06-20 14:41:06 +08:00
|
|
|
|
|
|
|
// Get the value of the memory area.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
SVal BufferPtrVal = C.getSVal(Buffer.Expression);
|
2017-06-20 14:41:06 +08:00
|
|
|
|
|
|
|
// If the size is zero, there won't be any actual memory access, so
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
// just bind the return value to the buffer and return.
|
|
|
|
if (ZeroSize && !NonZeroSize) {
|
|
|
|
ZeroSize = ZeroSize->BindExpr(CE, LCtx, BufferPtrVal);
|
|
|
|
C.addTransition(ZeroSize);
|
2017-06-20 14:41:06 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-21 08:57:37 +08:00
|
|
|
// Ensure the memory area is not null.
|
|
|
|
// If it is NULL there will be a NULL pointer dereference.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
State = checkNonNull(C, NonZeroSize, Buffer, BufferPtrVal);
|
2018-03-21 08:57:37 +08:00
|
|
|
if (!State)
|
|
|
|
return;
|
|
|
|
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
State = CheckBufferAccess(C, State, Buffer, Size, AccessKind::write);
|
2017-06-20 14:41:06 +08:00
|
|
|
if (!State)
|
|
|
|
return;
|
2018-05-16 20:37:53 +08:00
|
|
|
|
|
|
|
// According to the values of the arguments, bind the value of the second
|
|
|
|
// argument to the destination buffer and set string length, or just
|
|
|
|
// invalidate the destination buffer.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
if (!memsetAux(Buffer.Expression, C.getSVal(CharE.Expression),
|
|
|
|
Size.Expression, C, State))
|
2017-06-20 14:41:06 +08:00
|
|
|
return;
|
|
|
|
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
State = State->BindExpr(CE, LCtx, BufferPtrVal);
|
2017-06-20 14:41:06 +08:00
|
|
|
C.addTransition(State);
|
|
|
|
}
|
|
|
|
|
2018-12-12 02:57:07 +08:00
|
|
|
void CStringChecker::evalBzero(CheckerContext &C, const CallExpr *CE) const {
|
|
|
|
CurrentFunctionDescription = "memory clearance function";
|
|
|
|
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
DestinationArgExpr Buffer = {CE->getArg(0), 0};
|
|
|
|
SizeArgExpr Size = {CE->getArg(1), 1};
|
2018-12-12 02:57:07 +08:00
|
|
|
SVal Zero = C.getSValBuilder().makeZeroVal(C.getASTContext().IntTy);
|
|
|
|
|
|
|
|
ProgramStateRef State = C.getState();
|
2019-11-08 07:58:01 +08:00
|
|
|
|
2018-12-12 02:57:07 +08:00
|
|
|
// See if the size argument is zero.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
SVal SizeVal = C.getSVal(Size.Expression);
|
|
|
|
QualType SizeTy = Size.Expression->getType();
|
2018-12-12 02:57:07 +08:00
|
|
|
|
|
|
|
ProgramStateRef StateZeroSize, StateNonZeroSize;
|
|
|
|
std::tie(StateZeroSize, StateNonZeroSize) =
|
|
|
|
assumeZero(C, State, SizeVal, SizeTy);
|
|
|
|
|
|
|
|
// If the size is zero, there won't be any actual memory access,
|
|
|
|
// In this case we just return.
|
|
|
|
if (StateZeroSize && !StateNonZeroSize) {
|
|
|
|
C.addTransition(StateZeroSize);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the value of the memory area.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
SVal MemVal = C.getSVal(Buffer.Expression);
|
2018-12-12 02:57:07 +08:00
|
|
|
|
|
|
|
// Ensure the memory area is not null.
|
|
|
|
// If it is NULL there will be a NULL pointer dereference.
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
State = checkNonNull(C, StateNonZeroSize, Buffer, MemVal);
|
2018-12-12 02:57:07 +08:00
|
|
|
if (!State)
|
|
|
|
return;
|
|
|
|
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
State = CheckBufferAccess(C, State, Buffer, Size, AccessKind::write);
|
2018-12-12 02:57:07 +08:00
|
|
|
if (!State)
|
|
|
|
return;
|
|
|
|
|
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API
Summary:
I wanted to extend the diagnostics of the CStringChecker with taintedness.
This requires the CStringChecker to be refactored to support a more flexible
reporting mechanism.
This patch does only refactorings, such:
- eliminates always false parameters (like WarnAboutSize)
- reduces the number of parameters
- makes strong types differentiating *source* and *destination* buffers
(same with size expressions)
- binds the argument expression and the index, making diagnostics accurate
and easy to emit
- removes a bunch of default parameters to make it more readable
- remove random const char* warning message parameters, making clear where
and what is going to be emitted
Note that:
- CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC
code duplication
- not every function was refactored to use the /new/ strongly typed API, since
the CString related functions are really closely coupled monolithic beasts,
I will refactor them separately
- all tests are preserved and passing; only the message changed at some places.
In my opinion, these messages are holding the same information.
I would also highlight that this refactoring caught a bug in
clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality.
This catch backs my effort on simplifying this monolithic CStringChecker.
Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso
Reviewed By: NoQ
Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin,
mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
|
|
|
if (!memsetAux(Buffer.Expression, Zero, Size.Expression, C, State))
|
2018-12-12 02:57:07 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
C.addTransition(State);
|
|
|
|
}
|
|
|
|
|
2010-07-09 07:57:29 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2010-08-15 05:02:52 +08:00
|
|
|
// The driver method, and other Checker callbacks.
|
2010-07-09 07:57:29 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2010-07-07 07:11:01 +08:00
|
|
|
|
2019-07-02 07:02:10 +08:00
|
|
|
CStringChecker::FnCheck CStringChecker::identifyCall(const CallEvent &Call,
|
|
|
|
CheckerContext &C) const {
|
|
|
|
const auto *CE = dyn_cast_or_null<CallExpr>(Call.getOriginExpr());
|
|
|
|
if (!CE)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(Call.getDecl());
|
|
|
|
if (!FD)
|
2018-12-20 05:50:46 +08:00
|
|
|
return nullptr;
|
|
|
|
|
2021-11-20 01:32:13 +08:00
|
|
|
if (StdCopy.matches(Call))
|
2019-07-02 07:02:10 +08:00
|
|
|
return &CStringChecker::evalStdCopy;
|
2021-11-20 01:32:13 +08:00
|
|
|
if (StdCopyBackward.matches(Call))
|
2019-07-02 07:02:10 +08:00
|
|
|
return &CStringChecker::evalStdCopyBackward;
|
|
|
|
|
2018-12-20 05:50:46 +08:00
|
|
|
// Pro-actively check that argument types are safe to do arithmetic upon.
|
|
|
|
// We do not want to crash if someone accidentally passes a structure
|
2019-07-02 07:02:10 +08:00
|
|
|
// into, say, a C++ overload of any of these functions. We could not check
|
|
|
|
// that for std::copy because they may have arguments of other types.
|
|
|
|
for (auto I : CE->arguments()) {
|
|
|
|
QualType T = I->getType();
|
|
|
|
if (!T->isIntegralOrEnumerationType() && !T->isPointerType())
|
2018-12-20 05:50:46 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2010-07-07 07:11:01 +08:00
|
|
|
|
2019-07-02 07:02:10 +08:00
|
|
|
const FnCheck *Callback = Callbacks.lookup(Call);
|
|
|
|
if (Callback)
|
|
|
|
return *Callback;
|
2018-12-20 05:50:46 +08:00
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-06-20 07:33:42 +08:00
|
|
|
bool CStringChecker::evalCall(const CallEvent &Call, CheckerContext &C) const {
|
2019-07-02 07:02:10 +08:00
|
|
|
FnCheck Callback = identifyCall(Call, C);
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2010-07-09 07:57:29 +08:00
|
|
|
// If the callee isn't a string function, let another checker handle it.
|
2019-07-02 07:02:10 +08:00
|
|
|
if (!Callback)
|
2010-07-07 07:11:01 +08:00
|
|
|
return false;
|
|
|
|
|
2010-07-09 07:57:29 +08:00
|
|
|
// Check and evaluate the call.
|
2019-07-02 07:02:10 +08:00
|
|
|
const auto *CE = cast<CallExpr>(Call.getOriginExpr());
|
|
|
|
(this->*Callback)(C, CE);
|
2012-02-07 08:56:14 +08:00
|
|
|
|
|
|
|
// If the evaluate call resulted in no change, chain to the next eval call
|
|
|
|
// handler.
|
|
|
|
// Note, the custom CString evaluation calls assume that basic safety
|
|
|
|
// properties are held. However, if the user chooses to turn off some of these
|
|
|
|
// checks, we ignore the issues and leave the call evaluation to a generic
|
|
|
|
// handler.
|
2015-12-28 21:06:58 +08:00
|
|
|
return C.isDifferent();
|
2010-07-07 07:11:01 +08:00
|
|
|
}
|
2010-08-15 05:02:52 +08:00
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
void CStringChecker::checkPreStmt(const DeclStmt *DS, CheckerContext &C) const {
|
2010-08-15 05:02:52 +08:00
|
|
|
// Record string length for char a[] = "abc";
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state = C.getState();
|
2010-08-15 05:02:52 +08:00
|
|
|
|
2014-03-15 01:01:24 +08:00
|
|
|
for (const auto *I : DS->decls()) {
|
|
|
|
const VarDecl *D = dyn_cast<VarDecl>(I);
|
2010-08-15 05:02:52 +08:00
|
|
|
if (!D)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// FIXME: Handle array fields of structs.
|
|
|
|
if (!D->getType()->isArrayType())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const Expr *Init = D->getInit();
|
|
|
|
if (!Init)
|
|
|
|
continue;
|
|
|
|
if (!isa<StringLiteral>(Init))
|
|
|
|
continue;
|
|
|
|
|
2011-10-27 05:06:44 +08:00
|
|
|
Loc VarLoc = state->getLValue(D, C.getLocationContext());
|
2010-08-15 05:02:52 +08:00
|
|
|
const MemRegion *MR = VarLoc.getAsRegion();
|
|
|
|
if (!MR)
|
|
|
|
continue;
|
|
|
|
|
2018-01-18 04:27:29 +08:00
|
|
|
SVal StrVal = C.getSVal(Init);
|
2010-08-15 05:02:52 +08:00
|
|
|
assert(StrVal.isValid() && "Initializer string is unknown or undefined");
|
2013-02-20 13:52:05 +08:00
|
|
|
DefinedOrUnknownSVal strLength =
|
2018-05-15 06:32:24 +08:00
|
|
|
getCStringLength(C, state, Init, StrVal).castAs<DefinedOrUnknownSVal>();
|
2010-08-15 05:02:52 +08:00
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
state = state->set<CStringLength>(MR, strLength);
|
2010-08-15 05:02:52 +08:00
|
|
|
}
|
|
|
|
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(state);
|
2010-08-15 05:02:52 +08:00
|
|
|
}
|
|
|
|
|
2015-09-08 11:50:52 +08:00
|
|
|
ProgramStateRef
|
2012-01-27 05:29:00 +08:00
|
|
|
CStringChecker::checkRegionChanges(ProgramStateRef state,
|
2018-05-15 06:32:24 +08:00
|
|
|
const InvalidatedSymbols *,
|
|
|
|
ArrayRef<const MemRegion *> ExplicitRegions,
|
|
|
|
ArrayRef<const MemRegion *> Regions,
|
|
|
|
const LocationContext *LCtx,
|
|
|
|
const CallEvent *Call) const {
|
2012-11-02 09:54:06 +08:00
|
|
|
CStringLengthTy Entries = state->get<CStringLength>();
|
2010-08-15 05:02:52 +08:00
|
|
|
if (Entries.isEmpty())
|
|
|
|
return state;
|
|
|
|
|
|
|
|
llvm::SmallPtrSet<const MemRegion *, 8> Invalidated;
|
|
|
|
llvm::SmallPtrSet<const MemRegion *, 32> SuperRegions;
|
|
|
|
|
|
|
|
// First build sets for the changed regions and their super-regions.
|
2011-08-28 06:51:26 +08:00
|
|
|
for (ArrayRef<const MemRegion *>::iterator
|
2018-05-15 06:32:24 +08:00
|
|
|
I = Regions.begin(), E = Regions.end(); I != E; ++I) {
|
2011-08-28 06:51:26 +08:00
|
|
|
const MemRegion *MR = *I;
|
2010-08-15 05:02:52 +08:00
|
|
|
Invalidated.insert(MR);
|
|
|
|
|
|
|
|
SuperRegions.insert(MR);
|
|
|
|
while (const SubRegion *SR = dyn_cast<SubRegion>(MR)) {
|
|
|
|
MR = SR->getSuperRegion();
|
|
|
|
SuperRegions.insert(MR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-02 09:54:06 +08:00
|
|
|
CStringLengthTy::Factory &F = state->get_context<CStringLength>();
|
2010-08-15 05:02:52 +08:00
|
|
|
|
|
|
|
// Then loop over the entries in the current state.
|
2012-11-02 09:54:06 +08:00
|
|
|
for (CStringLengthTy::iterator I = Entries.begin(),
|
2018-05-15 06:32:24 +08:00
|
|
|
E = Entries.end(); I != E; ++I) {
|
2010-08-15 05:02:52 +08:00
|
|
|
const MemRegion *MR = I.getKey();
|
|
|
|
|
|
|
|
// Is this entry for a super-region of a changed region?
|
|
|
|
if (SuperRegions.count(MR)) {
|
2010-11-24 08:54:37 +08:00
|
|
|
Entries = F.remove(Entries, MR);
|
2010-08-15 05:02:52 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is this entry for a sub-region of a changed region?
|
|
|
|
const MemRegion *Super = MR;
|
|
|
|
while (const SubRegion *SR = dyn_cast<SubRegion>(Super)) {
|
|
|
|
Super = SR->getSuperRegion();
|
|
|
|
if (Invalidated.count(Super)) {
|
2010-11-24 08:54:37 +08:00
|
|
|
Entries = F.remove(Entries, MR);
|
2010-08-15 05:02:52 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return state->set<CStringLength>(Entries);
|
|
|
|
}
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
void CStringChecker::checkLiveSymbols(ProgramStateRef state,
|
2018-05-15 06:32:24 +08:00
|
|
|
SymbolReaper &SR) const {
|
2010-08-15 05:02:52 +08:00
|
|
|
// Mark all symbols in our string length map as valid.
|
2012-11-02 09:54:06 +08:00
|
|
|
CStringLengthTy Entries = state->get<CStringLength>();
|
2010-08-15 05:02:52 +08:00
|
|
|
|
2012-11-02 09:54:06 +08:00
|
|
|
for (CStringLengthTy::iterator I = Entries.begin(), E = Entries.end();
|
2018-05-15 06:32:24 +08:00
|
|
|
I != E; ++I) {
|
2010-08-15 05:02:52 +08:00
|
|
|
SVal Len = I.getData();
|
2011-06-15 13:52:56 +08:00
|
|
|
|
2011-12-07 07:12:33 +08:00
|
|
|
for (SymExpr::symbol_iterator si = Len.symbol_begin(),
|
2018-05-15 06:32:24 +08:00
|
|
|
se = Len.symbol_end(); si != se; ++si)
|
2011-06-15 13:52:56 +08:00
|
|
|
SR.markInUse(*si);
|
2010-08-15 05:02:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
void CStringChecker::checkDeadSymbols(SymbolReaper &SR,
|
2018-05-15 06:32:24 +08:00
|
|
|
CheckerContext &C) const {
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state = C.getState();
|
2012-11-02 09:54:06 +08:00
|
|
|
CStringLengthTy Entries = state->get<CStringLength>();
|
2010-08-15 05:02:52 +08:00
|
|
|
if (Entries.isEmpty())
|
|
|
|
return;
|
|
|
|
|
2012-11-02 09:54:06 +08:00
|
|
|
CStringLengthTy::Factory &F = state->get_context<CStringLength>();
|
|
|
|
for (CStringLengthTy::iterator I = Entries.begin(), E = Entries.end();
|
2018-05-15 06:32:24 +08:00
|
|
|
I != E; ++I) {
|
2010-08-15 05:02:52 +08:00
|
|
|
SVal Len = I.getData();
|
|
|
|
if (SymbolRef Sym = Len.getAsSymbol()) {
|
|
|
|
if (SR.isDead(Sym))
|
2010-11-24 08:54:37 +08:00
|
|
|
Entries = F.remove(Entries, I.getKey());
|
2010-08-15 05:02:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
state = state->set<CStringLength>(Entries);
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(state);
|
2010-08-15 05:02:52 +08:00
|
|
|
}
|
2011-02-24 09:05:30 +08:00
|
|
|
|
[analyzer] Reimplement dependencies between checkers
Unfortunately, up until now, the fact that certain checkers depended on one
another was known, but how these actually unfolded was hidden deep within the
implementation. For example, many checkers (like RetainCount, Malloc or CString)
modelled a certain functionality, and exposed certain reportable bug types to
the user. For example, while MallocChecker models many many different types of
memory handling, the actual "unix.MallocChecker" checker the user was exposed to
was merely and option to this modeling part.
Other than this being an ugly mess, this issue made resolving the checker naming
issue almost impossible. (The checker naming issue being that if a checker
registered more than one checker within its registry function, both checker
object recieved the same name) Also, if the user explicitly disabled a checker
that was a dependency of another that _was_ explicitly enabled, it implicitly,
without "telling" the user, reenabled it.
Clearly, changing this to a well structured, declarative form, where the
handling of dependencies are done on a higher level is very much preferred.
This patch, among the detailed things later, makes checkers declare their
dependencies within the TableGen file Checkers.td, and exposes the same
functionality to plugins and statically linked non-generated checkers through
CheckerRegistry::addDependency. CheckerRegistry now resolves these dependencies,
makes sure that checkers are added to CheckerManager in the correct order,
and makes sure that if a dependency is disabled, so will be every checker that
depends on it.
In detail:
* Add a new field to the Checker class in CheckerBase.td called Dependencies,
which is a list of Checkers.
* Move unix checkers before cplusplus, as there is no forward declaration in
tblgen :/
* Add the following new checkers:
- StackAddrEscapeBase
- StackAddrEscapeBase
- CStringModeling
- DynamicMemoryModeling (base of the MallocChecker family)
- IteratorModeling (base of the IteratorChecker family)
- ValistBase
- SecuritySyntaxChecker (base of bcmp, bcopy, etc...)
- NSOrCFErrorDerefChecker (base of NSErrorChecker and CFErrorChecker)
- IvarInvalidationModeling (base of IvarInvalidation checker family)
- RetainCountBase (base of RetainCount and OSObjectRetainCount)
* Clear up and registry functions in MallocChecker, happily remove old FIXMEs.
* Add a new addDependency function to CheckerRegistry.
* Neatly format RUN lines in files I looked at while debugging.
Big thanks to Artem Degrachev for all the guidance through this project!
Differential Revision: https://reviews.llvm.org/D54438
llvm-svn: 352287
2019-01-27 04:06:54 +08:00
|
|
|
void ento::registerCStringModeling(CheckerManager &Mgr) {
|
|
|
|
Mgr.registerChecker<CStringChecker>();
|
|
|
|
}
|
|
|
|
|
2020-03-27 21:29:31 +08:00
|
|
|
bool ento::shouldRegisterCStringModeling(const CheckerManager &mgr) {
|
[analyzer] Reimplement dependencies between checkers
Unfortunately, up until now, the fact that certain checkers depended on one
another was known, but how these actually unfolded was hidden deep within the
implementation. For example, many checkers (like RetainCount, Malloc or CString)
modelled a certain functionality, and exposed certain reportable bug types to
the user. For example, while MallocChecker models many many different types of
memory handling, the actual "unix.MallocChecker" checker the user was exposed to
was merely and option to this modeling part.
Other than this being an ugly mess, this issue made resolving the checker naming
issue almost impossible. (The checker naming issue being that if a checker
registered more than one checker within its registry function, both checker
object recieved the same name) Also, if the user explicitly disabled a checker
that was a dependency of another that _was_ explicitly enabled, it implicitly,
without "telling" the user, reenabled it.
Clearly, changing this to a well structured, declarative form, where the
handling of dependencies are done on a higher level is very much preferred.
This patch, among the detailed things later, makes checkers declare their
dependencies within the TableGen file Checkers.td, and exposes the same
functionality to plugins and statically linked non-generated checkers through
CheckerRegistry::addDependency. CheckerRegistry now resolves these dependencies,
makes sure that checkers are added to CheckerManager in the correct order,
and makes sure that if a dependency is disabled, so will be every checker that
depends on it.
In detail:
* Add a new field to the Checker class in CheckerBase.td called Dependencies,
which is a list of Checkers.
* Move unix checkers before cplusplus, as there is no forward declaration in
tblgen :/
* Add the following new checkers:
- StackAddrEscapeBase
- StackAddrEscapeBase
- CStringModeling
- DynamicMemoryModeling (base of the MallocChecker family)
- IteratorModeling (base of the IteratorChecker family)
- ValistBase
- SecuritySyntaxChecker (base of bcmp, bcopy, etc...)
- NSOrCFErrorDerefChecker (base of NSErrorChecker and CFErrorChecker)
- IvarInvalidationModeling (base of IvarInvalidation checker family)
- RetainCountBase (base of RetainCount and OSObjectRetainCount)
* Clear up and registry functions in MallocChecker, happily remove old FIXMEs.
* Add a new addDependency function to CheckerRegistry.
* Neatly format RUN lines in files I looked at while debugging.
Big thanks to Artem Degrachev for all the guidance through this project!
Differential Revision: https://reviews.llvm.org/D54438
llvm-svn: 352287
2019-01-27 04:06:54 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-12 05:49:21 +08:00
|
|
|
#define REGISTER_CHECKER(name) \
|
|
|
|
void ento::register##name(CheckerManager &mgr) { \
|
2019-01-27 05:41:50 +08:00
|
|
|
CStringChecker *checker = mgr.getChecker<CStringChecker>(); \
|
2014-02-12 05:49:21 +08:00
|
|
|
checker->Filter.Check##name = true; \
|
2019-09-13 03:09:24 +08:00
|
|
|
checker->Filter.CheckName##name = mgr.getCurrentCheckerName(); \
|
2019-01-26 22:23:08 +08:00
|
|
|
} \
|
|
|
|
\
|
2020-03-27 21:29:31 +08:00
|
|
|
bool ento::shouldRegister##name(const CheckerManager &mgr) { return true; }
|
2012-02-07 08:56:14 +08:00
|
|
|
|
2019-09-13 03:09:24 +08:00
|
|
|
REGISTER_CHECKER(CStringNullArg)
|
|
|
|
REGISTER_CHECKER(CStringOutOfBounds)
|
|
|
|
REGISTER_CHECKER(CStringBufferOverlap)
|
2012-02-07 08:56:14 +08:00
|
|
|
REGISTER_CHECKER(CStringNotNullTerm)
|
2022-04-10 20:44:11 +08:00
|
|
|
REGISTER_CHECKER(CStringUninitializedRead)
|