Skip to content

Commit 3d1794d

Browse files
author
git apple-llvm automerger
committed
Merge commit 'c42459ec3fc7' from apple/main into swift/next
2 parents 4aadd54 + c42459e commit 3d1794d

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

clang/lib/StaticAnalyzer/Checkers/OSObjectCStyleCast.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,36 @@ using namespace ento;
2424
using namespace ast_matchers;
2525

2626
namespace {
27-
28-
const char *WarnAtNode = "OSObjCast";
27+
static constexpr const char *const WarnAtNode = "WarnAtNode";
28+
static constexpr const char *const WarnRecordDecl = "WarnRecordDecl";
2929

3030
class OSObjectCStyleCastChecker : public Checker<check::ASTCodeBody> {
3131
public:
32-
void checkASTCodeBody(const Decl *D,
33-
AnalysisManager &AM,
32+
void checkASTCodeBody(const Decl *D, AnalysisManager &AM,
3433
BugReporter &BR) const;
3534
};
35+
}
3636

3737
static void emitDiagnostics(const BoundNodes &Nodes,
3838
BugReporter &BR,
3939
AnalysisDeclContext *ADC,
4040
const OSObjectCStyleCastChecker *Checker) {
4141
const auto *CE = Nodes.getNodeAs<CastExpr>(WarnAtNode);
42-
assert(CE);
42+
const CXXRecordDecl *RD = Nodes.getNodeAs<CXXRecordDecl>(WarnRecordDecl);
43+
assert(CE && RD);
4344

4445
std::string Diagnostics;
4546
llvm::raw_string_ostream OS(Diagnostics);
46-
OS << "C-style cast of OSObject. Use OSDynamicCast instead.";
47+
OS << "C-style cast of an OSObject is prone to type confusion attacks; "
48+
<< "use 'OSRequiredCast' if the object is definitely of type '"
49+
<< RD->getNameAsString() << "', or 'OSDynamicCast' followed by "
50+
<< "a null check if unsure",
4751

4852
BR.EmitBasicReport(
4953
ADC->getDecl(),
5054
Checker,
5155
/*Name=*/"OSObject C-Style Cast",
52-
/*BugCategory=*/"Security",
56+
categories::SecurityError,
5357
OS.str(),
5458
PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), ADC),
5559
CE->getSourceRange());
@@ -68,7 +72,7 @@ void OSObjectCStyleCastChecker::checkASTCodeBody(const Decl *D, AnalysisManager
6872

6973
auto OSObjTypeM = hasTypePointingTo(cxxRecordDecl(isDerivedFrom("OSMetaClassBase")));
7074
auto OSObjSubclassM = hasTypePointingTo(
71-
cxxRecordDecl(isDerivedFrom("OSObject")));
75+
cxxRecordDecl(isDerivedFrom("OSObject")).bind(WarnRecordDecl));
7276

7377
auto CastM = cStyleCastExpr(
7478
allOf(hasSourceExpression(allOf(OSObjTypeM, unless(DynamicCastM))),
@@ -78,7 +82,6 @@ void OSObjectCStyleCastChecker::checkASTCodeBody(const Decl *D, AnalysisManager
7882
for (BoundNodes Match : Matches)
7983
emitDiagnostics(Match, BR, ADC, this);
8084
}
81-
}
8285

8386
void ento::registerOSObjectCStyleCast(CheckerManager &Mgr) {
8487
Mgr.registerChecker<OSObjectCStyleCastChecker>();

clang/test/Analysis/osobjectcstylecastchecker_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct B : public A {
1313
};
1414

1515
unsigned warn_on_explicit_downcast(OSObject * obj) {
16-
OSArray *a = (OSArray *) obj; // expected-warning{{C-style cast of OSObject. Use OSDynamicCast instead}}
16+
OSArray *a = (OSArray *) obj; // expected-warning{{C-style cast of an OSObject is prone to type confusion attacks; use 'OSRequiredCast' if the object is definitely of type 'OSArray', or 'OSDynamicCast' followed by a null check if unsure}}
1717
return a->getCount();
1818
}
1919

0 commit comments

Comments
 (0)