vfs: check userland buffers before reading them.
[haiku.git] / src / system / libroot / os / PathBuffer.h
blob0fc0f9566b87c570436327316dfeea930ec5bd16
1 /*
2 * Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
5 #ifndef PATH_BUFFER_H
6 #define PATH_BUFFER_H
9 #include <string.h>
11 #include <algorithm>
14 namespace {
16 struct PathBuffer {
17 PathBuffer()
19 fBuffer(NULL),
20 fSize(0),
21 fLength(0)
25 PathBuffer(char* buffer, size_t size, size_t length = 0)
27 SetTo(buffer, size, length);
30 void SetTo(char* buffer, size_t size, size_t length = 0)
32 fBuffer = buffer;
33 fSize = size;
34 fLength = length;
35 if (fLength < fSize)
36 fBuffer[fLength] = '\0';
39 bool Append(const char* toAppend, size_t length)
41 if (length > 0 && fLength + 1 < fSize) {
42 size_t toCopy = std::min(length, fSize - fLength - 1);
43 memcpy(fBuffer + fLength, toAppend, toCopy);
44 fBuffer[fLength + toCopy] = '\0';
47 fLength += length;
48 return fLength < fSize;
51 bool Append(const char* toAppend)
53 return Append(toAppend, strlen(toAppend));
56 bool Append(char c)
58 return Append(&c, 1);
61 size_t Length() const
63 return fLength;
66 private:
67 char* fBuffer;
68 size_t fSize;
69 size_t fLength;
75 #endif // PATH_BUFFER_H