vfs: check userland buffers before reading them.
[haiku.git] / src / kits / debugger / model / TeamMemory.cpp
blob1afe8a9a93cef2a47ac1382ecfe58fd986311aa5
1 /*
2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
7 #include "TeamMemory.h"
9 #include <algorithm>
11 #include <OS.h>
12 #include <String.h>
15 TeamMemory::~TeamMemory()
20 status_t
21 TeamMemory::ReadMemoryString(target_addr_t address, size_t maxLength,
22 BString& _string)
24 char buffer[B_PAGE_SIZE];
26 _string.Truncate(0);
27 while (maxLength > 0) {
28 // read at max maxLength bytes, but don't read across page bounds
29 size_t toRead = std::min(maxLength,
30 B_PAGE_SIZE - size_t(address % B_PAGE_SIZE));
31 ssize_t bytesRead = ReadMemory(address, buffer, toRead);
32 if (bytesRead < 0)
33 return _string.Length() == 0 ? bytesRead : B_OK;
35 if (bytesRead == 0)
36 return _string.Length() == 0 ? B_BAD_ADDRESS : B_OK;
38 // append the bytes read
39 size_t length = strnlen(buffer, bytesRead);
40 _string.Append(buffer, length);
42 // stop at end of string
43 if (length < (size_t)bytesRead)
44 return B_OK;
46 address += bytesRead;
47 maxLength -= bytesRead;
50 return B_OK;