[analyzer] Fix a security.cert.env.InvalidPtr crash
authorBalazs Benics <benicsbalazs@gmail.com>
Wed, 17 Apr 2024 06:02:49 +0000 (08:02 +0200)
committerTom Stellard <tstellar@redhat.com>
Tue, 23 Apr 2024 15:52:08 +0000 (08:52 -0700)
Fixes #88181

(cherry picked from commit e096c144921daba59963f15e89d2ca6fb32d3a78)

clang/docs/ReleaseNotes.rst
clang/lib/StaticAnalyzer/Checkers/cert/InvalidPtrChecker.cpp
clang/test/Analysis/invalid-ptr-checker.cpp [new file with mode: 0644]

index ce7e615d87894460bf8c5cf9b7567ba6a231a024..1e88b58725bd953ebe6d078431bf37b630f89ad5 100644 (file)
@@ -1474,6 +1474,10 @@ Crash and bug fixes
 - Fix false positive in mutation check when using pointer to member function.
   (`#66204 <https://github.com/llvm/llvm-project/issues/66204>`_)
 
+- Fixed a crash in ``security.cert.env.InvalidPtr`` checker when accidentally
+  matched user-defined ``strerror`` and similar library functions.
+  (`#88181 <https://github.com/llvm/llvm-project/issues/88181>`_)
+
 Improvements
 ^^^^^^^^^^^^
 
index e5dd907c660d8ea90596a8bf6e7189240777b891..b2947f590c4ec135b1f57cf8f85d1c5896c79c08 100644 (file)
@@ -205,8 +205,12 @@ void InvalidPtrChecker::postPreviousReturnInvalidatingCall(
       CE, LCtx, CE->getType(), C.blockCount());
   State = State->BindExpr(CE, LCtx, RetVal);
 
+  const auto *SymRegOfRetVal =
+      dyn_cast_or_null<SymbolicRegion>(RetVal.getAsRegion());
+  if (!SymRegOfRetVal)
+    return;
+
   // Remember to this region.
-  const auto *SymRegOfRetVal = cast<SymbolicRegion>(RetVal.getAsRegion());
   const MemRegion *MR = SymRegOfRetVal->getBaseRegion();
   State = State->set<PreviousCallResultMap>(FD, MR);
 
diff --git a/clang/test/Analysis/invalid-ptr-checker.cpp b/clang/test/Analysis/invalid-ptr-checker.cpp
new file mode 100644 (file)
index 0000000..58bb45e
--- /dev/null
@@ -0,0 +1,10 @@
+// RUN: %clang_analyze_cc1 -analyzer-checker=core,security.cert.env.InvalidPtr -verify %s
+
+// expected-no-diagnostics
+
+namespace other {
+int strerror(int errnum); // custom strerror
+void no_crash_on_custom_strerror() {
+  (void)strerror(0); // no-crash
+}
+} // namespace other