Don't rely on locale dependent functions in base_blob<BITS>::SetHex(...) (uint256), DecodeBase58(...), ParseMoney(...) and ParseHex(...)

This commit is contained in:
practicalswift 2018-10-26 18:54:30 +02:00
parent f4e4ea1cee
commit 15db77f4dd
6 changed files with 25 additions and 13 deletions

View file

@ -6,6 +6,7 @@
#include <hash.h> #include <hash.h>
#include <uint256.h> #include <uint256.h>
#include <utilstrencodings.h>
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
@ -34,7 +35,7 @@ static const int8_t mapBase58[256] = {
bool DecodeBase58(const char* psz, std::vector<unsigned char>& vch) bool DecodeBase58(const char* psz, std::vector<unsigned char>& vch)
{ {
// Skip leading spaces. // Skip leading spaces.
while (*psz && isspace(*psz)) while (*psz && IsSpace(*psz))
psz++; psz++;
// Skip and count leading '1's. // Skip and count leading '1's.
int zeroes = 0; int zeroes = 0;
@ -48,7 +49,7 @@ bool DecodeBase58(const char* psz, std::vector<unsigned char>& vch)
std::vector<unsigned char> b256(size); std::vector<unsigned char> b256(size);
// Process the characters. // Process the characters.
static_assert(sizeof(mapBase58)/sizeof(mapBase58[0]) == 256, "mapBase58.size() should be 256"); // guarantee not out of range static_assert(sizeof(mapBase58)/sizeof(mapBase58[0]) == 256, "mapBase58.size() should be 256"); // guarantee not out of range
while (*psz && !isspace(*psz)) { while (*psz && !IsSpace(*psz)) {
// Decode base58 character // Decode base58 character
int carry = mapBase58[(uint8_t)*psz]; int carry = mapBase58[(uint8_t)*psz];
if (carry == -1) // Invalid b58 character if (carry == -1) // Invalid b58 character
@ -64,7 +65,7 @@ bool DecodeBase58(const char* psz, std::vector<unsigned char>& vch)
psz++; psz++;
} }
// Skip trailing spaces. // Skip trailing spaces.
while (isspace(*psz)) while (IsSpace(*psz))
psz++; psz++;
if (*psz != 0) if (*psz != 0)
return false; return false;

View file

@ -29,7 +29,7 @@ void base_blob<BITS>::SetHex(const char* psz)
memset(data, 0, sizeof(data)); memset(data, 0, sizeof(data));
// skip leading spaces // skip leading spaces
while (isspace(*psz)) while (IsSpace(*psz))
psz++; psz++;
// skip 0x // skip 0x

View file

@ -41,7 +41,7 @@ bool ParseMoney(const char* pszIn, CAmount& nRet)
std::string strWhole; std::string strWhole;
int64_t nUnits = 0; int64_t nUnits = 0;
const char* p = pszIn; const char* p = pszIn;
while (isspace(*p)) while (IsSpace(*p))
p++; p++;
for (; *p; p++) for (; *p; p++)
{ {
@ -56,14 +56,14 @@ bool ParseMoney(const char* pszIn, CAmount& nRet)
} }
break; break;
} }
if (isspace(*p)) if (IsSpace(*p))
break; break;
if (!isdigit(*p)) if (!isdigit(*p))
return false; return false;
strWhole.insert(strWhole.end(), *p); strWhole.insert(strWhole.end(), *p);
} }
for (; *p; p++) for (; *p; p++)
if (!isspace(*p)) if (!IsSpace(*p))
return false; return false;
if (strWhole.size() > 10) // guard against 63 bit overflow if (strWhole.size() > 10) // guard against 63 bit overflow
return false; return false;

View file

@ -85,7 +85,7 @@ std::vector<unsigned char> ParseHex(const char* psz)
std::vector<unsigned char> vch; std::vector<unsigned char> vch;
while (true) while (true)
{ {
while (isspace(*psz)) while (IsSpace(*psz))
psz++; psz++;
signed char c = HexDigit(*psz++); signed char c = HexDigit(*psz++);
if (c == (signed char)-1) if (c == (signed char)-1)
@ -266,7 +266,7 @@ static bool ParsePrechecks(const std::string& str)
{ {
if (str.empty()) // No empty string allowed if (str.empty()) // No empty string allowed
return false; return false;
if (str.size() >= 1 && (isspace(str[0]) || isspace(str[str.size()-1]))) // No padding allowed if (str.size() >= 1 && (IsSpace(str[0]) || IsSpace(str[str.size()-1]))) // No padding allowed
return false; return false;
if (str.size() != strlen(str.c_str())) // No embedded NUL characters allowed if (str.size() != strlen(str.c_str())) // No embedded NUL characters allowed
return false; return false;

View file

@ -71,6 +71,21 @@ constexpr bool IsDigit(char c)
return c >= '0' && c <= '9'; return c >= '0' && c <= '9';
} }
/**
* Tests if the given character is a whitespace character. The whitespace characters
* are: space, form-feed ('\f'), newline ('\n'), carriage return ('\r'), horizontal
* tab ('\t'), and vertical tab ('\v').
*
* This function is locale independent. Under the C locale this function gives the
* same result as std::isspace.
*
* @param[in] c character to test
* @return true if the argument is a whitespace character; otherwise false
*/
constexpr inline bool IsSpace(char c) noexcept {
return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v';
}
/** /**
* Convert string to signed 32-bit integer with strict parse error feedback. * Convert string to signed 32-bit integer with strict parse error feedback.
* @returns true if the entire string could be parsed as valid integer, * @returns true if the entire string could be parsed as valid integer,

View file

@ -2,7 +2,6 @@
export LC_ALL=C export LC_ALL=C
KNOWN_VIOLATIONS=( KNOWN_VIOLATIONS=(
"src/base58.cpp:.*isspace"
"src/bitcoin-tx.cpp.*stoul" "src/bitcoin-tx.cpp.*stoul"
"src/bitcoin-tx.cpp.*trim_right" "src/bitcoin-tx.cpp.*trim_right"
"src/bitcoin-tx.cpp:.*atoi" "src/bitcoin-tx.cpp:.*atoi"
@ -18,15 +17,12 @@ KNOWN_VIOLATIONS=(
"src/test/getarg_tests.cpp.*split" "src/test/getarg_tests.cpp.*split"
"src/torcontrol.cpp:.*atoi" "src/torcontrol.cpp:.*atoi"
"src/torcontrol.cpp:.*strtol" "src/torcontrol.cpp:.*strtol"
"src/uint256.cpp:.*isspace"
"src/uint256.cpp:.*tolower" "src/uint256.cpp:.*tolower"
"src/util.cpp:.*atoi" "src/util.cpp:.*atoi"
"src/util.cpp:.*fprintf" "src/util.cpp:.*fprintf"
"src/util.cpp:.*tolower" "src/util.cpp:.*tolower"
"src/utilmoneystr.cpp:.*isdigit" "src/utilmoneystr.cpp:.*isdigit"
"src/utilmoneystr.cpp:.*isspace"
"src/utilstrencodings.cpp:.*atoi" "src/utilstrencodings.cpp:.*atoi"
"src/utilstrencodings.cpp:.*isspace"
"src/utilstrencodings.cpp:.*strtol" "src/utilstrencodings.cpp:.*strtol"
"src/utilstrencodings.cpp:.*strtoll" "src/utilstrencodings.cpp:.*strtoll"
"src/utilstrencodings.cpp:.*strtoul" "src/utilstrencodings.cpp:.*strtoul"