diff --git a/Code/Framework/AzCore/AzCore/std/string/conversions.h b/Code/Framework/AzCore/AzCore/std/string/conversions.h index 235eb188d1..473c327143 100644 --- a/Code/Framework/AzCore/AzCore/std/string/conversions.h +++ b/Code/Framework/AzCore/AzCore/std/string/conversions.h @@ -338,7 +338,7 @@ namespace AZStd if (srcLen > 0) { char* endStr = Internal::WCharTPlatformConverter<>::to_string(dest, destSize, str, str + srcLen); - if (*(endStr - 1) != '\0') + if (endStr < (dest + destSize) && *(endStr - 1) != '\0') { *endStr = '\0'; // copy null terminator } @@ -495,7 +495,7 @@ namespace AZStd if (srcLen > 0) { wchar_t* endWStr = Internal::WCharTPlatformConverter<>::to_wstring(dest, destSize, str, str + srcLen); - if (*(endWStr - 1) != '\0') + if (endWStr < (dest + destSize) && *(endWStr - 1) != '\0') { *endWStr = '\0'; // copy null terminator } diff --git a/Code/Framework/AzCore/AzCore/std/string/utf8/unchecked.h b/Code/Framework/AzCore/AzCore/std/string/utf8/unchecked.h index cfc917ddc4..7199c58d3e 100644 --- a/Code/Framework/AzCore/AzCore/std/string/utf8/unchecked.h +++ b/Code/Framework/AzCore/AzCore/std/string/utf8/unchecked.h @@ -251,6 +251,54 @@ namespace Utf8::Unchecked return result; } + + static constexpr size_t utf8_codepoint_length(AZ::u32 cp) + { + if (cp < 0x80) + { + return 1; + } + else if (cp < 0x800) + { + return 2; + } + else if (cp < 0x10000) + { + return 3; + } + return 4; + } + + template + size_t utf16ToUtf8BytesRequired(u16bit_iterator start, u16bit_iterator end) + { + size_t bytesRequired = 0; + while (start != end) + { + AZ::u32 cp = Utf8::Internal::mask16(*start++); + // Take care of surrogate pairs first + if (Utf8::Internal::is_lead_surrogate(cp)) + { + AZ::u32 trail_surrogate = Utf8::Internal::mask16(*start++); + cp = (cp << 10) + trail_surrogate + Internal::SURROGATE_OFFSET; + } + bytesRequired += utf8_codepoint_length(cp); + } + return bytesRequired; + } + + template + size_t utf32ToUtf8BytesRequired(u32bit_iterator start, u32bit_iterator end) + { + size_t bytesRequired = 0; + while (start != end) + { + bytesRequired += utf8_codepoint_length(*start++); + } + return bytesRequired; + } + + } // namespace Utf8::Unchecked diff --git a/Code/Framework/AzCore/Platform/Common/WinAPI/AzCore/Utils/Utils_WinAPI.cpp b/Code/Framework/AzCore/Platform/Common/WinAPI/AzCore/Utils/Utils_WinAPI.cpp index dc104668be..4d8c9c8cfc 100644 --- a/Code/Framework/AzCore/Platform/Common/WinAPI/AzCore/Utils/Utils_WinAPI.cpp +++ b/Code/Framework/AzCore/Platform/Common/WinAPI/AzCore/Utils/Utils_WinAPI.cpp @@ -31,7 +31,7 @@ namespace AZ // Platform specific get exe path: http://stackoverflow.com/a/1024937 // https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getmodulefilenamea wchar_t pathBufferW[AZ::IO::MaxPathLength] = { 0 }; - const DWORD pathLen = GetModuleFileNameW(nullptr, pathBufferW, static_cast(exeStorageSize)); + const DWORD pathLen = GetModuleFileNameW(nullptr, pathBufferW, static_cast(AZStd::size(pathBufferW))); const DWORD errorCode = GetLastError(); if (pathLen == exeStorageSize && errorCode == ERROR_INSUFFICIENT_BUFFER) { @@ -43,7 +43,15 @@ namespace AZ } else { - AZStd::to_string(exeStorageBuffer, exeStorageSize, pathBufferW); + size_t utf8PathSize = Utf8::Unchecked::utf16ToUtf8BytesRequired(pathBufferW, pathBufferW + pathLen); + if (utf8PathSize >= exeStorageSize) + { + result.m_pathStored = ExecutablePathResult::BufferSizeNotLargeEnough; + } + else + { + AZStd::to_string(exeStorageBuffer, exeStorageSize, pathBufferW); + } } return result; diff --git a/Code/Framework/AzCore/Platform/Windows/AzCore/Debug/StackTracer_Windows.cpp b/Code/Framework/AzCore/Platform/Windows/AzCore/Debug/StackTracer_Windows.cpp index 8a99616de3..6cfaf1703b 100644 --- a/Code/Framework/AzCore/Platform/Windows/AzCore/Debug/StackTracer_Windows.cpp +++ b/Code/Framework/AzCore/Platform/Windows/AzCore/Debug/StackTracer_Windows.cpp @@ -90,7 +90,7 @@ namespace AZ { { case CBA_EVENT: evt = (PIMAGEHLP_CBA_EVENT)CallbackData; - _tprintf(_T("%s"), evt->desc); + printf("%s", evt->desc); break; default: diff --git a/Code/Legacy/CryCommon/CryTypeInfo.cpp b/Code/Legacy/CryCommon/CryTypeInfo.cpp index b1f7f20601..82743f7eef 100644 --- a/Code/Legacy/CryCommon/CryTypeInfo.cpp +++ b/Code/Legacy/CryCommon/CryTypeInfo.cpp @@ -132,9 +132,7 @@ const CTypeInfo&PtrTypeInfo() // bool AZStd::string ToString(bool const& val) { - static AZStd::fixed_string<5> sTrue = "true"; - static AZStd::fixed_string<6> sFalse = "false"; - return val ? sTrue.c_str() : sFalse.c_str(); + return val ? "true" : "false"; } bool FromString(bool& val, cstr s) diff --git a/Code/Tools/SceneAPI/SceneCore/DataTypes/Rules/IMeshAdvancedRule.h b/Code/Tools/SceneAPI/SceneCore/DataTypes/Rules/IMeshAdvancedRule.h index 48310091b3..04ed3f0949 100644 --- a/Code/Tools/SceneAPI/SceneCore/DataTypes/Rules/IMeshAdvancedRule.h +++ b/Code/Tools/SceneAPI/SceneCore/DataTypes/Rules/IMeshAdvancedRule.h @@ -19,7 +19,7 @@ namespace AZ { namespace DataTypes { - static AZStd::string s_advancedDisabledString = "Disabled"; + static const char* s_advancedDisabledString = "Disabled"; class IMeshAdvancedRule : public IRule