GCC Code Coverage Report


Directory: ./
File: Os/FilePathUtils.cpp
Date: 2026-11-03 21:15:54
Exec Total Coverage
Lines: 111 119 93.3%
Functions: 8 8 100.0%
Branches: 92 118 78.0%

Line Branch Exec Source
1 // ======================================================================
2 // \title Os/FilePathUtils.cpp
3 // \brief Implementation of file path utilities
4 // ======================================================================
5 #include <Fw/Types/Assert.hpp>
6 #include <Fw/Types/StringUtils.hpp>
7 #include <Os/FilePathUtils.hpp>
8 #include <Os/FileSystem.hpp>
9 #include <cstring>
10
11 namespace Os {
12 namespace FilePathUtils {
13
14 // Helper: validate that string_length did not return the buffer size
15 // (which indicates no null terminator was found — an invalid string).
16 1019 static bool isValidCString(FwSizeType measuredLen, FwSizeType bufferLen) {
17 1019 return measuredLen < bufferLen;
18 }
19
20 // Helper: copy the raw (unresolved) absolute path into resolvedOut.
21 // If path is relative, it is prepended with baseDir.
22 307 static Status buildAbsolutePath(const char* path, const char* baseDir, char* resolvedOut, FwSizeType resolvedSize) {
23 307 FW_ASSERT(path != nullptr);
24 307 FW_ASSERT(resolvedOut != nullptr);
25
26
2/2
✓ Branch 1 taken 103 times.
✓ Branch 2 taken 204 times.
307 if (path[0] != '/') {
27 // Relative path: baseDir is required and must start with '/'
28
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 102 times.
103 if (baseDir == nullptr) {
29 1 return INVALID_PATH;
30 }
31 102 const FwSizeType baseDirLen = Fw::StringUtils::string_length(baseDir, MAX_PATH_LENGTH);
32
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 102 times.
102 if (!isValidCString(baseDirLen, MAX_PATH_LENGTH)) {
33 ✗ return INVALID_PATH;
34 }
35
2/4
✓ Branch 0 taken 102 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 102 times.
102 if (baseDirLen == 0 || baseDir[0] != '/') {
36 ✗ return INVALID_PATH;
37 }
38 102 const FwSizeType pathLen = Fw::StringUtils::string_length(path, MAX_PATH_LENGTH);
39
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 102 times.
102 if (!isValidCString(pathLen, MAX_PATH_LENGTH)) {
40 ✗ return INVALID_PATH;
41 }
42
2/2
✓ Branch 2 taken 95 times.
✓ Branch 3 taken 7 times.
102 const FwSizeType needsSlash = (baseDir[baseDirLen - 1] != '/') ? 1 : 0;
43
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 99 times.
102 if ((baseDirLen + needsSlash + pathLen + 1) > resolvedSize) {
44 3 return TOO_LONG;
45 }
46
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 99 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 99 times.
99 (void)std::memcpy(resolvedOut, baseDir, baseDirLen);
47 99 FwSizeType pos = baseDirLen;
48
2/2
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 6 times.
99 if (needsSlash) {
49 93 resolvedOut[pos++] = '/';
50 }
51
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 99 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 99 times.
99 (void)std::memcpy(&resolvedOut[pos], path, pathLen);
52 99 pos += pathLen;
53 99 resolvedOut[pos] = '\0';
54 } else {
55 204 const FwSizeType pathLen = Fw::StringUtils::string_length(path, MAX_PATH_LENGTH);
56
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 203 times.
204 if (!isValidCString(pathLen, MAX_PATH_LENGTH)) {
57 1 return INVALID_PATH;
58 }
59
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 202 times.
203 if (pathLen + 1 > resolvedSize) {
60 1 return TOO_LONG;
61 }
62
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 202 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 202 times.
202 (void)std::memcpy(resolvedOut, path, pathLen + 1);
63 }
64 301 return VALID;
65 }
66
67 // Helper: resolve `.`, `..`, and `//` in place within resolvedOut.
68 //
69 // Algorithm: single-pass using read (readPos) and write (writePos) pointers
70 // on the same buffer. Since resolving only shrinks or maintains length,
71 // writePos <= readPos always holds, making memmove safe for overlapping regions.
72 //
73 // For each path segment between '/' separators:
74 // - Empty or `.` segments are skipped (no-op)
75 // - `..` segments back up writePos to the previous '/'
76 // - Normal segments are shifted left (via memmove) to writePos
77 301 static Status resolveInPlace(char* resolvedOut) {
78 301 FW_ASSERT(resolvedOut != nullptr);
79 301 FW_ASSERT(resolvedOut[0] == '/');
80
81 301 const FwSizeType pathLength = Fw::StringUtils::string_length(resolvedOut, MAX_PATH_LENGTH);
82
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 301 times.
301 if (!isValidCString(pathLength, MAX_PATH_LENGTH)) {
83 ✗ return INVALID_PATH;
84 }
85
86 301 FwSizeType writePos = 1; // position past root '/'
87
88
2/2
✓ Branch 0 taken 1364 times.
✓ Branch 1 taken 301 times.
1665 for (FwSizeType readPos = 1; readPos <= pathLength;) {
89 // Mark start of current segment
90 1364 FwSizeType segStart = readPos;
91
92 // Scan forward to find the end of this segment (next '/' or end of string)
93
4/4
✓ Branch 0 taken 9189 times.
✓ Branch 1 taken 301 times.
✓ Branch 4 taken 8126 times.
✓ Branch 5 taken 1063 times.
9490 for (; readPos < pathLength && resolvedOut[readPos] != '/'; readPos++) {
94 }
95 1364 FwSizeType segLen = readPos - segStart;
96
97 // Advance readPos past the '/' separator (or force exit at end)
98
2/2
✓ Branch 0 taken 1063 times.
✓ Branch 1 taken 301 times.
1364 if (readPos < pathLength) {
99 1063 readPos++;
100 } else {
101 301 readPos = pathLength + 1;
102 }
103
104 // Skip empty segments (caused by consecutive slashes like "//")
105
2/2
✓ Branch 0 taken 71 times.
✓ Branch 1 taken 1293 times.
1364 if (segLen == 0) {
106 71 continue;
107 }
108 // Skip `.` segments (current directory, no-op)
109
4/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1286 times.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 1 times.
1293 if (segLen == 1 && resolvedOut[segStart] == '.') {
110 6 continue;
111 }
112 // Handle `..` segments by backing up to the previous directory
113
5/6
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 1248 times.
✓ Branch 4 taken 24 times.
✓ Branch 5 taken 15 times.
✓ Branch 8 taken 24 times.
✗ Branch 9 not taken.
1287 if (segLen == 2 && resolvedOut[segStart] == '.' && resolvedOut[segStart + 1] == '.') {
114
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 3 times.
24 if (writePos > 1) {
115 21 writePos--;
116
4/4
✓ Branch 0 taken 139 times.
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 120 times.
✓ Branch 5 taken 19 times.
141 for (; writePos > 1 && resolvedOut[writePos - 1] != '/'; writePos--) {
117 }
118 }
119 24 continue;
120 }
121
122 // Normal segment: shift left to writePos
123
2/4
✗ Branch 2 not taken.
✓ Branch 3 taken 1263 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1263 times.
1263 (void)std::memmove(&resolvedOut[writePos], &resolvedOut[segStart], segLen);
124 1263 writePos += segLen;
125 1263 resolvedOut[writePos++] = '/';
126 }
127
128 // Remove trailing '/' (unless path is root "/")
129
2/2
✓ Branch 0 taken 264 times.
✓ Branch 1 taken 37 times.
301 if (writePos > 1) {
130 264 writePos--;
131 }
132 301 resolvedOut[writePos] = '\0';
133 301 return VALID;
134 }
135
136 310 Status resolvePath(const char* path, const char* baseDir, char* resolvedOut, FwSizeType resolvedSize) {
137 310 FW_ASSERT(resolvedOut != nullptr);
138
4/4
✓ Branch 0 taken 309 times.
✓ Branch 1 taken 1 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 307 times.
310 if (path == nullptr || path[0] == '\0') {
139 3 return INVALID_PATH;
140 }
141
142 307 const Status buildStatus = buildAbsolutePath(path, baseDir, resolvedOut, resolvedSize);
143
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 301 times.
307 if (buildStatus != VALID) {
144 6 return buildStatus;
145 }
146 301 return resolveInPlace(resolvedOut);
147 }
148
149 1 Status resolvePath(const Fw::ConstStringBase& path, const Fw::ConstStringBase& baseDir, Fw::StringBase& resolvedOut) {
150 // SAFETY: write directly into resolvedOut's internal buffer to avoid a temporary copy.
151 // StringBase::toChar() returns a pointer to the internal buffer, and resolvePath always
152 // null-terminates the output, keeping the StringBase in a valid state.
153 1 char* outBuffer = const_cast<char*>(resolvedOut.toChar());
154 // Cap at MAX_PATH_LENGTH so resolveInPlace's string_length scan stays consistent
155 1 const FwSizeType capacity = static_cast<FwSizeType>(resolvedOut.getCapacity());
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 const FwSizeType resolvedSize = (capacity > MAX_PATH_LENGTH) ? MAX_PATH_LENGTH : capacity;
157 1 return resolvePath(path.toChar(), baseDir.toChar(), outBuffer, resolvedSize);
158 }
159
160 289 Status resolveFromCwd(const char* path, char* resolvedOut, FwSizeType resolvedSize) {
161 289 FW_ASSERT(path != nullptr);
162 289 FW_ASSERT(resolvedOut != nullptr);
163
164
2/2
✓ Branch 1 taken 96 times.
✓ Branch 2 taken 193 times.
289 if (path[0] != '/') {
165 96 char cwdBuffer[MAX_PATH_LENGTH];
166
1/1
✓ Branch 1 taken 96 times.
96 const Os::FileSystem::Status cwdStatus = Os::FileSystem::getWorkingDirectory(cwdBuffer, MAX_PATH_LENGTH);
167
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
96 if (cwdStatus != Os::FileSystem::Status::OP_OK) {
168 ✗ return INVALID_PATH;
169 }
170
1/1
✓ Branch 1 taken 96 times.
96 return resolvePath(path, cwdBuffer, resolvedOut, resolvedSize);
171 }
172 193 return resolvePath(path, "/", resolvedOut, resolvedSize);
173 }
174
175 109 Status resolveDirectoryFromCwd(const char* path, char* resolvedOut, FwSizeType resolvedSize) {
176 109 FW_ASSERT(path != nullptr);
177 109 FW_ASSERT(resolvedOut != nullptr);
178
179 109 const Status status = resolveFromCwd(path, resolvedOut, resolvedSize);
180
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 107 times.
109 if (status != VALID) {
181 2 return status;
182 }
183 107 const FwSizeType len = Fw::StringUtils::string_length(resolvedOut, resolvedSize);
184
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 107 times.
107 if (len == 0) {
185 ✗ return INVALID_PATH;
186 }
187
2/2
✓ Branch 2 taken 106 times.
✓ Branch 3 taken 1 times.
107 if (resolvedOut[len - 1] != '/') {
188 // Need room for the '/' and the terminator
189
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 105 times.
106 if (len + 2 > resolvedSize) {
190 1 return TOO_LONG;
191 }
192 105 resolvedOut[len] = '/';
193 105 resolvedOut[len + 1] = '\0';
194 }
195 106 return VALID;
196 }
197
198 // Internal containment check on already-resolved paths.
199 // Verifies that resolvedPath starts with allowedDirectory as a prefix,
200 // and that the match occurs at a `/` boundary.
201 155 Status checkContainment(const char* resolvedPath, const char* allowedDirectory) {
202 155 FW_ASSERT(resolvedPath != nullptr);
203 155 FW_ASSERT(allowedDirectory != nullptr);
204
205 155 const FwSizeType allowedLen = Fw::StringUtils::string_length(allowedDirectory, MAX_PATH_LENGTH);
206 155 const FwSizeType pathLen = Fw::StringUtils::string_length(resolvedPath, MAX_PATH_LENGTH);
207
208
3/6
✓ Branch 1 taken 155 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 155 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 155 times.
155 if (!isValidCString(allowedLen, MAX_PATH_LENGTH) || !isValidCString(pathLen, MAX_PATH_LENGTH)) {
209 ✗ return INVALID_PATH;
210 }
211
4/4
✓ Branch 0 taken 154 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 153 times.
155 if (allowedLen == 0 || pathLen == 0) {
212 2 return OUTSIDE_SANDBOX;
213 }
214
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 153 times.
153 if (allowedDirectory[allowedLen - 1] != '/') {
215 ✗ return OUTSIDE_SANDBOX;
216 }
217
218 // Special case: path equals the allowed directory without trailing '/'.
219 // e.g. path="/data/uplink" matches allowedDirectory="/data/uplink/"
220 // This is valid because the path IS the sandbox directory itself.
221
5/8
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 152 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
153 if (pathLen == allowedLen - 1 && std::memcmp(resolvedPath, allowedDirectory, pathLen) == 0) {
222 1 return VALID;
223 }
224
225 // Normal case: path must be at least as long as allowed and start with it
226
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 138 times.
152 if (pathLen < allowedLen) {
227 14 return OUTSIDE_SANDBOX;
228 }
229
4/6
✗ Branch 0 not taken.
✓ Branch 1 taken 138 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 138 times.
✓ Branch 4 taken 16 times.
✓ Branch 5 taken 122 times.
138 if (std::memcmp(resolvedPath, allowedDirectory, allowedLen) != 0) {
230 16 return OUTSIDE_SANDBOX;
231 }
232 122 return VALID;
233 }
234
235 } // namespace FilePathUtils
236 } // namespace Os
237