From 875d9dcead1490406e42276fd7e52f53b342fff9 Mon Sep 17 00:00:00 2001 From: elsid Date: Tue, 15 Feb 2022 22:54:23 +0100 Subject: [PATCH] Fix buffer resizing by StatelessUtf8Encoder --- apps/openmw_test_suite/toutf8/toutf8.cpp | 20 ++++++++++++++++++++ components/to_utf8/to_utf8.cpp | 8 +++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/apps/openmw_test_suite/toutf8/toutf8.cpp b/apps/openmw_test_suite/toutf8/toutf8.cpp index bad4f34fd5..d1cf6b2851 100644 --- a/apps/openmw_test_suite/toutf8/toutf8.cpp +++ b/apps/openmw_test_suite/toutf8/toutf8.cpp @@ -136,4 +136,24 @@ namespace Params {ToUTF8::WINDOWS_1251, "russian-win1251.txt", "russian-utf8.txt"}, Params {ToUTF8::WINDOWS_1252, "french-win1252.txt", "french-utf8.txt"} )); + + TEST(StatelessUtf8EncoderTest, shouldCleanupBuffer) + { + std::string buffer; + StatelessUtf8Encoder encoder(FromType::WINDOWS_1252); + encoder.getUtf8(std::string_view("long string\x92"), BufferAllocationPolicy::UseGrowFactor, buffer); + const std::string shortString("short\x92"); + ASSERT_GT(buffer.size(), shortString.size()); + const std::string_view shortUtf8 = encoder.getUtf8(shortString, BufferAllocationPolicy::UseGrowFactor, buffer); + ASSERT_GE(buffer.size(), shortUtf8.size()); + EXPECT_EQ(buffer[shortUtf8.size()], '\0') << buffer; + } + + TEST(StatelessUtf8EncoderTest, withFitToRequiredSizeShouldResizeBuffer) + { + std::string buffer; + StatelessUtf8Encoder encoder(FromType::WINDOWS_1252); + const std::string_view utf8 = encoder.getUtf8(std::string_view("long string\x92"), BufferAllocationPolicy::FitToRequiredSize, buffer); + EXPECT_EQ(buffer.size(), utf8.size()); + } } diff --git a/components/to_utf8/to_utf8.cpp b/components/to_utf8/to_utf8.cpp index a193e6375d..f4b52b644b 100644 --- a/components/to_utf8/to_utf8.cpp +++ b/components/to_utf8/to_utf8.cpp @@ -72,7 +72,13 @@ namespace // including a terminating zero after it. void resize(std::size_t size, BufferAllocationPolicy bufferAllocationPolicy, std::string& buffer) { - if (buffer.size() >= size) + if (buffer.size() > size) + { + buffer[size] = 0; + return; + } + + if (buffer.size() == size) return; switch (bufferAllocationPolicy)