Opened 10 years ago
Closed 5 years ago
#8268 closed Bugs (fixed)
basic_hold_any missing assignment operator
Reported by: | Owned by: | Joel de Guzman | |
---|---|---|---|
Milestone: | To Be Determined | Component: | spirit |
Version: | Boost Development Trunk | Severity: | Problem |
Keywords: | hold_any | Cc: |
Description
Self contatined repro:
struct Big {
int x[ 4 ];
}; (big enough to prevent the small optimisation in hold_any)
{
boost::spirit::hold_any sourceAny = boost::spirit::hold_any( Big() );
boost::spirit::hold_any copyInto;
copyInto = sourceAny;
(calls a compiler generated assignment operator which copies the address of the memory allocated in sourceAny)
} CRASH: double deletion of the heap allocated Big held in sourceAny as both copyInto and sourceAny go out of scope
The problem is that the assignment operator provided in basic_hold_any doesn't suppress the compiler generated one because it is a template function, and the compiler generator one only does a shallow copy.
The relevant section in the C++ standard is N3485 §12.18:
"A user-declared copy assignment operator X::operator= is a non-static non-template member function of class X with exactly one parameter of type X, X&, const X&, volatile X& or const volatile X&. (121)"
"(121) Because a template assignment operator or an assignment operator taking an rvalue reference parameter is never a copy assignment operator, the presence of such an assignment operator does not suppress the implicit declaration of a copy assignment operator. Such assignment operators participate in overload resolution with other assignment operators, including copy assignment operators, and, if selected, will be used to assign an object."
To fix this issue, simply add the non-template assignment constructor to basic_hold_any:
assignment operator basic_hold_any& operator=(basic_hold_any const& x) {
return assign(x);
}
Attachments (1)
Change History (7)
comment:1 by , 10 years ago
by , 10 years ago
Attachment: | msouth_ticket8268.patch added |
---|
Patch created in /trunk/boost/spirit/home/support/detail/ as described in bug report.
comment:2 by , 10 years ago
I've attached a patch but I've not run the tests (other than my own) as my current environment is not setup to run them (I'm not a boost developer).
comment:3 by , 9 years ago
I ran into the same issue. Thanks for the fix.
When will this reach the official boost release?
I'm not a boost developer and don't know what is required to make "sure all tests pass". I take it this is what is required: "Otherwise, create a temporary branch in subversion, make your changes there, and ask the library author(s)/maintainer(s) to review them; if approve the new code, either you or they can integrate the fixes into the main trunk."
Is that right?
comment:5 by , 5 years ago
Have been fixed for C++11 in https://github.com/boostorg/spirit/pull/36. Fixed completely in https://github.com/boostorg/spirit/pull/361.
comment:6 by , 5 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Indeed. Please provide a patch making sure all tests pass. Thanks!