From 17e2f8c477e4064a042c8deea1adb2efde8109e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Va=CC=81clav=20Slavi=CC=81k?= Date: Sat, 28 Sep 2019 18:57:26 +0200 Subject: [PATCH] Fix EOLs buffer shortening in wxFFile::ReadAll() As the comments in the function explain, fread() may return a shorter buffer than expected due to CRT's implicit conversion of DOS EOLs to \n. The logic for handling this was however broken: it NUL-terminated the buffer appropriately, but that had no effect when later used in wxString constructor, which used buffer's length for string length. This resulted in slightly larger strings with uninitialized tails that were mostly invisible to the user as the tail would disappear anywhere the string was handled as a NULL-terminated C string. It also caused occassional UTF-8 conversion failures when the tailing bytes didn't form a valid sequence. Fixed by using wxCharBuffer::shrink() to properly truncate the buffer. --- src/common/ffile.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/common/ffile.cpp b/src/common/ffile.cpp index abccca0abb..b77a60c649 100644 --- a/src/common/ffile.cpp +++ b/src/common/ffile.cpp @@ -111,7 +111,8 @@ bool wxFFile::ReadAll(wxString *str, const wxMBConv& conv) return false; } - buf.data()[length] = 0; + // shrink the buffer to possibly shorter data as explained above: + buf.shrink(length); wxString strTmp(buf, conv); str->swap(strTmp);