-
Notifications
You must be signed in to change notification settings - Fork 273
SEC-89 Fix messaget's copy-constructor and operator= #1464
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
Merged
smowton
merged 1 commit into
diffblue:develop
from
smowton:smowton/fix/messaget_copy_and_assign_operators
Oct 11, 2017
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -154,10 +154,17 @@ class messaget | |
|
||
messaget(const messaget &other): | ||
message_handler(other.message_handler), | ||
mstream(other.mstream) | ||
mstream(other.mstream, *this) | ||
{ | ||
} | ||
|
||
messaget &operator=(const messaget &other) | ||
{ | ||
message_handler=other.message_handler; | ||
mstream.assign_from(other.mstream); | ||
return *this; | ||
} | ||
|
||
explicit messaget(message_handlert &_message_handler): | ||
message_handler(&_message_handler), | ||
mstream(M_DEBUG, *this) | ||
|
@@ -177,13 +184,17 @@ class messaget | |
{ | ||
} | ||
|
||
mstreamt(const mstreamt &other): | ||
mstreamt(const mstreamt &other)=delete; | ||
|
||
mstreamt(const mstreamt &other, messaget &_message): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is it useful for, it's not even tested. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Called from |
||
message_level(other.message_level), | ||
message(other.message), | ||
message(_message), | ||
source_location(other.source_location) | ||
{ | ||
} | ||
|
||
mstreamt &operator=(const mstreamt &other)=delete; | ||
|
||
unsigned message_level; | ||
messaget &message; | ||
source_locationt source_location; | ||
|
@@ -220,6 +231,16 @@ class messaget | |
{ | ||
return func(*this); | ||
} | ||
|
||
private: | ||
void assign_from(const mstreamt &other) | ||
{ | ||
message_level=other.message_level; | ||
source_location=other.source_location; | ||
// message, the pointer to my enclosing messaget, remains unaltered. | ||
} | ||
|
||
friend class messaget; | ||
}; | ||
|
||
// Feeding 'eom' into the stream triggers | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/*******************************************************************\ | ||
|
||
Module: Messaget tests | ||
|
||
Author: Diffblue Limited. All rights reserved. | ||
|
||
\*******************************************************************/ | ||
|
||
#include <testing-utils/catch.hpp> | ||
#include <util/message.h> | ||
#include <sstream> | ||
#include <string.h> | ||
|
||
TEST_CASE("Copy a messaget") | ||
{ | ||
std::ostringstream sstream1, sstream2; | ||
stream_message_handlert handler1(sstream1), handler2(sstream2); | ||
|
||
messaget msg1(handler1); | ||
|
||
// Copy messaget: | ||
messaget msg2(msg1); | ||
|
||
// Change its handler: | ||
msg2.set_message_handler(handler2); | ||
|
||
msg2.status() << "Test" << messaget::eom; | ||
|
||
CHECK(sstream1.str()==""); | ||
CHECK(sstream2.str()=="Test\n"); | ||
} | ||
|
||
TEST_CASE("Assign a messaget") | ||
{ | ||
std::ostringstream sstream1, sstream2; | ||
stream_message_handlert handler1(sstream1), handler2(sstream2); | ||
|
||
messaget msg1(handler1); | ||
|
||
// Assign messaget: | ||
messaget msg2; | ||
msg2=msg1; | ||
|
||
// Change its handler: | ||
msg2.set_message_handler(handler2); | ||
|
||
msg2.status() << "Test" << messaget::eom; | ||
|
||
CHECK(sstream1.str()==""); | ||
CHECK(sstream2.str()=="Test\n"); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why are you deleting copy-constructor and adding copy-assignment? Either none or both should be available. Follow The Rule of Five.
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'm not.
messaget
's copy-constructor is fixed and gains a matchingoperator=
(replacing its implicit one, which was also incorrect mirroring its existing explicit copy constructor); its internal classmstreamt
is made neither copyable nor assignable (it should always move around with its enclosing messaget)