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

fixed stack-use-after-scope when assigning message_reader #32

Merged
merged 1 commit into from
Oct 11, 2020
Merged
Show file tree
Hide file tree
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
24 changes: 22 additions & 2 deletions include/hffix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1953,16 +1953,36 @@ class message_reader {
is_valid_(true) {
init();
}

/*!
* \brief Copy constructor. The hffix::message_reader is immutable, so copying it is fine.
*/
message_reader(message_reader const& that) :
buffer_(that.buffer_),
buffer_end_(that.buffer_end_),
begin_(that.begin_),
end_(that.end_),
begin_(*this, 0),
end_(*this, 0),
is_complete_(that.is_complete_),
is_valid_(that.is_valid_) {
init();
}

/*!
* \brief Assignment operator.
*
* This can't be the default assignment operator because begin_ and end_ are const_iterators which
* point back to 'this'. If we copy them as is they will point back to the previous 'that'.
*/
message_reader& operator = (const message_reader& that)
{
buffer_= that.buffer_;
buffer_end_ = that.buffer_end_;
begin_ = const_iterator(*this, 0);
end_ = const_iterator(*this, 0);
is_complete_ = that.is_complete_;
is_valid_ = that.is_valid_;
init();
return *this;
}

/*!
Expand Down
25 changes: 25 additions & 0 deletions test/src/unit_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ using namespace hffix;
#include <chrono>
#include <iomanip>
#endif
#include <iterator>

BOOST_AUTO_TEST_CASE(basic)
{
Expand Down Expand Up @@ -258,6 +259,30 @@ BOOST_AUTO_TEST_CASE(data_length)
BOOST_CHECK_EQUAL(i->value().as_string(), datum);
}

BOOST_AUTO_TEST_CASE(iterating)
{
char b[1024];
char* ptr = b;
unsigned num = 0;
for(size_t i = 0; i < 10; i++ ) {
message_writer w(ptr, 1024 - (ptr - b));
w.push_back_header("FIX.4.2");
w.push_back_string(tag::MsgType, "A");
w.push_back_trailer();
ptr += w.message_size();
}

for (message_reader reader(b);
reader.is_complete();
reader = reader.next_message_reader()) {
if(reader.is_complete() && reader.is_valid()) {
BOOST_CHECK_EQUAL(std::distance(reader.begin(), reader.end()), 1);
num++;
}
}
BOOST_CHECK_EQUAL(num, 10u);
}

#if __cplusplus >= 201103L
// test that std::chrono values can be written and read correctly
BOOST_AUTO_TEST_CASE(chrono)
Expand Down