From b2f4f43d7e34898dad642f465271cd9da7b49d70 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Fri, 2 Feb 2018 22:50:55 +0000 Subject: [PATCH] Replaced static assert about TCHAR being not wider that ImWchar with a runtime check validating whether TCHAR values are in accepted range. Runtime check only happens on platforms where TCHAR is wider than ImWchar. --- Source/ImGui/Private/ImGuiInputState.cpp | 41 ++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/Source/ImGui/Private/ImGuiInputState.cpp b/Source/ImGui/Private/ImGuiInputState.cpp index c796d0b..c562227 100644 --- a/Source/ImGui/Private/ImGuiInputState.cpp +++ b/Source/ImGui/Private/ImGuiInputState.cpp @@ -5,8 +5,45 @@ #include "ImGuiInputState.h" #include +#include +#include +// If TCHAR is wider than ImWchar, enable or disable validation of input character before conversions. +#define VALIDATE_INPUT_CHARACTERS 1 + +#if VALIDATE_INPUT_CHARACTERS +DEFINE_LOG_CATEGORY_STATIC(LogImGuiInputState, Warning, All); +#endif + +namespace +{ + template* = nullptr> + ImWchar CastInputChar(T Char) + { + return static_cast(Char); + } + + template* = nullptr> + ImWchar CastInputChar(T Char) + { +#if VALIDATE_INPUT_CHARACTERS + // We only need a runtime validation if TCHAR is wider than ImWchar. + // Signed and unsigned integral types with the same size as ImWchar should be safely converted. As long as the + // char value is in that range we can safely use it, otherwise we should log an error to notify about possible + // truncations. + static constexpr auto MinLimit = (std::numeric_limits>::min)(); + static constexpr auto MaxLimit = (std::numeric_limits>::max)(); + UE_CLOG(!(Char >= MinLimit && Char <= MaxLimit), LogImGuiInputState, Error, + TEXT("TCHAR value '%c' (%#x) is out of range %d (%#x) to %u (%#x) that can be safely converted to ImWchar. ") + TEXT("If you wish to disable this validation, please set VALIDATE_INPUT_CHARACTERS in ImGuiInputState.cpp to 0."), + Char, Char, MinLimit, MinLimit, MaxLimit, MaxLimit); +#endif + + return static_cast(Char); + } +} + FImGuiInputState::FImGuiInputState() { ResetState(); @@ -14,11 +51,9 @@ FImGuiInputState::FImGuiInputState() void FImGuiInputState::AddCharacter(TCHAR Char) { - static_assert(sizeof(TCHAR) <= sizeof(InputCharacters[0]), "Size of elements in Input Characters buffer is smaller than size of 'TCHAR'. Possible truncation."); - if (InputCharactersNum < Utilities::GetArraySize(InputCharacters)) { - InputCharacters[InputCharactersNum++] = static_cast(Char); + InputCharacters[InputCharactersNum++] = CastInputChar(Char); InputCharacters[InputCharactersNum] = 0; } }