Skip to content

Commit

Permalink
VG-3617: Fix some weird crash in wd integration tests (#858)
Browse files Browse the repository at this point in the history
* fix: weird crash in wd integration tests when loading libcore.

The fix is to avoid using std::regex constructor of global static variables
using lazy initialization of static variables. The crash happens when the
WD tries to load the libcore and static regex variable are being
initialized. It looks like the locale is not yet correctly defined and the
regex constructor does not accept this case.

* fix: btc transaction incorrect parsing when values are string

Handle string type for some fields in BTC transaction returned
by explorers.

* fix: possible memory leak in Base58

* ci: bump patch version
  • Loading branch information
jcoatelen-ledger authored Mar 2, 2022
1 parent aca69c9 commit 401573f
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 18 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ include_what_you_use() # add cmake conf option IWYU=ON to activate
# The project version number.
set(VERSION_MAJOR 4 CACHE STRING "Project major version number.")
set(VERSION_MINOR 1 CACHE STRING "Project minor version number.")
set(VERSION_PATCH 5 CACHE STRING "Project patch version number.")
set(VERSION_PATCH 6 CACHE STRING "Project patch version number.")
mark_as_advanced(VERSION_MAJOR VERSION_MINOR VERSION_PATCH)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY build)
Expand Down
4 changes: 2 additions & 2 deletions core/src/math/Base58.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <utils/hex.h>
#include <functional>
#include <crypto/Keccak.h>
#include <vector>

using namespace ledger::core;
static const std::string DIGITS = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
Expand Down Expand Up @@ -62,7 +63,7 @@ std::string ledger::core::Base58::encode(const std::vector<uint8_t> &bytes,
pend = len;
while (pbegin != pend && !bytes[pbegin]) pbegin = ++zeros;
const int size = 1 + iFactor * (double)(pend - pbegin);
unsigned char* b58 = new unsigned char[size];
std::vector<unsigned char> b58(size, 0);
for (int i = 0; i < size; i++) b58[i] = 0;
while (pbegin != pend) {
unsigned int carry = bytes[pbegin];
Expand All @@ -82,7 +83,6 @@ std::string ledger::core::Base58::encode(const std::vector<uint8_t> &bytes,
int ri = 0;
while (ri < zeros) { result += base58Dictionary[0]; ri++; }
for (; it2 < size; ++it2) result += base58Dictionary[b58[it2]];
delete[] b58;
return result;
}

Expand Down
11 changes: 7 additions & 4 deletions core/src/utils/DateUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ namespace {
{"Dec", "12"},
};

const std::regex PARSE_JSON_DATE_REGEX("([0-9]+)-([0-9]+)-([0-9]+)T([0-9]+):([0-9]+):([0-9]+)[\\.0-9]*([Z]?)");
const std::regex FORMAT_JSON_DATE_REGEX("([0-9]+)-([a-zA-Z]+)-([0-9]+) ([0-9]+):([0-9]+):([0-9]+)[\\.0-9]*([Z]?)");
constexpr auto PARSE_JSON_DATE_REGEX("([0-9]+)-([0-9]+)-([0-9]+)T([0-9]+):([0-9]+):([0-9]+)[\\.0-9]*([Z]?)");
constexpr auto FORMAT_JSON_DATE_REGEX("([0-9]+)-([a-zA-Z]+)-([0-9]+) ([0-9]+):([0-9]+):([0-9]+)[\\.0-9]*([Z]?)");
}

#if defined(_WIN32) || defined(_WIN64)
Expand All @@ -67,9 +67,11 @@ namespace {
namespace ledger {
namespace core {
std::chrono::system_clock::time_point ledger::core::DateUtils::fromJSON(const std::string &str) {
static const std::regex parseJsonDateRegex {PARSE_JSON_DATE_REGEX};

std::cmatch what;

if (regex_match(str.c_str(), what, PARSE_JSON_DATE_REGEX)) {
if (regex_match(str.c_str(), what, parseJsonDateRegex)) {
auto year = boost::lexical_cast<unsigned int>(std::string(what[1].first, what[1].second));
auto month = boost::lexical_cast<unsigned int>(std::string(what[2].first, what[2].second));
auto day = boost::lexical_cast<unsigned int>(std::string(what[3].first, what[3].second));
Expand Down Expand Up @@ -104,10 +106,11 @@ namespace ledger {
}

std::string ledger::core::DateUtils::formatDateFromJSON(const std::string &str) {
static const std::regex formatJsonDateRegex {FORMAT_JSON_DATE_REGEX};

std::cmatch what;

if (regex_match(str.c_str(), what, FORMAT_JSON_DATE_REGEX)) {
if (regex_match(str.c_str(), what, formatJsonDateRegex)) {
auto year = boost::lexical_cast<unsigned int>(std::string(what[1].first, what[1].second));
auto month = boost::lexical_cast<std::string>(std::string(what[2].first, what[2].second));
auto day = boost::lexical_cast<unsigned int>(std::string(what[3].first, what[3].second));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,7 @@ namespace ledger {
confirmations = -1;
}

BitcoinLikeBlockchainExplorerTransaction(const BitcoinLikeBlockchainExplorerTransaction &cpy) {
this->confirmations = cpy.confirmations;
this->version = cpy.version;
this->outputs = cpy.outputs;
this->inputs = cpy.inputs;
this->receivedAt = cpy.receivedAt;
this->lockTime = cpy.lockTime;
this->fees = cpy.fees;
this->hash = cpy.hash;
this->block = cpy.block;
}
BitcoinLikeBlockchainExplorerTransaction(const BitcoinLikeBlockchainExplorerTransaction &cpy) = default;
};

class BitcoinLikeBlockchainExplorer : public ConfigurationMatchable,
Expand Down
2 changes: 2 additions & 0 deletions core/src/wallet/bitcoin/explorers/api/InputParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ namespace ledger {
_input->coinbase = Option<std::string>(value);
} else if (_lastKey == "value") {
_input->value = Option<BigInt>(BigInt::fromString(value));
} else if (_lastKey == "sequence") {
_input->sequence = BigInt::fromString(value).toUint64();
}
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions core/src/wallet/bitcoin/explorers/api/TransactionParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,11 @@ namespace ledger {
}
else if (_lastKey == "received_at") {
_transaction->receivedAt = DateUtils::fromJSON(value);
} else if (_lastKey == "fees") {
BigInt intValue = BigInt::fromString(value);
_transaction->fees = Option<BigInt>(intValue);
}

return true;
}
}
Expand Down

0 comments on commit 401573f

Please sign in to comment.