1
0
mirror of synced 2024-11-24 15:50:16 +01:00

patterns: Fix local variables holding larger values than allowed

This commit is contained in:
WerWolv 2022-01-12 08:59:14 +01:00
parent d399a6427a
commit 6b20a9bdd5
2 changed files with 11 additions and 6 deletions

View File

@ -115,6 +115,11 @@ namespace hex {
return result;
}
[[nodiscard]]
constexpr uint64_t bitmask(uint8_t bits) {
return (uint64_t(1) << (bits)) - 1;
}
template<typename T>
constexpr T changeEndianess(T value, size_t size, std::endian endian) {
if (endian == std::endian::native)

View File

@ -81,11 +81,11 @@ namespace hex::pl {
Token::Literal castedLiteral = std::visit(overloaded {
[&](double &value) -> Token::Literal {
if (dynamic_cast<PatternDataUnsigned*>(pattern))
return u128(value);
return u128(value) & bitmask(pattern->getSize() * 8);
else if (dynamic_cast<PatternDataSigned*>(pattern))
return s128(value);
return s128(value) & bitmask(pattern->getSize() * 8);
else if (dynamic_cast<PatternDataFloat*>(pattern))
return value;
return pattern->getSize() == sizeof(float) ? double(float(value)) : value;
else
LogConsole::abortEvaluation(hex::format("cannot cast type 'double' to type '{}'", pattern->getTypeName()));
},
@ -103,15 +103,15 @@ namespace hex::pl {
},
[&](auto &&value) -> Token::Literal {
if (dynamic_cast<PatternDataUnsigned*>(pattern) || dynamic_cast<PatternDataEnum*>(pattern))
return u128(value);
return u128(value) & bitmask(pattern->getSize() * 8);
else if (dynamic_cast<PatternDataSigned*>(pattern))
return s128(value);
return s128(value) & bitmask(pattern->getSize() * 8);
else if (dynamic_cast<PatternDataCharacter*>(pattern))
return char(value);
else if (dynamic_cast<PatternDataBoolean*>(pattern))
return bool(value);
else if (dynamic_cast<PatternDataFloat*>(pattern))
return double(value);
return pattern->getSize() == sizeof(float) ? double(float(value)) : value;
else
LogConsole::abortEvaluation(hex::format("cannot cast integer literal to type '{}'", pattern->getTypeName()));
}