From ea9f60e32c3b4e6517cb865364f189e5abd872e6 Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Sun, 3 Apr 2016 23:23:01 +0300 Subject: [PATCH] Fix a buffer underflow in the Center function from the string library. --- source/Library/String.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/source/Library/String.cpp b/source/Library/String.cpp index 0b579eb8..40f54848 100644 --- a/source/Library/String.cpp +++ b/source/Library/String.cpp @@ -410,10 +410,12 @@ CSStr CenterStr(CSStr s, SQChar f, Uint32 w) { // Calculate the string length const Uint32 n = std::strlen(s); + // Calculate the insert position + const Int32 p = ((w/2) - (n/2)); // Insert only the fill character first std::memset(b.Data(), f, w); // Overwrite with the specified string - std::strncpy(b.Data() + ((w/2) - (n/2)), s, n); + std::strncpy(b.Data() + (p < 0 ? 0 : p), s, n); } // End the resulted string b.At(w) = '\0';