Skip to content

Replaced char array in output_stream to std::string to avoid buffer overflow#54

Merged
mikelik merged 3 commits intomainfrom
output_stream_overflow
Nov 9, 2022
Merged

Replaced char array in output_stream to std::string to avoid buffer overflow#54
mikelik merged 3 commits intomainfrom
output_stream_overflow

Conversation

@mikelik
Copy link
Contributor

@mikelik mikelik commented Nov 3, 2022

Change Description

Solving #12
Replaced char buffer with std::string. Initial capacity of string is set to 4096.
Using output_stream::push should no longer cause buffer overflow.

Added unit tests for overflow.

API Changes

  • API Changes

Documentation Additions

  • Documentation Additions

@mikelik mikelik self-assigned this Nov 3, 2022
@mikelik mikelik linked an issue Nov 3, 2022 that may be closed by this pull request
@mikelik mikelik linked an issue Nov 3, 2022 that may be closed by this pull request
@mikelik mikelik requested a review from larryk85 November 3, 2022 12:27
@mikelik mikelik marked this pull request as draft November 3, 2022 14:58
@mikelik mikelik marked this pull request as ready for review November 4, 2022 10:58
@mikelik mikelik changed the title Add overflow check to output_stream Replaced char array in output_stream to std::string to avoid buffer overflow Nov 7, 2022
@mikelik mikelik force-pushed the output_stream_overflow branch from e62ade2 to 5ed84b1 Compare November 8, 2022 09:59
@mikelik mikelik requested a review from dimas1185 November 8, 2022 15:09
CHECK_EQUAL(std_err.index(), large_msg.size());

std_err.clear();
EOSIO_TEST_END
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add get() and push() tests. I see clear(), to_string() and 'index()' calls only

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


EOSIO_TEST_BEGIN(output_stream_push_overflow)
std_err.clear();
const auto initial_capacity = std_err.to_string().capacity();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it worth to check initial_capaticy as well. if it will be zero test should pass anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initial capacity of string is unspecified so it can be anything. Do you want to add:

Suggested change
const auto initial_capacity = std_err.to_string().capacity();
const auto initial_capacity = std_err.to_string().capacity();
CHECK_EQUAL(initial_capacity >= 0, true);

But this will be always true, because it is unsigned_int

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially I thought we can check here hardcoded 4kb size but that might be too much of details.
So this looks resolved now.

Michal Lesiak added 3 commits November 9, 2022 08:50
… and std_err) is capped at 2048.

Trying to add more data to output_stream will be silently ignored.
Add unit tests for overflow.
Added unit test for get and push.
@mikelik mikelik force-pushed the output_stream_overflow branch from 5ed84b1 to bb3f985 Compare November 9, 2022 10:46

public:
output_stream() { output.reserve(initial_size); }
std::string to_string() const { return output; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was copy an intention here?
const reference might be an alternative

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, after we talked yesterday I have changed it, so now it is const ref.
I believe originally it was a copy, because the underlying was a char array.


EOSIO_TEST_BEGIN(output_stream_push_overflow)
std_err.clear();
const auto initial_capacity = std_err.to_string().capacity();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially I thought we can check here hardcoded 4kb size but that might be too much of details.
So this looks resolved now.

@mikelik mikelik merged commit 7444ccb into main Nov 9, 2022
@mikelik mikelik deleted the output_stream_overflow branch November 9, 2022 15:24
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.

[native] Buffer overflow bug writting to output_stream

3 participants