Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions icu4c/source/i18n/numrange_fluent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,10 @@ LocalizedNumberRangeFormatter::LocalizedNumberRangeFormatter(LocalizedNumberRang
: LNF(static_cast<NFS<LNF>&&>(src)) {}

LocalizedNumberRangeFormatter::LocalizedNumberRangeFormatter(NFS<LNF>&& src) noexcept
: NFS<LNF>(std::move(src)) {
: 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);
Comment on lines +248 to 252
Copy link
Member

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.

Copy link
Member

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.

Copy link
Member

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?

Copy link
Member

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:

https://cplusplus.com/forum/beginner/187808/

}

Expand Down
Loading