Skip to content

Commit

Permalink
Merge pull request #1195 from cogutvalera/valera_issue_1109
Browse files Browse the repository at this point in the history
CLI wallet: avoid directly overwriting wallet file on exit #1109
  • Loading branch information
abitmore authored Aug 17, 2018
2 parents 31af7aa + d0c7a94 commit 880a00a
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion libraries/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,7 @@ class wallet_api_impl
wlog( "saving wallet to file ${fn}", ("fn", wallet_filename) );

string data = fc::json::to_pretty_string( _wallet );

try
{
enable_umask_protection();
Expand All @@ -808,14 +809,40 @@ class wallet_api_impl
//
// http://en.wikipedia.org/wiki/Most_vexing_parse
//
fc::ofstream outfile{ fc::path( wallet_filename ) };
std::string tmp_wallet_filename = wallet_filename + ".tmp";
fc::ofstream outfile{ fc::path( tmp_wallet_filename ) };
outfile.write( data.c_str(), data.length() );
outfile.flush();
outfile.close();

wlog( "saved successfully wallet to tmp file ${fn}", ("fn", tmp_wallet_filename) );

std::string wallet_file_content;
fc::read_file_contents(tmp_wallet_filename, wallet_file_content);

if (wallet_file_content == data) {
wlog( "validated successfully tmp wallet file ${fn}", ("fn", tmp_wallet_filename) );

fc::rename( tmp_wallet_filename, wallet_filename );

wlog( "renamed successfully tmp wallet file ${fn}", ("fn", tmp_wallet_filename) );
}
else
{
FC_THROW("tmp wallet file cannot be validated ${fn}", ("fn", tmp_wallet_filename) );
}

wlog( "successfully saved wallet to file ${fn}", ("fn", wallet_filename) );

disable_umask_protection();
}
catch(...)
{
string ws_password = _wallet.ws_password;
_wallet.ws_password = "";
wlog("wallet file content is next: ${data}", ("data", fc::json::to_pretty_string( _wallet ) ) );
_wallet.ws_password = ws_password;

disable_umask_protection();
throw;
}
Expand Down

0 comments on commit 880a00a

Please sign in to comment.