From 57e9834786218024b6a9aafb2102049b164137dd Mon Sep 17 00:00:00 2001 From: dongresource Date: Mon, 25 Oct 2021 21:05:49 +0200 Subject: [PATCH] Fix U16toU8() returning strings longer than max UTF-16 inputs containing actual multi-byte characters resulted in codecvt returning a UTF-8 string longer than the requested max length. --- src/main.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index c4ccb10..c4e0345 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -155,7 +155,12 @@ std::string U16toU8(char16_t* src, size_t max) { src[max-1] = '\0'; // force a NULL terminatorstd::string U16toU8(char16_t* src) { try { std::wstring_convert,char16_t> convert; - return convert.to_bytes(src); + std::string ret = convert.to_bytes(src); + + if (ret.size() >= max) + ret.resize(max-2); + + return ret; } catch(const std::exception& e) { return ""; }