[clang-tools-extra] [clang][dataflow]Use cast_or_null instead of cast to prevent crash (PR #68510)

2023-10-18 Thread Yitzhak Mandelbaum via cfe-commits

https://github.com/ymand approved this pull request.

Thanks, looks good! 

You can submit as is, but if you're up for it, it would actually be better to 
add the new test case directly to the model's unittests. Something like this 
test (though just one case is enough -- please put it in a separate TEST_P): 
https://github.com/llvm/llvm-project/blob/a3a0f59a1e1cb0ac02f06b19f730ea05a6541c96/clang/unittests/Analysis/FlowSensitive/UncheckedOptionalAccessModelTest.cpp#L1966

In general, the lit tests for the clang tidy check only do minimal testing to 
ensure the check is properly calling the model.

https://github.com/llvm/llvm-project/pull/68510
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang][dataflow]Use cast_or_null instead of cast to prevent crash (PR #68510)

2023-10-18 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky updated 
https://github.com/llvm/llvm-project/pull/68510

>From a5c5fc7a17f57a0b6ae328f7138435b4aaf7f9b5 Mon Sep 17 00:00:00 2001
From: huqizhi 
Date: Sun, 8 Oct 2023 16:00:29 +0800
Subject: [PATCH] [clang][analysis]Use dyn_cast_or_null instead of 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 +-
 .../UncheckedOptionalAccessModelTest.cpp  | 18 +
 5 files changed, 45 insertions(+), 1 deletion(-)

diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 3e1fbe091c9ff6a..922c7aa42ea402c 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -209,6 +209,10 @@ Changes in existing checks
   ` check, so that it does not
   warn on macros starting with underscore and lowercase letter.
 
+- Improved :doc:`bugprone-unchecked-optional-access
+  ` check, so that it 
does
+  not crash during handling of optional values.
+
 - Improved :doc:`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  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&& 
opt) {
 
   std::forward>(opt).value();
 }
+
+namespace std {
+
+template  class vector {
+public:
+  T &operator[](unsigned long index);
+  bool empty();
+};
+
+} // namespace std
+
+struct S {
+  absl::optional x;
+};
+std::vector 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(
+  if (auto *Loc = cast_or_null(
   State.Env.getStorageLocation(*E->getArg(0 {
 createOptionalValue(*Loc, HasValueVal, State.Env);
 
diff --git 
a/clang/unittests/Analysis/FlowSensitive/UncheckedOptionalAccessModelTest.cpp 
b/clang/unittests/Analysis/FlowSensitive/UncheckedOptionalAccessModelTest.cpp
index c41a378a8341b71..76af8baba851839 100644
--- 
a/clang/unittests/Analysis/FlowSensitive/UncheckedOptionalAccessModelTest.cpp
+++ 
b/clang/unittests/Analysis/FlowSensitive/UncheckedOptionalAccessModelTest.cpp
@@ -2131,6 +2131,24 @@ TEST_P(UncheckedOptionalAccessTest, OptionalSwap) {
   )");
 }
 
+TEST_P(UncheckedOptionalAccessTest, OptionalReturnedFromFuntionCall) {
+  ExpectDiagnosticsFor(
+  R"(
+#include "unchecked_optional_access_test.h"
+
+struct S {
+  $ns::$optional x;
+} s;
+S getOptional() {
+  return s;
+}
+
+void target() {
+  getOptional().x = 0;
+}
+  )");
+}
+
 TEST_P(UncheckedOptionalAccessTest, StdSwap) {
   ExpectDiagnosticsFor(
   R"(

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang][dataflow]Use cast_or_null instead of cast to prevent crash (PR #68510)

2023-10-18 Thread Qizhi Hu via cfe-commits

jcsxky wrote:

> Thanks, looks good!
> 
> You can submit as is, but if you're up for it, it would actually be better to 
> add the new test case directly to the model's unittests. Something like this 
> test (though just one case is enough -- please put it in a separate TEST_P):
> 
> https://github.com/llvm/llvm-project/blob/a3a0f59a1e1cb0ac02f06b19f730ea05a6541c96/clang/unittests/Analysis/FlowSensitive/UncheckedOptionalAccessModelTest.cpp#L1966
> 
> In general, the lit tests for the clang tidy check only do minimal testing to 
> ensure the check is properly calling the model.

Thanks for your suggestion! New test case has been added to unittests.

https://github.com/llvm/llvm-project/pull/68510
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang][dataflow]Use cast_or_null instead of cast to prevent crash (PR #68510)

2023-10-20 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky closed https://github.com/llvm/llvm-project/pull/68510
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits