Bug Summary

File:/usr/ports/src/got/libexec/got-read-tree/../../lib/path.c
Warning:line 295, column 6
1st function call argument is an uninitialized value

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple amd64-unknown-openbsd6.6 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name path.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -mrelocation-model pic -pic-level 1 -pic-is-pie -mthread-model posix -mdisable-fp-elim -relaxed-aliasing -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -dwarf-column-info -debugger-tuning=gdb -resource-dir /usr/local/lib/clang/8.0.1 -I /usr/ports/src/got/libexec/got-read-tree/../../include -I /usr/ports/src/got/libexec/got-read-tree/../../lib -D GOT_LIBEXECDIR=/root/bin -D GOT_VERSION=0.19-current -O0 -fdebug-compilation-dir /usr/ports/src/got/libexec/got-read-tree -ferror-limit 19 -fmessage-length 0 -fwrapv -D_RET_PROTECTOR -ret-protector -fobjc-runtime=gnustep -fdiagnostics-show-option -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -o /tmp/scan-build-2019-10-25-192004-30128-1 -x c /usr/ports/src/got/libexec/got-read-tree/../../lib/path.c -faddrsig
1/*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 * Copyright (c) 2015 Theo de Raadt <deraadt@openbsd.org>
4 * Copyright (c) 1997 Todd C. Miller <millert@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <sys/queue.h>
20#include <sys/stat.h>
21
22#include <errno(*__errno()).h>
23#include <fcntl.h>
24#include <limits.h>
25#include <libgen.h>
26#include <stdlib.h>
27#include <unistd.h>
28#include <stdio.h>
29#include <string.h>
30#include <dirent.h>
31#include <paths.h>
32
33#include "got_error.h"
34#include "got_path.h"
35
36#ifndef MIN
37#define MIN(_a,_b)((_a) < (_b) ? (_a) : (_b)) ((_a) < (_b) ? (_a) : (_b))
38#endif
39
40int
41got_path_is_absolute(const char *path)
42{
43 return path[0] == '/';
44}
45
46char *
47got_path_get_absolute(const char *relpath)
48{
49 char cwd[PATH_MAX1024];
50 char *abspath;
51
52 if (getcwd(cwd, sizeof(cwd)) == NULL((void *)0))
53 return NULL((void *)0);
54
55 if (asprintf(&abspath, "%s/%s/", cwd, relpath) == -1)
56 return NULL((void *)0);
57
58 return abspath;
59}
60
61/* based on canonpath() from kern_pledge.c */
62const struct got_error *
63got_canonpath(const char *input, char *buf, size_t bufsize)
64{
65 const char *p;
66 char *q;
67
68 /* can't canon relative paths, don't bother */
69 if (!got_path_is_absolute(input)) {
70 if (strlcpy(buf, input, bufsize) >= bufsize)
71 return got_error(GOT_ERR_NO_SPACE9);
72 return NULL((void *)0);
73 }
74
75 p = input;
76 q = buf;
77 while (*p && (q - buf < bufsize)) {
78 if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
79 p += 1;
80
81 } else if (p[0] == '/' && p[1] == '.' &&
82 (p[2] == '/' || p[2] == '\0')) {
83 p += 2;
84
85 } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
86 (p[3] == '/' || p[3] == '\0')) {
87 p += 3;
88 if (q != buf) /* "/../" at start of buf */
89 while (*--q != '/')
90 continue;
91
92 } else {
93 *q++ = *p++;
94 }
95 }
96 if ((*p == '\0') && (q - buf < bufsize)) {
97 *q = 0;
98 return NULL((void *)0);
99 } else
100 return got_error(GOT_ERR_NO_SPACE9);
101}
102
103const struct got_error *
104got_path_skip_common_ancestor(char **child, const char *parent_abspath,
105 const char *abspath)
106{
107 const struct got_error *err = NULL((void *)0);
108 size_t len_parent, len, bufsize;
109
110 *child = NULL((void *)0);
111
112 len_parent = strlen(parent_abspath);
113 len = strlen(abspath);
114 if (len_parent >= len)
115 return got_error(GOT_ERR_BAD_PATH4);
116 if (strncmp(parent_abspath, abspath, len_parent) != 0)
117 return got_error(GOT_ERR_BAD_PATH4);
118 if (!got_path_is_root_dir(parent_abspath) && abspath[len_parent] != '/')
119 return got_error(GOT_ERR_BAD_PATH4);
120 while (abspath[len_parent] == '/')
121 abspath++;
122 bufsize = len - len_parent + 1;
123 *child = malloc(bufsize);
124 if (*child == NULL((void *)0))
125 return got_error_from_errno("malloc");
126 if (strlcpy(*child, abspath + len_parent, bufsize) >= bufsize) {
127 err = got_error_from_errno("strlcpy");
128 free(*child);
129 *child = NULL((void *)0);
130 return err;
131 }
132 return NULL((void *)0);
133}
134
135int
136got_path_is_root_dir(const char *path)
137{
138 return (path[0] == '/' && path[1] == '\0');
139}
140
141int
142got_path_is_current_dir(const char *path)
143{
144 return (path[0] == '.' && path[1] == '\0');
145}
146
147int
148got_path_is_child(const char *child, const char *parent, size_t parent_len)
149{
150 if (parent_len == 0 || got_path_is_root_dir(parent))
151 return 1;
152
153 if (strncmp(parent, child, parent_len) != 0)
154 return 0;
155 if (child[parent_len] != '/')
156 return 0;
157
158 return 1;
159}
160
161int
162got_path_cmp(const char *path1, const char *path2, size_t len1, size_t len2)
163{
164 size_t min_len;
165 size_t i = 0;
166
167 /* Leading directory separators are insignificant. */
168 while (path1[0] == '/') {
169 path1++;
170 len1--;
171 }
172 while (path2[0] == '/') {
173 path2++;
174 len2--;
175 }
176
177 min_len = MIN(len1, len2)((len1) < (len2) ? (len1) : (len2));
178
179 /* Skip over common prefix. */
180 while (i < min_len && path1[i] == path2[i])
181 i++;
182
183 /* Are the paths exactly equal (besides path separators)? */
184 if (len1 == len2 && i >= min_len)
185 return 0;
186
187 /* Skip over redundant trailing path seperators. */
188 while (path1[i] == '/' && path1[i + 1] == '/')
189 path1++;
190 while (path2[i] == '/' && path2[i + 1] == '/')
191 path2++;
192
193 /* Trailing path separators are insignificant. */
194 if (path1[i] == '/' && path1[i + 1] == '\0' && path2[i] == '\0')
195 return 0;
196 if (path2[i] == '/' && path2[i + 1] == '\0' && path1[i] == '\0')
197 return 0;
198
199 /* Order children in subdirectories directly after their parents. */
200 if (path1[i] == '/' && path2[i] == '\0')
201 return 1;
202 if (path2[i] == '/' && path1[i] == '\0')
203 return -1;
204 if (path1[i] == '/' && path2[i] != '\0')
205 return -1;
206 if (path2[i] == '/' && path1[i] != '\0')
207 return 1;
208
209 /* Next character following the common prefix determines order. */
210 return (unsigned char)path1[i] < (unsigned char)path2[i] ? -1 : 1;
211}
212
213const struct got_error *
214got_pathlist_insert(struct got_pathlist_entry **inserted,
215 struct got_pathlist_head *pathlist, const char *path, void *data)
216{
217 struct got_pathlist_entry *new, *pe;
218
219 if (inserted)
220 *inserted = NULL((void *)0);
221
222 new = malloc(sizeof(*new));
223 if (new == NULL((void *)0))
224 return got_error_from_errno("malloc");
225 new->path = path;
226 new->path_len = strlen(path);
227 new->data = data;
228
229 /*
230 * Many callers will provide paths in a somewhat sorted order while
231 * constructing a path list from inputs such as tree objects or
232 * dirents. Iterating backwards from the tail of the list should
233 * be more efficient than traversing through the entire list each
234 * time an element is inserted.
235 */
236 pe = TAILQ_LAST(pathlist, got_pathlist_head)(*(((struct got_pathlist_head *)((pathlist)->tqh_last))->
tqh_last))
;
237 while (pe) {
238 int cmp = got_path_cmp(pe->path, new->path,
239 pe->path_len, new->path_len);
240 if (cmp == 0) {
241 free(new); /* duplicate */
242 return NULL((void *)0);
243 } else if (cmp < 0) {
244 TAILQ_INSERT_AFTER(pathlist, pe, new, entry)do { if (((new)->entry.tqe_next = (pe)->entry.tqe_next)
!= ((void *)0)) (new)->entry.tqe_next->entry.tqe_prev =
&(new)->entry.tqe_next; else (pathlist)->tqh_last =
&(new)->entry.tqe_next; (pe)->entry.tqe_next = (new
); (new)->entry.tqe_prev = &(pe)->entry.tqe_next; }
while (0)
;
245 if (inserted)
246 *inserted = new;
247 return NULL((void *)0);
248 }
249 pe = TAILQ_PREV(pe, got_pathlist_head, entry)(*(((struct got_pathlist_head *)((pe)->entry.tqe_prev))->
tqh_last))
;
250 }
251
252 TAILQ_INSERT_HEAD(pathlist, new, entry)do { if (((new)->entry.tqe_next = (pathlist)->tqh_first
) != ((void *)0)) (pathlist)->tqh_first->entry.tqe_prev
= &(new)->entry.tqe_next; else (pathlist)->tqh_last
= &(new)->entry.tqe_next; (pathlist)->tqh_first = (
new); (new)->entry.tqe_prev = &(pathlist)->tqh_first
; } while (0)
;
253 if (inserted)
254 *inserted = new;
255 return NULL((void *)0);
256}
257
258const struct got_error *
259got_pathlist_append(struct got_pathlist_head *pathlist,
260 const char *path, void *data)
261{
262 struct got_pathlist_entry *new;
263
264 new = malloc(sizeof(*new));
265 if (new == NULL((void *)0))
266 return got_error_from_errno("malloc");
267 new->path = path;
268 new->path_len = strlen(path);
269 new->data = data;
270 TAILQ_INSERT_TAIL(pathlist, new, entry)do { (new)->entry.tqe_next = ((void *)0); (new)->entry.
tqe_prev = (pathlist)->tqh_last; *(pathlist)->tqh_last =
(new); (pathlist)->tqh_last = &(new)->entry.tqe_next
; } while (0)
;
271 return NULL((void *)0);
272}
273
274void
275got_pathlist_free(struct got_pathlist_head *pathlist)
276{
277 struct got_pathlist_entry *pe;
278
279 while ((pe = TAILQ_FIRST(pathlist)((pathlist)->tqh_first)) != NULL((void *)0)) {
280 TAILQ_REMOVE(pathlist, pe, entry)do { if (((pe)->entry.tqe_next) != ((void *)0)) (pe)->entry
.tqe_next->entry.tqe_prev = (pe)->entry.tqe_prev; else (
pathlist)->tqh_last = (pe)->entry.tqe_prev; *(pe)->entry
.tqe_prev = (pe)->entry.tqe_next; ; ; } while (0)
;
281 free(pe);
282 }
283}
284
285static const struct got_error *
286make_parent_dirs(const char *abspath)
287{
288 const struct got_error *err = NULL((void *)0);
289 char *parent;
6
'parent' declared without an initial value
290
291 err = got_path_dirname(&parent, abspath);
7
Calling 'got_path_dirname'
11
Returning from 'got_path_dirname'
292 if (err)
12
Assuming 'err' is null
13
Taking false branch
293 return err;
294
295 if (mkdir(parent, GOT_DEFAULT_DIR_MODE(0000700 | 0000040|0000010 | 0000004|0000001)) == -1) {
14
1st function call argument is an uninitialized value
296 if (errno(*__errno()) == ENOENT2) {
297 err = make_parent_dirs(parent);
298 if (err)
299 goto done;
300 if (mkdir(parent, GOT_DEFAULT_DIR_MODE(0000700 | 0000040|0000010 | 0000004|0000001)) == -1) {
301 err = got_error_from_errno2("mkdir", parent);
302 goto done;
303 }
304 } else
305 err = got_error_from_errno2("mkdir", parent);
306 }
307done:
308 free(parent);
309 return err;
310}
311
312const struct got_error *
313got_path_mkdir(const char *abspath)
314{
315 const struct got_error *err = NULL((void *)0);
316
317 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE(0000700 | 0000040|0000010 | 0000004|0000001)) == -1) {
1
Assuming the condition is true
2
Taking true branch
318 if (errno(*__errno()) == ENOENT2) {
3
Assuming the condition is true
4
Taking true branch
319 err = make_parent_dirs(abspath);
5
Calling 'make_parent_dirs'
320 if (err)
321 goto done;
322 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE(0000700 | 0000040|0000010 | 0000004|0000001)) == -1)
323 err = got_error_from_errno2("mkdir", abspath);
324 } else
325 err = got_error_from_errno2("mkdir", abspath);
326 }
327
328done:
329 return err;
330}
331
332int
333got_path_dir_is_empty(const char *dir)
334{
335 DIR *d;
336 struct dirent *dent;
337 int empty = 1;
338
339 d = opendir(dir);
340 if (d == NULL((void *)0))
341 return 1;
342
343 while ((dent = readdir(d)) != NULL((void *)0)) {
344 if (strcmp(dent->d_name, ".") == 0 ||
345 strcmp(dent->d_name, "..") == 0)
346 continue;
347
348 empty = 0;
349 break;
350 }
351
352 closedir(d);
353 return empty;
354}
355
356const struct got_error *
357got_path_dirname(char **parent, const char *path)
358{
359 char *p;
360
361 p = dirname(path);
362 if (p == NULL((void *)0))
8
Assuming 'p' is equal to NULL
9
Taking true branch
363 return got_error_from_errno2("dirname", path);
10
Returning without writing to '*parent'
364
365 if (p[0] == '.' && p[1] == '\0')
366 return got_error(GOT_ERR_BAD_PATH4);
367
368 *parent = strdup(p);
369 if (*parent == NULL((void *)0))
370 return got_error_from_errno("strdup");
371
372 return NULL((void *)0);
373}
374
375const struct got_error *
376got_path_basename(char **s, const char *path)
377{
378 char *base;
379
380 base = basename(path);
381 if (base == NULL((void *)0))
382 return got_error_from_errno2("basename", path);
383
384 *s = strdup(base);
385 if (*s == NULL((void *)0))
386 return got_error_from_errno("strdup");
387
388 return NULL((void *)0);
389}
390
391void
392got_path_strip_trailing_slashes(char *path)
393{
394 int x;
395
396 while (path[x = strlen(path) - 1] == '/')
397 path[x] = '\0';
398}
399
400/* based on findprog() from usr.sbin/which/which.c */
401const struct got_error *
402got_path_find_prog(char **filename, const char *prog)
403{
404 const struct got_error *err = NULL((void *)0);
405 char *p;
406 int len;
407 struct stat sbuf;
408 char *path, *pathcpy;
409
410 *filename = NULL((void *)0);
411
412 path = getenv("PATH");
413 if (path == NULL((void *)0))
414 path = _PATH_DEFPATH"/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11R6/bin:/usr/local/bin:/usr/local/sbin";
415
416 /* Special case if prog contains '/' */
417 if (strchr(prog, '/')) {
418 if ((stat(prog, &sbuf) == 0) && S_ISREG(sbuf.st_mode)((sbuf.st_mode & 0170000) == 0100000) &&
419 access(prog, X_OK0x01) == 0) {
420 *filename = strdup(prog);
421 if (*filename == NULL((void *)0))
422 return got_error_from_errno("strdup");
423 }
424 return NULL((void *)0);
425 }
426
427 if ((path = strdup(path)) == NULL((void *)0))
428 return got_error_from_errno("strdup");
429 pathcpy = path;
430
431 while ((p = strsep(&pathcpy, ":")) != NULL((void *)0)) {
432 if (*p == '\0')
433 p = ".";
434
435 len = strlen(p);
436 while (len > 0 && p[len-1] == '/')
437 p[--len] = '\0'; /* strip trailing '/' */
438
439 if (asprintf(filename, "%s/%s", p, prog) == -1) {
440 err = got_error_from_errno("asprintf");
441 break;
442 }
443 if ((stat(*filename, &sbuf) == 0) && S_ISREG(sbuf.st_mode)((sbuf.st_mode & 0170000) == 0100000) &&
444 access(*filename, X_OK0x01) == 0)
445 break;
446 free(*filename);
447 *filename = NULL((void *)0);
448 continue;
449 }
450 free(path);
451 return err;
452}
453
454const struct got_error *
455got_path_create_file(const char *path, const char *content)
456{
457 const struct got_error *err = NULL((void *)0);
458 int fd = -1;
459
460 fd = open(path, O_RDWR0x0002 | O_CREAT0x0200 | O_EXCL0x0800 | O_NOFOLLOW0x0100,
461 GOT_DEFAULT_FILE_MODE(0000400|0000200 | 0000040 | 0000004));
462 if (fd == -1) {
463 err = got_error_from_errno2("open", path);
464 goto done;
465 }
466
467 if (content) {
468 int len = dprintf(fd, "%s\n", content);
469 if (len != strlen(content) + 1) {
470 err = got_error_from_errno("dprintf");
471 goto done;
472 }
473 }
474
475done:
476 if (fd != -1 && close(fd) == -1 && err == NULL((void *)0))
477 err = got_error_from_errno("close");
478 return err;
479}