Fix ubsan runtime error: left shift of negative value -9

This commit is contained in:
Michael Krasnyk 2016-11-16 00:08:40 +01:00 committed by Daniel Patterson
parent 73e365d398
commit 1eaf9f3269

View File

@ -33,20 +33,16 @@ std::string encode(std::vector<int> &numbers)
std::string output; std::string output;
for (auto &number : numbers) for (auto &number : numbers)
{ {
bool isNegative = number < 0; if (number < 0)
if (isNegative)
{ {
const unsigned binary = std::llabs(number); const unsigned binary = std::llabs(number);
const unsigned twos = (~binary) + 1u; const unsigned twos = (~binary) + 1u;
number = twos; const unsigned shl = twos << 1u;
number = static_cast<int>(~shl);
} }
else
number <<= 1u;
if (isNegative)
{ {
number = ~number; number <<= 1u;
} }
} }
for (const int number : numbers) for (const int number : numbers)