Skip to content

Commit e2f1cba

Browse files
authored
[analyzer] Use explicit call description mode (easy cases) (#88879)
This commit explicitly specifies the matching mode (C library function, any non-method function, or C++ method) for the `CallDescription`s constructed in various checkers where this transition was easy and straightforward. This change won't cause major functional changes, but isn't NFC because it ensures that e.g. call descriptions for a non-method function won't accidentally match a method that has the same name. Separate commits will perform (or have already performed) this change in other checkers. My goal is to ensure that the call description mode is always explicitly specified and eliminate (or strongly restrict) the vague "may be either a method or a simple function" mode that's the current default.
1 parent 5af9701 commit e2f1cba

File tree

7 files changed

+28
-24
lines changed

7 files changed

+28
-24
lines changed

clang/lib/StaticAnalyzer/Checkers/CastValueChecker.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,23 @@ class CastValueChecker : public Checker<check::DeadSymbols, eval::Call> {
5656

5757
private:
5858
// These are known in the LLVM project. The pairs are in the following form:
59-
// {{{namespace, call}, argument-count}, {callback, kind}}
59+
// {{match-mode, {namespace, call}, argument-count}, {callback, kind}}
6060
const CallDescriptionMap<std::pair<CastCheck, CallKind>> CDM = {
61-
{{{"llvm", "cast"}, 1},
61+
{{CDM::SimpleFunc, {"llvm", "cast"}, 1},
6262
{&CastValueChecker::evalCast, CallKind::Function}},
63-
{{{"llvm", "dyn_cast"}, 1},
63+
{{CDM::SimpleFunc, {"llvm", "dyn_cast"}, 1},
6464
{&CastValueChecker::evalDynCast, CallKind::Function}},
65-
{{{"llvm", "cast_or_null"}, 1},
65+
{{CDM::SimpleFunc, {"llvm", "cast_or_null"}, 1},
6666
{&CastValueChecker::evalCastOrNull, CallKind::Function}},
67-
{{{"llvm", "dyn_cast_or_null"}, 1},
67+
{{CDM::SimpleFunc, {"llvm", "dyn_cast_or_null"}, 1},
6868
{&CastValueChecker::evalDynCastOrNull, CallKind::Function}},
69-
{{{"clang", "castAs"}, 0},
69+
{{CDM::CXXMethod, {"clang", "castAs"}, 0},
7070
{&CastValueChecker::evalCastAs, CallKind::Method}},
71-
{{{"clang", "getAs"}, 0},
71+
{{CDM::CXXMethod, {"clang", "getAs"}, 0},
7272
{&CastValueChecker::evalGetAs, CallKind::Method}},
73-
{{{"llvm", "isa"}, 1},
73+
{{CDM::SimpleFunc, {"llvm", "isa"}, 1},
7474
{&CastValueChecker::evalIsa, CallKind::InstanceOf}},
75-
{{{"llvm", "isa_and_nonnull"}, 1},
75+
{{CDM::SimpleFunc, {"llvm", "isa_and_nonnull"}, 1},
7676
{&CastValueChecker::evalIsaAndNonNull, CallKind::InstanceOf}}};
7777

7878
void evalCast(const CallEvent &Call, DefinedOrUnknownSVal DV,

clang/lib/StaticAnalyzer/Checkers/ChrootChecker.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ class ChrootChecker : public Checker<eval::Call, check::PreCall> {
4343
// This bug refers to possibly break out of a chroot() jail.
4444
const BugType BT_BreakJail{this, "Break out of jail"};
4545

46-
const CallDescription Chroot{{"chroot"}, 1}, Chdir{{"chdir"}, 1};
46+
const CallDescription Chroot{CDM::CLibrary, {"chroot"}, 1},
47+
Chdir{CDM::CLibrary, {"chdir"}, 1};
4748

4849
public:
4950
ChrootChecker() {}

clang/lib/StaticAnalyzer/Checkers/ErrnoTesterChecker.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,15 @@ class ErrnoTesterChecker : public Checker<eval::Call> {
7070

7171
using EvalFn = std::function<void(CheckerContext &, const CallEvent &)>;
7272
const CallDescriptionMap<EvalFn> TestCalls{
73-
{{{"ErrnoTesterChecker_setErrno"}, 1}, &ErrnoTesterChecker::evalSetErrno},
74-
{{{"ErrnoTesterChecker_getErrno"}, 0}, &ErrnoTesterChecker::evalGetErrno},
75-
{{{"ErrnoTesterChecker_setErrnoIfError"}, 0},
73+
{{CDM::SimpleFunc, {"ErrnoTesterChecker_setErrno"}, 1},
74+
&ErrnoTesterChecker::evalSetErrno},
75+
{{CDM::SimpleFunc, {"ErrnoTesterChecker_getErrno"}, 0},
76+
&ErrnoTesterChecker::evalGetErrno},
77+
{{CDM::SimpleFunc, {"ErrnoTesterChecker_setErrnoIfError"}, 0},
7678
&ErrnoTesterChecker::evalSetErrnoIfError},
77-
{{{"ErrnoTesterChecker_setErrnoIfErrorRange"}, 0},
79+
{{CDM::SimpleFunc, {"ErrnoTesterChecker_setErrnoIfErrorRange"}, 0},
7880
&ErrnoTesterChecker::evalSetErrnoIfErrorRange},
79-
{{{"ErrnoTesterChecker_setErrnoCheckState"}, 0},
81+
{{CDM::SimpleFunc, {"ErrnoTesterChecker_setErrnoCheckState"}, 0},
8082
&ErrnoTesterChecker::evalSetErrnoCheckState}};
8183
};
8284

clang/lib/StaticAnalyzer/Checkers/MmapWriteExecChecker.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,15 @@ using namespace ento;
2727

2828
namespace {
2929
class MmapWriteExecChecker : public Checker<check::PreCall> {
30-
CallDescription MmapFn;
31-
CallDescription MprotectFn;
30+
CallDescription MmapFn{CDM::CLibrary, {"mmap"}, 6};
31+
CallDescription MprotectFn{CDM::CLibrary, {"mprotect"}, 3};
3232
static int ProtWrite;
3333
static int ProtExec;
3434
static int ProtRead;
3535
const BugType BT{this, "W^X check fails, Write Exec prot flags set",
3636
"Security"};
3737

3838
public:
39-
MmapWriteExecChecker() : MmapFn({"mmap"}, 6), MprotectFn({"mprotect"}, 3) {}
4039
void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
4140
int ProtExecOv;
4241
int ProtReadOv;

clang/lib/StaticAnalyzer/Checkers/StdVariantChecker.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,11 @@ static llvm::StringRef indefiniteArticleBasedOnVowel(char a) {
129129

130130
class StdVariantChecker : public Checker<eval::Call, check::RegionChanges> {
131131
// Call descriptors to find relevant calls
132-
CallDescription VariantConstructor{{"std", "variant", "variant"}};
133-
CallDescription VariantAssignmentOperator{{"std", "variant", "operator="}};
134-
CallDescription StdGet{{"std", "get"}, 1, 1};
132+
CallDescription VariantConstructor{CDM::CXXMethod,
133+
{"std", "variant", "variant"}};
134+
CallDescription VariantAssignmentOperator{CDM::CXXMethod,
135+
{"std", "variant", "operator="}};
136+
CallDescription StdGet{CDM::SimpleFunc, {"std", "get"}, 1, 1};
135137

136138
BugType BadVariantType{this, "BadVariantType", "BadVariantType"};
137139

@@ -295,4 +297,4 @@ bool clang::ento::shouldRegisterStdVariantChecker(
295297

296298
void clang::ento::registerStdVariantChecker(clang::ento::CheckerManager &mgr) {
297299
mgr.registerChecker<StdVariantChecker>();
298-
}
300+
}

clang/lib/StaticAnalyzer/Checkers/StringChecker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class StringChecker : public Checker<check::PreCall> {
2727
mutable const FunctionDecl *StringConstCharPtrCtor = nullptr;
2828
mutable CanQualType SizeTypeTy;
2929
const CallDescription TwoParamStdStringCtor = {
30-
{"std", "basic_string", "basic_string"}, 2, 2};
30+
CDM::CXXMethod, {"std", "basic_string", "basic_string"}, 2, 2};
3131

3232
bool isCharToStringCtor(const CallEvent &Call, const ASTContext &ACtx) const;
3333

clang/lib/StaticAnalyzer/Checkers/cert/PutenvWithAutoChecker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class PutenvWithAutoChecker : public Checker<check::PostCall> {
3030
private:
3131
BugType BT{this, "'putenv' function should not be called with auto variables",
3232
categories::SecurityError};
33-
const CallDescription Putenv{{"putenv"}, 1};
33+
const CallDescription Putenv{CDM::CLibrary, {"putenv"}, 1};
3434

3535
public:
3636
void checkPostCall(const CallEvent &Call, CheckerContext &C) const;

0 commit comments

Comments
 (0)