-
Notifications
You must be signed in to change notification settings - Fork 31
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
Replaced char array in output_stream to std::string to avoid buffer overflow #54
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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 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,69 @@ | ||
/** | ||
* @file | ||
* @copyright defined in eosio.cdt/LICENSE.txt | ||
*/ | ||
|
||
#include <cstdint> | ||
#include <cstdio> | ||
#include <string> | ||
|
||
#include <eosio/tester.hpp> | ||
#include <eosio/crt.hpp> | ||
|
||
using eosio::cdt::output_stream; | ||
|
||
EOSIO_TEST_BEGIN(output_stream_push) | ||
std_err.clear(); | ||
const char* msg = "abc"; | ||
_prints(msg, eosio::cdt::output_stream_kind::std_err); | ||
CHECK_EQUAL(std_err.to_string(), "abc"); | ||
CHECK_EQUAL(std_err.index(), 3); | ||
|
||
std_err.clear(); | ||
const char* msg2 = ""; | ||
_prints(msg2, eosio::cdt::output_stream_kind::std_err); | ||
CHECK_EQUAL(std_err.to_string(), ""); | ||
CHECK_EQUAL(std_err.index(), 0); | ||
|
||
std_err.clear(); | ||
EOSIO_TEST_END | ||
|
||
EOSIO_TEST_BEGIN(output_stream_push_overflow) | ||
std_err.clear(); | ||
const auto initial_capacity = std_err.to_string().capacity(); | ||
CHECK_EQUAL(std_err.index(), 0); | ||
|
||
std::string large_msg('x', initial_capacity + 1); | ||
|
||
_prints(large_msg.c_str(), eosio::cdt::output_stream_kind::std_err); | ||
CHECK_EQUAL(std_err.to_string().capacity() > initial_capacity, true); | ||
CHECK_EQUAL(std_err.index(), large_msg.size()); | ||
|
||
std_err.clear(); | ||
EOSIO_TEST_END | ||
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. please add 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. Done |
||
|
||
EOSIO_TEST_BEGIN(output_stream_get_and_push) | ||
std_err.clear(); | ||
std::string example_msg("abcdef"); | ||
|
||
_prints(example_msg.c_str(), eosio::cdt::output_stream_kind::std_err); | ||
CHECK_EQUAL(strcmp(std_err.get(), "abcdef") , 0); | ||
|
||
std_err.push('g'); | ||
CHECK_EQUAL(strcmp(std_err.get(), "abcdefg") , 0); | ||
|
||
std_err.clear(); | ||
EOSIO_TEST_END | ||
|
||
int main(int argc, char* argv[]) { | ||
bool verbose = false; | ||
if( argc >= 2 && std::strcmp( argv[1], "-v" ) == 0 ) { | ||
verbose = true; | ||
} | ||
silence_output(!verbose); | ||
|
||
EOSIO_TEST(output_stream_push) | ||
EOSIO_TEST(output_stream_push_overflow) | ||
EOSIO_TEST(output_stream_get_and_push) | ||
return has_failed(); | ||
} |
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.
I think it worth to check
initial_capaticy
as well. if it will be zero test should pass anyway.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.
initial capacity of string is unspecified so it can be anything. Do you want to add:
But this will be always true, because it is unsigned_int
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.
Initially I thought we can check here hardcoded 4kb size but that might be too much of details.
So this looks resolved now.