From 6b62a1963e30b3bed2dfc1754136f23b489ad315 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Mon, 15 Aug 2022 21:08:09 +0200 Subject: [PATCH] fix: Some occurrences of undefined behaviour --- lib/libimhex/include/hex/helpers/utils.hpp | 2 +- plugins/builtin/source/content/data_inspector.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/libimhex/include/hex/helpers/utils.hpp b/lib/libimhex/include/hex/helpers/utils.hpp index 025b5f766..e3d1034f6 100644 --- a/lib/libimhex/include/hex/helpers/utils.hpp +++ b/lib/libimhex/include/hex/helpers/utils.hpp @@ -68,7 +68,7 @@ namespace hex { } constexpr inline i128 signExtend(size_t numBits, i128 value) { - i128 mask = 1U << (numBits - 1); + i128 mask = 1ULL << (numBits - 1); return (value ^ mask) - mask; } diff --git a/plugins/builtin/source/content/data_inspector.cpp b/plugins/builtin/source/content/data_inspector.cpp index 6642e9392..e4419c1f0 100644 --- a/plugins/builtin/source/content/data_inspector.cpp +++ b/plugins/builtin/source/content/data_inspector.cpp @@ -86,7 +86,7 @@ namespace hex::plugin::builtin { auto format = (style == Style::Decimal) ? "{0:d}" : ((style == Style::Hexadecimal) ? hex::format("0x{{0:0{}X}}", Size * 2) : hex::format("0o{{0:0{}o}}", Size * 3)); T value = 0x00; - std::memcpy(&value, buffer.data(), Size); + std::memcpy(&value, buffer.data(), std::min(sizeof(T), Size)); return hex::format(format, hex::changeEndianess(value, Size, endian)); } @@ -98,7 +98,7 @@ namespace hex::plugin::builtin { auto format = (style == Style::Decimal) ? "{0}{1:d}" : ((style == Style::Hexadecimal) ? hex::format("{{0}}0x{{1:0{}X}}", Size * 2) : hex::format("{{0}}0o{{1:0{}o}}", Size * 3)); T value = 0x00; - std::memcpy(&value, buffer.data(), Size); + std::memcpy(&value, buffer.data(), std::min(sizeof(T), Size)); auto number = hex::signExtend(Size * 8, hex::changeEndianess(value, Size, endian)); bool negative = number < 0;