From 6ba5fa4d64c5cdda19404c9d8360d809d9546144 Mon Sep 17 00:00:00 2001 From: Michael Lotz Date: Fri, 9 Dec 2011 15:25:56 +0100 Subject: [PATCH] Ensure 0 termination of the buffer being converted to a string. The file content isn't normally 0 terminated, so making a string out of it would usually result in reading beyond the allocated buffer to find the string length, possibly leading to a crash. --- src/servers/registrar/TRoster.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/servers/registrar/TRoster.cpp b/src/servers/registrar/TRoster.cpp index e93d5605dd..31d14bde73 100644 --- a/src/servers/registrar/TRoster.cpp +++ b/src/servers/registrar/TRoster.cpp @@ -1836,15 +1836,17 @@ TRoster::_LoadRosterSettings(const char* path) char* data = NULL; if (!error) { - data = new(nothrow) char[size]; + data = new(nothrow) char[size + 1]; error = data ? B_OK : B_NO_MEMORY; } if (!error) { ssize_t bytes = file.Read(data, size); error = bytes < 0 ? bytes : (bytes == size ? B_OK : B_FILE_ERROR); } - if (!error) + if (!error) { + data[size] = 0; error = stream.SetTo(std::string(data)); + } delete[] data;