Skip to content

Commit

Permalink
[clang][dataflow]Use cast_or_null instead of cast to prevent crash (#…
Browse files Browse the repository at this point in the history
…68510)

`getStorageLocation` may return `nullptr` and this will produce crash
when use `cast`, use `dyn_cast_or_null` instead. I test it locally using
[FTXUI](https://github.com/ArthurSonzogni/FTXUI) and it may be the cause
of issue [issue](#68412), but
I am not sure.

Co-authored-by: huqizhi <huqizhi@836744285@qq.com>
  • Loading branch information
jcsxky and huqizhi committed Oct 21, 2023
1 parent bdc3e6c commit 14bc11a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
4 changes: 4 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class optional {
void reset() noexcept;

void swap(optional &rhs) noexcept;

template <typename U> optional &operator=(const U &u);
};

} // namespace absl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2131,6 +2131,24 @@ TEST_P(UncheckedOptionalAccessTest, OptionalSwap) {
)");
}

TEST_P(UncheckedOptionalAccessTest, OptionalReturnedFromFuntionCall) {
ExpectDiagnosticsFor(
R"(
#include "unchecked_optional_access_test.h"
struct S {
$ns::$optional<float> x;
} s;
S getOptional() {
return s;
}
void target() {
getOptional().x = 0;
}
)");
}

TEST_P(UncheckedOptionalAccessTest, StdSwap) {
ExpectDiagnosticsFor(
R"(

This comment has been minimized.

Copy link
@Pao2502

Pao2502 Oct 21, 2023

Afirmar

Expand Down

0 comments on commit 14bc11a

Please sign in to comment.