Skip to content

Commit

Permalink
[analyzer] MmapWriteExecChecker: use getAs instead of castAs
Browse files Browse the repository at this point in the history
Use 'getAs' instead of 'castAs'

Reviewed By: steakhal

Fixes #62285

Differential Revision: https://reviews.llvm.org/D158953
  • Loading branch information
danix800 committed Aug 30, 2023
1 parent fa1dc06 commit 2b6160e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 4 additions & 2 deletions clang/lib/StaticAnalyzer/Checkers/MmapWriteExecChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ void MmapWriteExecChecker::checkPreCall(const CallEvent &Call,
CheckerContext &C) const {
if (matchesAny(Call, MmapFn, MprotectFn)) {
SVal ProtVal = Call.getArgSVal(2);
auto ProtLoc = ProtVal.castAs<nonloc::ConcreteInt>();
int64_t Prot = ProtLoc.getValue().getSExtValue();
auto ProtLoc = ProtVal.getAs<nonloc::ConcreteInt>();
if (!ProtLoc)
return;
int64_t Prot = ProtLoc->getValue().getSExtValue();
if (ProtExecOv != ProtExec)
ProtExec = ProtExecOv;
if (ProtReadOv != ProtRead)
Expand Down
6 changes: 6 additions & 0 deletions clang/test/Analysis/mmap-writeexec.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,9 @@ void f3(void)
int m = mprotect(p, 1024, PROT_WRITE | PROT_EXEC); // expected-warning{{Both PROT_WRITE and PROT_EXEC flags are set. This can lead to exploitable memory regions, which could be overwritten with malicious code}}
(void)m;
}

// gh62285: no crash on non concrete arg 'prot'
void *gh62285(void *addr, int prot)
{
return mmap(addr, 1, prot, 1, 1, 1);
}

0 comments on commit 2b6160e

Please sign in to comment.