[clang] [clang][dataflow]Use cast_or_null instead cast to prevent crash (PR #68510)
Qizhi Hu via cfe-commits
cfe-commits at lists.llvm.org
Tue Oct 10 19:29:43 PDT 2023
https://github.com/jcsxky updated https://github.com/llvm/llvm-project/pull/68510
>From 55ca8fa197e469a7c8f57d7a174c07a063eb022e Mon Sep 17 00:00:00 2001
From: huqizhi <huqizhi at feysh.com>
Date: Sun, 8 Oct 2023 16:00:29 +0800
Subject: [PATCH] [clang][analysis]Use dyn_cast_or_null instead cast to prevent
crash
---
clang-tools-extra/docs/ReleaseNotes.rst | 4 ++++
.../absl/types/optional.h | 2 ++
.../bugprone/unchecked-optional-access.cpp | 20 +++++++++++++++++++
.../Models/UncheckedOptionalAccessModel.cpp | 2 +-
4 files changed, 27 insertions(+), 1 deletion(-)
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index c1b926b296b055a..837fd6ca1b61173 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -202,6 +202,10 @@ Changes in existing checks
<clang-tidy/checks/bugprone/reserved-identifier>` check, so that it does not
warn on macros starting with underscore and lowercase letter.
+- Improved :doc:`bugprone-unchecked-optional-access
+ <clang-tidy/checks/bugprone/unchecked-optional-access>` check, so that it does
+ not crash during handling of optional values.
+
- Improved :doc:`bugprone-undefined-memory-manipulation
<clang-tidy/checks/bugprone/undefined-memory-manipulation>` check to support
fixed-size arrays of non-trivial types.
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/Inputs/unchecked-optional-access/absl/types/optional.h b/clang-tools-extra/test/clang-tidy/checkers/bugprone/Inputs/unchecked-optional-access/absl/types/optional.h
index 154cc262ab7cd79..3e692f347aa1ea5 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/Inputs/unchecked-optional-access/absl/types/optional.h
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/Inputs/unchecked-optional-access/absl/types/optional.h
@@ -66,6 +66,8 @@ class optional {
void reset() noexcept;
void swap(optional &rhs) noexcept;
+
+ template <typename U> optional &operator=(const U &u);
};
} // namespace absl
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unchecked-optional-access.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unchecked-optional-access.cpp
index 1921291f2187d92..13a3ff52f3ebc59 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unchecked-optional-access.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unchecked-optional-access.cpp
@@ -180,3 +180,23 @@ void std_forward_rvalue_ref_safe(absl::optional<int>&& opt) {
std::forward<absl::optional<int>>(opt).value();
}
+
+namespace std {
+
+template <typename T> class vector {
+public:
+ T &operator[](unsigned long index);
+ bool empty();
+};
+
+} // namespace std
+
+struct S {
+ absl::optional<float> x;
+};
+std::vector<S> vec;
+
+void foo() {
+ if (!vec.empty())
+ vec[0].x = 0;
+}
diff --git a/clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp b/clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp
index f61f26ff27804ec..8bd9a030f50cda0 100644
--- a/clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp
+++ b/clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp
@@ -599,7 +599,7 @@ void transferAssignment(const CXXOperatorCallExpr *E, BoolValue &HasValueVal,
LatticeTransferState &State) {
assert(E->getNumArgs() > 0);
- if (auto *Loc = cast<RecordStorageLocation>(
+ if (auto *Loc = cast_or_null<RecordStorageLocation>(
State.Env.getStorageLocation(*E->getArg(0)))) {
createOptionalValue(*Loc, HasValueVal, State.Env);
More information about the cfe-commits
mailing list