From 07b2945c5b35fe359fb5316e1f12c073e6a0d31d Mon Sep 17 00:00:00 2001 From: duncanspumpkin Date: Fri, 19 Mar 2021 18:16:26 +0000 Subject: [PATCH 1/3] Fix #14353. Provide a buffer for text input --- src/openrct2-ui/windows/TextInput.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/openrct2-ui/windows/TextInput.cpp b/src/openrct2-ui/windows/TextInput.cpp index fa401ad79b..4e386597fb 100644 --- a/src/openrct2-ui/windows/TextInput.cpp +++ b/src/openrct2-ui/windows/TextInput.cpp @@ -106,6 +106,7 @@ public: void SetText(std::string_view text, size_t maxLength) { _buffer = String::UTF8Truncate(text, maxLength); + _buffer.resize(maxLength); _maxInputLength = maxLength; gTextInput = context_start_text_input(_buffer.data(), maxLength); } @@ -164,7 +165,7 @@ public: void OnPrepareDraw() override { // Change window size if required. - int32_t newHeight = CalculateWindowHeight(_buffer.data()); + int32_t newHeight = CalculateWindowHeight(_buffer); if (newHeight != height) { Invalidate(); @@ -215,7 +216,7 @@ public: screenCoords.y += 25; char wrapped_string[TEXT_INPUT_SIZE]; - safe_strcpy(wrapped_string, _buffer.data(), TEXT_INPUT_SIZE); + safe_strcpy(wrapped_string, _buffer.c_str(), TEXT_INPUT_SIZE); // String length needs to add 12 either side of box // +13 for cursor when max length. @@ -249,7 +250,7 @@ public: cursorY = screenCoords.y; int32_t textWidth = 6; - if (gTextInput->SelectionStart < strlen(_buffer.data())) + if (gTextInput->SelectionStart < _buffer.length()) { // Make a 1 utf8-character wide string for measuring the width // of the currently selected character. @@ -342,7 +343,7 @@ private: { if (_callback) { - _callback(_buffer.data()); + _callback(_buffer); } } else From ee575d5b8cc19f14d1f56c7040552aa4cc8fc5b3 Mon Sep 17 00:00:00 2001 From: duncanspumpkin Date: Sat, 20 Mar 2021 07:22:00 +0000 Subject: [PATCH 2/3] Use std::vector to prevent violations of std string --- src/openrct2-ui/windows/TextInput.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/openrct2-ui/windows/TextInput.cpp b/src/openrct2-ui/windows/TextInput.cpp index 4e386597fb..8d278b99f3 100644 --- a/src/openrct2-ui/windows/TextInput.cpp +++ b/src/openrct2-ui/windows/TextInput.cpp @@ -53,7 +53,7 @@ private: int32_t _cursorBlink{}; size_t _maxInputLength{}; - std::string _buffer; + std::vector _buffer; public: void OnOpen() override @@ -105,8 +105,8 @@ public: void SetText(std::string_view text, size_t maxLength) { - _buffer = String::UTF8Truncate(text, maxLength); _buffer.resize(maxLength); + safe_strcpy(_buffer.data(), std::string(text).c_str(), maxLength); _maxInputLength = maxLength; gTextInput = context_start_text_input(_buffer.data(), maxLength); } @@ -165,7 +165,7 @@ public: void OnPrepareDraw() override { // Change window size if required. - int32_t newHeight = CalculateWindowHeight(_buffer); + int32_t newHeight = CalculateWindowHeight(_buffer.data()); if (newHeight != height) { Invalidate(); @@ -216,7 +216,7 @@ public: screenCoords.y += 25; char wrapped_string[TEXT_INPUT_SIZE]; - safe_strcpy(wrapped_string, _buffer.c_str(), TEXT_INPUT_SIZE); + safe_strcpy(wrapped_string, _buffer.data(), _buffer.size()); // String length needs to add 12 either side of box // +13 for cursor when max length. @@ -250,7 +250,7 @@ public: cursorY = screenCoords.y; int32_t textWidth = 6; - if (gTextInput->SelectionStart < _buffer.length()) + if (gTextInput->SelectionStart < strnlen_s(_buffer.data(), _buffer.size())) { // Make a 1 utf8-character wide string for measuring the width // of the currently selected character. @@ -343,7 +343,7 @@ private: { if (_callback) { - _callback(_buffer); + _callback(_buffer.data()); } } else From 820668d0aa123341f02543357586298929aff545 Mon Sep 17 00:00:00 2001 From: duncanspumpkin Date: Sat, 20 Mar 2021 07:27:23 +0000 Subject: [PATCH 3/3] Use unsafe strlen for platforms without strlen_s --- src/openrct2-ui/windows/TextInput.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/openrct2-ui/windows/TextInput.cpp b/src/openrct2-ui/windows/TextInput.cpp index 8d278b99f3..956252064c 100644 --- a/src/openrct2-ui/windows/TextInput.cpp +++ b/src/openrct2-ui/windows/TextInput.cpp @@ -250,7 +250,7 @@ public: cursorY = screenCoords.y; int32_t textWidth = 6; - if (gTextInput->SelectionStart < strnlen_s(_buffer.data(), _buffer.size())) + if (gTextInput->SelectionStart < strlen(_buffer.data())) { // Make a 1 utf8-character wide string for measuring the width // of the currently selected character.