Browse Source

CharacterFunctions: Avoid UB when parsing hex strings

v6.1.6
reuk 6 years ago
parent
commit
6244fc293f
No known key found for this signature in database GPG Key ID: 9ADCD339CFC98A11
4 changed files with 9 additions and 6 deletions
  1. +1
    -1
      modules/juce_core/network/juce_IPAddress.cpp
  2. +5
    -2
      modules/juce_core/text/juce_CharacterFunctions.h
  3. +2
    -2
      modules/juce_core/text/juce_String.cpp
  4. +1
    -1
      modules/juce_graphics/colour/juce_Colour.cpp

+ 1
- 1
modules/juce_core/network/juce_IPAddress.cpp 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];


+ 5
- 2
modules/juce_core/text/juce_CharacterFunctions.h 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;


+ 2
- 2
modules/juce_core/text/juce_String.cpp 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)


+ 1
- 1
modules/juce_graphics/colour/juce_Colour.cpp 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


Loading…
Cancel
Save