-
-
Notifications
You must be signed in to change notification settings - Fork 743
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
ICU-22911 Fix coverity warning in numrange_fluent.cpp #3202
base: main
Are you sure you want to change the base?
ICU-22911 Fix coverity warning in numrange_fluent.cpp #3202
Conversation
: NFS<LNF>(std::move(src)) { | ||
// Safely access the member from *this, which holds the moved-from state | ||
// Steal the compiled formatter | ||
LNF&& _src = static_cast<LNF&&>(src); | ||
auto* stolen = _src.fAtomicFormatter.exchange(nullptr); | ||
auto* stolen = this->fAtomicFormatter.exchange(nullptr); | ||
delete fAtomicFormatter.exchange(stolen); |
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.
Good catch on use-after-move, but... now we are stealing from ourselves, and putting the stolen pointer right back where it came from??
Do we need to futz with the fAtomicFormatter at all in this move copy constructor? If that got moved, then "this" should have the right state, right?
If we do need to futz with it, then I suspect we need to do it before moving stuff from src.
@aheninger @sffc please chime in. I don't have context for how these objects are built.
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.
I think that the pre-existing code is completely fine. fAtomicFormatter
is a field of LNF but not NFS. We read only that field, not any other fields.
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.
@sffc coverity complains that we are using src
after it's been subject to std::move
. This seems like a fair complaint.
Could we std::move just the NFS slice of src?
Could we move the formatter before moving src?
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.
I don't think it is a fair complaint. As far as I can tell, C++ does not offer a mechanism to mark only part of an object as having been moved (unlike Rust which provides such a mechanism). I found a thread that says that what this code is doing is the correct pattern when you have a child class calling the move constructor of a base class, although I don't know the credibility of the people responding on this thread:
: NFS<LNF>(std::move(src)) { | ||
// Safely access the member from *this, which holds the moved-from state | ||
// Steal the compiled formatter | ||
LNF&& _src = static_cast<LNF&&>(src); | ||
auto* stolen = _src.fAtomicFormatter.exchange(nullptr); | ||
auto* stolen = this->fAtomicFormatter.exchange(nullptr); | ||
delete fAtomicFormatter.exchange(stolen); |
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.
I think that the pre-existing code is completely fine. fAtomicFormatter
is a field of LNF but not NFS. We read only that field, not any other fields.
@sffc OK, so that means that the coverity warning is a false positive, right? That is reassuring. But would my patch help to avoid the coverity warning, or is is wrong? Or should a cmment to suppress the coverity warning be added? |
The patch as it currently stands is wrong; @markusicu explained why here: #3202 (comment) It might be that there's a way to write this code that doesn't trigger the coverity warning, and doing so would be nice if possible. I'm not that familiar with how Coverity works. |
I don't know much about coverity either. Adding this comment might remove the coverity warning: --- a/icu4c/source/i18n/numrange_fluent.cpp
+++ b/icu4c/source/i18n/numrange_fluent.cpp
@@ -239,6 +239,7 @@ LocalizedNumberRangeFormatter::LocalizedNumberRangeFormatter(LocalizedNumberRang
LocalizedNumberRangeFormatter::LocalizedNumberRangeFormatter(NFS<LNF>&& src) noexcept
: NFS<LNF>(std::move(src)) {
// Steal the compiled formatter
+ // coverity[use_after_move]
LNF&& _src = static_cast<LNF&&>(src);
auto* stolen = _src.fAtomicFormatter.exchange(nullptr);
delete fAtomicFormatter.exchange(stolen); |
See: https://unicode-org.atlassian.net/browse/ICU-22911
Checklist