1
0
Fork 0
mirror of https://git.suyu.dev/suyu/sirit.git synced 2024-12-22 20:22:02 +00:00

Remove undefined behaviour from literal number

This commit is contained in:
ReinUsesLisp 2019-02-19 04:37:00 -03:00
parent b23716087a
commit 88191480a8

View file

@ -8,6 +8,7 @@
#include "operand.h" #include "operand.h"
#include "stream.h" #include "stream.h"
#include <cstring>
#include <typeindex> #include <typeindex>
namespace Sirit { namespace Sirit {
@ -26,18 +27,15 @@ class LiteralNumber : public Operand {
static_assert(sizeof(T) == 4 || sizeof(T) == 8); static_assert(sizeof(T) == 4 || sizeof(T) == 8);
LiteralNumber* number = new LiteralNumber(std::type_index(typeid(T))); LiteralNumber* number = new LiteralNumber(std::type_index(typeid(T)));
if (number->is_32 = sizeof(T) == 4; number->is_32) { number->is_32 = sizeof(T) == 4;
number->raw = *reinterpret_cast<u32*>(&value); std::memcpy(&number->raw, &value, sizeof(T));
} else {
number->raw = *reinterpret_cast<u64*>(&value);
}
return number; return number;
} }
private: private:
const std::type_index type; std::type_index type;
bool is_32; bool is_32{};
u64 raw; u64 raw{};
}; };
} // namespace Sirit } // namespace Sirit