1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 00:37:15 +01:00

Fix a buffer underflow in the Center function from the string library.

This commit is contained in:
Sandu Liviu Catalin 2016-04-03 23:23:01 +03:00
parent 159e2f5929
commit ea9f60e32c

View File

@ -410,10 +410,12 @@ CSStr CenterStr(CSStr s, SQChar f, Uint32 w)
{ {
// Calculate the string length // Calculate the string length
const Uint32 n = std::strlen(s); const Uint32 n = std::strlen(s);
// Calculate the insert position
const Int32 p = ((w/2) - (n/2));
// Insert only the fill character first // Insert only the fill character first
std::memset(b.Data(), f, w); std::memset(b.Data(), f, w);
// Overwrite with the specified string // 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 // End the resulted string
b.At(w) = '\0'; b.At(w) = '\0';