1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-03-30 14:06:41 +00:00

Fix buffer resizing by StatelessUtf8Encoder

This commit is contained in:
elsid 2022-02-15 22:54:23 +01:00
parent 8a0fb4d633
commit 875d9dcead
No known key found for this signature in database
GPG key ID: B845CB9FEE18AB40
2 changed files with 27 additions and 1 deletions

View file

@ -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());
}
}

View file

@ -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)