Commit ef537b04 authored by artem-bv's avatar artem-bv

fix metadata size validation checks on nft proto registration

parent c4e60414
......@@ -82,7 +82,7 @@ namespace Platform
NftProtocolRegTxBuilder & SetMaxMetadataSize(const json_spirit::Value & value)
{
m_nftProto.maxMetadataSize = ParseInt32V(value, "maxMetadataSize");
m_nftProto.maxMetadataSize = ParseUInt8V(value, "maxMetadataSize");
return *this;
}
......
......@@ -152,6 +152,24 @@ int32_t ParseInt32V(const Value& v, const std::string &strName)
return num;
}
uint32_t ParseUInt32V(const Value& v, const std::string &strName)
{
std::string strNum = v.get_str();
uint32_t num;
if (!ParseUInt32(strNum, &num))
throw JSONRPCError(RPC_INVALID_PARAMETER, strName+" must be a 32bit unsigned integer (not '"+strNum+"')");
return num;
}
uint8_t ParseUInt8V(const Value& v, const std::string &strName)
{
std::string strNum = v.get_str();
uint8_t num;
if (!ParseUInt8(strNum, &num))
throw JSONRPCError(RPC_INVALID_PARAMETER, strName+" must be an 8bit unsigned integer (not '"+strNum+"')");
return num;
}
bool ParseBoolV(const Value& v, const std::string &strName)
{
std::string strBool;
......
......@@ -126,6 +126,8 @@ extern std::vector<unsigned char> ParseHexV(const json_spirit::Value& v, std::st
extern std::vector<unsigned char> ParseHexO(const json_spirit::Object& o, std::string strKey);
extern int32_t ParseInt32V(const json_spirit::Value& v, const std::string &strName);
extern uint32_t ParseUInt32V(const json_spirit::Value& v, const std::string &strName);
extern uint8_t ParseUInt8V(const json_spirit::Value& v, const std::string &strName);
extern bool ParseBoolV(const json_spirit::Value& v, const std::string &strName);
extern void InitRPCMining();
......
......@@ -435,6 +435,32 @@ bool ParseInt32(const std::string& str, int32_t *out)
n <= std::numeric_limits<int32_t>::max();
}
bool ParseUInt32(const std::string& str, uint32_t *out) {
char *endp = NULL;
errno = 0; // strtol will not set errno if valid
unsigned long int n = strtoul(str.c_str(), &endp, 10);
if(out)
*out = (unsigned int)n;
// Note that strtol returns a *long int*, so even if strtol doesn't report a over/underflow
// we still have to check that the returned value is within the range of an *int32_t*. On 64-bit
// platforms the size of these types may be different.
return endp && *endp == 0 && !errno &&
n >= std::numeric_limits<uint32_t >::min() &&
n <= std::numeric_limits<uint32_t >::max();
}
bool ParseUInt8(const std::string& str, uint8_t *out) {
uint32_t internalOut;
if (!ParseUInt32(str, &internalOut)) {
return false;
}
if (out && internalOut >= std::numeric_limits<uint8_t >::min() && internalOut <= std::numeric_limits<uint8_t >::max()) {
*out = static_cast<uint8_t >(internalOut);
return true;
}
return false;
}
std::string FormatParagraph(const std::string in, size_t width, size_t indent)
{
std::stringstream out;
......
......@@ -48,6 +48,8 @@ int atoi(const std::string& str);
* false if not the entire string could be parsed or when overflow or underflow occurred.
*/
bool ParseInt32(const std::string& str, int32_t *out);
bool ParseUInt32(const std::string& str, uint32_t *out);
bool ParseUInt8(const std::string& str, uint8_t *out);
template<typename T>
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment