1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

CharacterFunctions: Avoid UB when parsing hex strings

This commit is contained in:
reuk 2019-04-03 11:50:22 +01:00
parent 6d3504adfa
commit 6244fc293f
No known key found for this signature in database
GPG key ID: 9ADCD339CFC98A11
4 changed files with 9 additions and 6 deletions

View file

@ -166,7 +166,7 @@ IPAddress::IPAddress (const String& adr)
}
IPAddressByteUnion temp;
temp.combined = (uint16) CharacterFunctions::HexParser<int>::parse (tokens[i].getCharPointer());
temp.combined = CharacterFunctions::HexParser<uint16>::parse (tokens[i].getCharPointer());
address[i * 2] = temp.split[0];
address[i * 2 + 1] = temp.split[1];

View file

@ -490,6 +490,9 @@ public:
template <typename ResultType>
struct HexParser
{
static_assert (std::is_unsigned<ResultType>::value, "ResultType must be unsigned because "
"left-shifting a negative value is UB");
template <typename CharPointerType>
static ResultType parse (CharPointerType t) noexcept
{
@ -497,10 +500,10 @@ public:
while (! t.isEmpty())
{
auto hexValue = CharacterFunctions::getHexDigitValue (t.getAndAdvance());
auto hexValue = static_cast<ResultType> (CharacterFunctions::getHexDigitValue (t.getAndAdvance()));
if (hexValue >= 0)
result = (result << 4) | hexValue;
result = static_cast<ResultType> (result << 4) | hexValue;
}
return result;

View file

@ -1957,8 +1957,8 @@ String String::toHexString (const void* const d, const int size, const int group
return s;
}
int String::getHexValue32() const noexcept { return CharacterFunctions::HexParser<int> ::parse (text); }
int64 String::getHexValue64() const noexcept { return CharacterFunctions::HexParser<int64>::parse (text); }
int String::getHexValue32() const noexcept { return (int32) CharacterFunctions::HexParser<uint32>::parse (text); }
int64 String::getHexValue64() const noexcept { return (int64) CharacterFunctions::HexParser<uint64>::parse (text); }
//==============================================================================
static String getStringFromWindows1252Codepage (const char* data, size_t num)

View file

@ -542,7 +542,7 @@ String Colour::toString() const
Colour Colour::fromString (StringRef encodedColourString)
{
return Colour ((uint32) CharacterFunctions::HexParser<int>::parse (encodedColourString.text));
return Colour (CharacterFunctions::HexParser<uint32>::parse (encodedColourString.text));
}
String Colour::toDisplayString (const bool includeAlphaValue) const