-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[clang][dataflow]Use cast_or_null instead of cast to prevent crash #68510
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@llvm/pr-subscribers-clang-tidy @llvm/pr-subscribers-clang-analysis Changes
Full diff: https://github.com/llvm/llvm-project/pull/68510.diff 1 Files Affected:
diff --git a/clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp b/clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp
index f61f26ff27804ec..dda1c6cfca017d0 100644
--- a/clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp
+++ b/clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp
@@ -26,6 +26,7 @@
#include "clang/Analysis/FlowSensitive/NoopLattice.h"
#include "clang/Analysis/FlowSensitive/StorageLocation.h"
#include "clang/Analysis/FlowSensitive/Value.h"
+#include "clang/Basic/LLVM.h"
#include "clang/Basic/SourceLocation.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Casting.h"
@@ -599,7 +600,7 @@ void transferAssignment(const CXXOperatorCallExpr *E, BoolValue &HasValueVal,
LatticeTransferState &State) {
assert(E->getNumArgs() > 0);
- if (auto *Loc = cast<RecordStorageLocation>(
+ if (auto *Loc = dyn_cast_or_null<RecordStorageLocation>(
State.Env.getStorageLocation(*E->getArg(0)))) {
createOptionalValue(*Loc, HasValueVal, State.Env);
|
870856a
to
b0f1344
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fix looks great to me! On the other hand, we usually try to add a regression test for each of the fixes. Any chance you could get a minimal reproducer from the codebase you are looking at?
b0f1344
to
2de9679
Compare
Thanks for your suggestion! Test case has been added to reproduce this issue. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please update release note also
clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp
Outdated
Show resolved
Hide resolved
Although |
2de9679
to
3978a43
Compare
3978a43
to
7a8c515
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM,
When release notes could be nice (clang-tools-extra/doc/ReleaseNotest.rst) with something like: Improved bugprone-unchecked-optional-acces check to not crash during handling of optional values
or to not crash i certain situations
it may also not be so necessary, as this is just very quick fix.
38cf358
to
466b612
Compare
Release notes have been updated according to your suggestion. Thank you for detail guidance! |
466b612
to
eb9f834
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this fix! Unfortunately, I wasn't able to repro the crash in godbolt: https://godbolt.org/z/s741z5djY. Can you double check that the check crashes on that example without your fix?
clang-tools-extra/test/clang-tidy/checkers/bugprone/unchecked-optional-access.cpp
Outdated
Show resolved
Hide resolved
clang-tools-extra/test/clang-tidy/checkers/bugprone/unchecked-optional-access.cpp
Outdated
Show resolved
Hide resolved
clang-tools-extra/test/clang-tidy/checkers/bugprone/unchecked-optional-access.cpp
Outdated
Show resolved
Hide resolved
clang-tools-extra/test/clang-tidy/checkers/bugprone/unchecked-optional-access.cpp
Outdated
Show resolved
Hide resolved
clang-tools-extra/test/clang-tidy/checkers/bugprone/unchecked-optional-access.cpp
Outdated
Show resolved
Hide resolved
clang-tools-extra/test/clang-tidy/checkers/bugprone/unchecked-optional-access.cpp
Outdated
Show resolved
Hide resolved
The test case is different from that in this patch. Please Use unless(hasDeclaration(cxxMethodDecl(
anyOf(isCopyAssignmentOperator(), isMoveAssignmentOperator())))) in vec[0].x = 0; so, flow can't enter into |
eb9f834
to
55ca8fa
Compare
@ymand Could you take a look at this pr? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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):
llvm-project/clang/unittests/Analysis/FlowSensitive/UncheckedOptionalAccessModelTest.cpp
Line 1966 in a3a0f59
TEST_P(UncheckedOptionalAccessTest, ValueAssignment) { |
In general, the lit tests for the clang tidy check only do minimal testing to ensure the check is properly calling the model.
55ca8fa
to
a5c5fc7
Compare
Thanks for your suggestion! New test case has been added to unittests. |
getStorageLocation
may returnnullptr
and this will produce crash when usecast
, usedyn_cast_or_null
instead. I test it locally using FTXUI and it may be the cause of issue issue, but I am not sure.