From aef8a345b25e57c4614260d0962a356095c9b2fa Mon Sep 17 00:00:00 2001 From: rubidium42 Date: Fri, 8 Mar 2024 14:26:14 +0100 Subject: [PATCH] Codefix #12162, 3105d0b: Textbuf::Assign read beyond std::string_view (#12177) --- src/textbuf.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/textbuf.cpp b/src/textbuf.cpp index 2cffc81aeb..60b839e326 100644 --- a/src/textbuf.cpp +++ b/src/textbuf.cpp @@ -413,9 +413,11 @@ void Textbuf::Assign(StringID string) */ void Textbuf::Assign(const std::string_view text) { - const char *last_of = &this->buf[this->max_bytes - 1]; - strecpy(this->buf, text.data(), last_of); - StrMakeValidInPlace(this->buf, last_of, SVS_NONE); + size_t bytes = std::min(this->max_bytes - 1, text.size()); + memcpy(this->buf, text.data(), bytes); + this->buf[bytes] = '\0'; + + StrMakeValidInPlace(this->buf, &this->buf[bytes], SVS_NONE); /* Make sure the name isn't too long for the text buffer in the number of * characters (not bytes). max_chars also counts the '\0' characters. */