Allocator propagation broken for std::string move assignment
| Bugzilla Link | 48439 |
| Version | 10.0 |
| OS | All |
| CC | @mclow |
Extended Description
Consider the case where
propagate on container copy assignment = true propagate on container move assignment = false
and focus on move assignment.
Suppose that the (stateful) instances of the allocators are not equal to one another.
In this case, __move_assign delegates to assign. As far as I can tell, this delegates to copy assignment. However, the latter considers propagation on container copy and not container move.
This gives the wrong outcome in the case described.
Do you have a reproducer?
@ojrosten Any chance of getting a reproducer? That would be really helpful.
Hi Louis,
Here's a minimal reproduction: https://godbolt.org/z/a9oTohd8K
Here is a smaller (and IMO less confusing) reproducer: https://godbolt.org/z/eP1Tz7TKW
Thanks!
@jwakely It looks like there's a similar problem with libstdc++, but it takes bit more effort to reveal it:
https://godbolt.org/z/h7avPfqhz
I think what's going on is as follows. Referring to basic_string.h [a523c2b]:
-
Looking at move assignment, because the allocators aren't equal, we're not propagating on move assignment, and _M_is_local() returns false, we end up in the final else.
-
This dispatches to assign(const basic_string& __str) which is a problem because POCCA is true. The predicates for both nested ifs are false, so we go into the else on line 1607. The bug is harder to expose compared to libc++ because of the copy of the allocator taken on line 1610. But using a shared_ptr reveals the issue.
propagate on container copy assignment = true
propagate on container move assignment = false
Don't do that.
This gives the wrong outcome in the case described.
Your propagation traits are meaningless.
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0177r2.html#2.0 describes why doing that is bad, but changes to the standard to say "don't do that" were deferred (indefinitely).
Looks like the libstdc++ bug was introduced by https://gcc.gnu.org/g:db33efde1793 because before that, assign did not consider POCCA, so calling assign from the move assignment op was fine. The fix for libstdc++ is for the move assignment op to call _M_assign(__str) instead of assign(__str), as _M_assign does the actual deep copy and doesn't consider POCCA.
But it's still a meaningless combination of propagation traits.
Yeah, I had a feeling it was a regression but wasn't 100% sure. I do agree that it's hard to imagine a use case for this combination of propagation traits. Even so, it's not obvious to me that Alisdair's paper is saying that it shouldn't work in this case, since basic_string does have a move assignment operator.
"The issue with copy-assignment is a little more subtle, but for a type that provides a copy-assignment operator but no move- assignment operator, then, when move-assignment is requested, the copy-assignment operator will be called. If the propagation traits of the user-supplied allocator differ, then problems will follow."
Then again, having all three traits be equivalent would be a huge simplification that would certainly have saved me a lot of pain in the past!
The seemingly incorrect call to assign is currently here:
https://github.com/llvm/llvm-project/blob/caebb4562ce634a22f7b13480b19cffc2a6a6730/libcxx/include/string#L2678-L2685
IIUC there's a one-liner fix: changing assign(__str) to assign(__str.data(), __str.size()). It seems that the change will also make ~debug builds a bit faster~ debugging a bit simpler as the call stack will be made smaller.
propagate on container copy assignment = true propagate on container move assignment = false
Don't do that.
This gives the wrong outcome in the case described.
Your propagation traits are meaningless.
It seems to me that the most performant propagation traits would be inconsistent.
Given vector<T, A> where A is stateful, if we want move and swap to transfer memory recources, POCMA and POCS should be true. Then consider copy assignment between two vector<T, A>s, we might want to avoid reallocation for elements when lhs.capacity() >= rhs.size(). This can always be done only if POCCA is false.
But POCCA true and POCMA false is not useful