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

Conversation

mike-fabian
Copy link
Contributor

@mike-fabian mike-fabian commented Sep 22, 2024

See: https://unicode-org.atlassian.net/browse/ICU-22911

Checklist
  • Required: Issue filed: https://unicode-org.atlassian.net/browse/ICU-22911
  • Required: The PR title must be prefixed with a JIRA Issue number.
  • Required: The PR description must include the link to the Jira Issue, for example by completing the URL in the first checklist item
  • Required: Each commit message must be prefixed with a JIRA Issue number.
  • Issue accepted (done by Technical Committee after discussion)
  • Tests included, if applicable
  • API docs and/or User Guide docs changed or added, if applicable

@markusicu markusicu self-assigned this Sep 24, 2024
Comment on lines +248 to 252
: 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);
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/

Comment on lines +248 to 252
: 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);
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.

@markusicu markusicu assigned sffc and unassigned markusicu Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants