From 2d7edeea3fb0ba19a4b1a159519cb4bb8f4e79a9 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Mon, 11 May 2020 15:41:39 +0100 Subject: [PATCH] Add shortcut to to/fromHex for empty input and fix signedness warning --- libsolutil/CommonData.cpp | 8 +++++++- libsolutil/CommonData.h | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/libsolutil/CommonData.cpp b/libsolutil/CommonData.cpp index bb1a7696c..d23a2ce6c 100644 --- a/libsolutil/CommonData.cpp +++ b/libsolutil/CommonData.cpp @@ -53,6 +53,9 @@ string solidity::util::toHex(uint8_t _data, HexCase _case) string solidity::util::toHex(bytes const& _data, HexPrefix _prefix, HexCase _case) { + if (_data.empty()) + return {}; + std::string ret(_data.size() * 2 + (_prefix == HexPrefix::Add ? 2 : 0), 0); size_t i = 0; @@ -64,7 +67,7 @@ string solidity::util::toHex(bytes const& _data, HexPrefix _prefix, HexCase _cas // Mixed case will be handled inside the loop. char const* chars = _case == HexCase::Upper ? upperHexChars : lowerHexChars; - int rix = _data.size() - 1; + size_t rix = _data.size() - 1; for (uint8_t c: _data) { // switch hex case every four hexchars @@ -95,6 +98,9 @@ int solidity::util::fromHex(char _i, WhenError _throw) bytes solidity::util::fromHex(std::string const& _s, WhenError _throw) { + if (_s.empty()) + return {}; + unsigned s = (_s.size() >= 2 && _s[0] == '0' && _s[1] == 'x') ? 2 : 0; std::vector ret; ret.reserve((_s.size() - s + 1) / 2); diff --git a/libsolutil/CommonData.h b/libsolutil/CommonData.h index adac1fb05..8fbe20568 100644 --- a/libsolutil/CommonData.h +++ b/libsolutil/CommonData.h @@ -299,7 +299,7 @@ template inline bytes toCompactBigEndian(T _val, unsigned _min = 0) { static_assert(std::is_same::value || !std::numeric_limits::is_signed, "only unsigned types or bigint supported"); //bigint does not carry sign bit on shift - int i = 0; + unsigned i = 0; for (T v = _val; v; ++i, v >>= 8) {} bytes ret(std::max(_min, i), 0); toBigEndian(_val, ret);