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.
This commit is contained in:
@@ -1836,15 +1836,17 @@ TRoster::_LoadRosterSettings(const char* path)
|
|||||||
char* data = NULL;
|
char* data = NULL;
|
||||||
|
|
||||||
if (!error) {
|
if (!error) {
|
||||||
data = new(nothrow) char[size];
|
data = new(nothrow) char[size + 1];
|
||||||
error = data ? B_OK : B_NO_MEMORY;
|
error = data ? B_OK : B_NO_MEMORY;
|
||||||
}
|
}
|
||||||
if (!error) {
|
if (!error) {
|
||||||
ssize_t bytes = file.Read(data, size);
|
ssize_t bytes = file.Read(data, size);
|
||||||
error = bytes < 0 ? bytes : (bytes == size ? B_OK : B_FILE_ERROR);
|
error = bytes < 0 ? bytes : (bytes == size ? B_OK : B_FILE_ERROR);
|
||||||
}
|
}
|
||||||
if (!error)
|
if (!error) {
|
||||||
|
data[size] = 0;
|
||||||
error = stream.SetTo(std::string(data));
|
error = stream.SetTo(std::string(data));
|
||||||
|
}
|
||||||
|
|
||||||
delete[] data;
|
delete[] data;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user