vfs: check userland buffers before reading them.
[haiku.git] / src / servers / input / PathList.cpp
blob07c8814e37ee44b1e04276573e8623d9b2bbc464
1 /*
2 * Copyright 2008 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Axel Dörfler, axeld@pinc-software.de
7 */
10 #include "PathList.h"
12 #include <new>
13 #include <stdlib.h>
14 #include <string.h>
17 struct PathList::path_entry {
18 path_entry(const char* _path)
20 ref_count(1)
22 path = strdup(_path);
25 ~path_entry()
27 free((char*)path);
30 const char* path;
31 int32 ref_count;
35 PathList::PathList()
37 fPaths(10, true)
42 PathList::~PathList()
47 bool
48 PathList::HasPath(const char* path, int32* _index) const
50 for (int32 i = fPaths.CountItems(); i-- > 0;) {
51 if (!strcmp(fPaths.ItemAt(i)->path, path)) {
52 if (_index != NULL)
53 *_index = i;
54 return true;
58 return false;
62 status_t
63 PathList::AddPath(const char* path)
65 if (path == NULL)
66 return B_BAD_VALUE;
68 int32 index;
69 if (HasPath(path, &index)) {
70 fPaths.ItemAt(index)->ref_count++;
71 return B_OK;
74 path_entry* entry = new(std::nothrow) path_entry(path);
75 if (entry == NULL || entry->path == NULL || !fPaths.AddItem(entry)) {
76 delete entry;
77 return B_NO_MEMORY;
80 return B_OK;
84 status_t
85 PathList::RemovePath(const char* path)
87 int32 index;
88 if (!HasPath(path, &index))
89 return B_ENTRY_NOT_FOUND;
91 if (--fPaths.ItemAt(index)->ref_count == 0)
92 delete fPaths.RemoveItemAt(index);
94 return B_OK;
98 int32
99 PathList::CountPaths() const
101 return fPaths.CountItems();
105 const char*
106 PathList::PathAt(int32 index) const
108 path_entry* entry = fPaths.ItemAt(index);
109 if (entry == NULL)
110 return NULL;
112 return entry->path;