Bug Summary

File:libexec/got-read-tag/../../lib/path.c
Warning:line 282, column 6
1st function call argument is an uninitialized value

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd6.9 -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 -setup-static-analyzer -mrelocation-model pic -pic-level 1 -pic-is-pie -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /usr/local/lib/clang/11.1.0 -I /home/ben/Projects/got/libexec/got-read-tag/../../include -I /home/ben/Projects/got/libexec/got-read-tag/../../lib -D GOT_LIBEXECDIR=/home/ben/bin -D GOT_VERSION=0.53-current -internal-isystem /usr/local/lib/clang/11.1.0/include -internal-externc-isystem /usr/include -O0 -fdebug-compilation-dir /home/ben/Projects/got/libexec/got-read-tag/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fgnuc-version=4.2.1 -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -o /home/ben/Projects/got/scan/2021-05-28-230913-68537-1 -x c /home/ben/Projects/got/libexec/got-read-tag/../../lib/path.c
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
46/* based on canonpath() from kern_pledge.c */
47const struct got_error *
48got_canonpath(const char *input, char *buf, size_t bufsize)
49{
50 const char *p;
51 char *q;
52
53 /* can't canon relative paths, don't bother */
54 if (!got_path_is_absolute(input)) {
55 if (strlcpy(buf, input, bufsize) >= bufsize)
56 return got_error(GOT_ERR_NO_SPACE9);
57 return NULL((void *)0);
58 }
59
60 p = input;
61 q = buf;
62 while (*p && (q - buf < bufsize)) {
63 if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
64 p += 1;
65
66 } else if (p[0] == '/' && p[1] == '.' &&
67 (p[2] == '/' || p[2] == '\0')) {
68 p += 2;
69
70 } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
71 (p[3] == '/' || p[3] == '\0')) {
72 p += 3;
73 if (q != buf) /* "/../" at start of buf */
74 while (*--q != '/')
75 continue;
76
77 } else {
78 *q++ = *p++;
79 }
80 }
81 if ((*p == '\0') && (q - buf < bufsize)) {
82 *q = 0;
83 return NULL((void *)0);
84 } else
85 return got_error(GOT_ERR_NO_SPACE9);
86}
87
88const struct got_error *
89got_path_skip_common_ancestor(char **child, const char *parent_abspath,
90 const char *abspath)
91{
92 const struct got_error *err = NULL((void *)0);
93 size_t len_parent, len, bufsize;
94
95 *child = NULL((void *)0);
96
97 len_parent = strlen(parent_abspath);
98 len = strlen(abspath);
99 if (len_parent >= len)
100 return got_error_path(abspath, GOT_ERR_BAD_PATH4);
101 if (strncmp(parent_abspath, abspath, len_parent) != 0)
102 return got_error_path(abspath, GOT_ERR_BAD_PATH4);
103 if (!got_path_is_root_dir(parent_abspath) && abspath[len_parent] != '/')
104 return got_error_path(abspath, GOT_ERR_BAD_PATH4);
105 while (abspath[len_parent] == '/')
106 abspath++;
107 bufsize = len - len_parent + 1;
108 *child = malloc(bufsize);
109 if (*child == NULL((void *)0))
110 return got_error_from_errno("malloc");
111 if (strlcpy(*child, abspath + len_parent, bufsize) >= bufsize) {
112 err = got_error_from_errno("strlcpy");
113 free(*child);
114 *child = NULL((void *)0);
115 return err;
116 }
117 return NULL((void *)0);
118}
119
120int
121got_path_is_root_dir(const char *path)
122{
123 while (*path == '/')
124 path++;
125 return (*path == '\0');
126}
127
128int
129got_path_is_current_dir(const char *path)
130{
131 return (path[0] == '.' && path[1] == '\0');
132}
133
134int
135got_path_is_child(const char *child, const char *parent, size_t parent_len)
136{
137 if (parent_len == 0 || got_path_is_root_dir(parent))
138 return 1;
139
140 if (strncmp(parent, child, parent_len) != 0)
141 return 0;
142 if (child[parent_len] != '/')
143 return 0;
144
145 return 1;
146}
147
148int
149got_path_cmp(const char *path1, const char *path2, size_t len1, size_t len2)
150{
151 size_t min_len;
152 size_t i = 0;
153
154 /* Leading directory separators are insignificant. */
155 while (path1[0] == '/') {
156 path1++;
157 len1--;
158 }
159 while (path2[0] == '/') {
160 path2++;
161 len2--;
162 }
163
164 min_len = MIN(len1, len2)((len1) < (len2) ? (len1) : (len2));
165
166 /* Skip over common prefix. */
167 while (i < min_len && path1[i] == path2[i])
168 i++;
169
170 /* Are the paths exactly equal (besides path separators)? */
171 if (len1 == len2 && i >= min_len)
172 return 0;
173
174 /* Skip over redundant trailing path seperators. */
175 while (path1[i] == '/' && path1[i + 1] == '/')
176 path1++;
177 while (path2[i] == '/' && path2[i + 1] == '/')
178 path2++;
179
180 /* Trailing path separators are insignificant. */
181 if (path1[i] == '/' && path1[i + 1] == '\0' && path2[i] == '\0')
182 return 0;
183 if (path2[i] == '/' && path2[i + 1] == '\0' && path1[i] == '\0')
184 return 0;
185
186 /* Order children in subdirectories directly after their parents. */
187 if (path1[i] == '/' && path2[i] == '\0')
188 return 1;
189 if (path2[i] == '/' && path1[i] == '\0')
190 return -1;
191 if (path1[i] == '/' && path2[i] != '\0')
192 return -1;
193 if (path2[i] == '/' && path1[i] != '\0')
194 return 1;
195
196 /* Next character following the common prefix determines order. */
197 return (unsigned char)path1[i] < (unsigned char)path2[i] ? -1 : 1;
198}
199
200const struct got_error *
201got_pathlist_insert(struct got_pathlist_entry **inserted,
202 struct got_pathlist_head *pathlist, const char *path, void *data)
203{
204 struct got_pathlist_entry *new, *pe;
205
206 if (inserted)
207 *inserted = NULL((void *)0);
208
209 new = malloc(sizeof(*new));
210 if (new == NULL((void *)0))
211 return got_error_from_errno("malloc");
212 new->path = path;
213 new->path_len = strlen(path);
214 new->data = data;
215
216 /*
217 * Many callers will provide paths in a somewhat sorted order while
218 * constructing a path list from inputs such as tree objects or
219 * dirents. Iterating backwards from the tail of the list should
220 * be more efficient than traversing through the entire list each
221 * time an element is inserted.
222 */
223 pe = TAILQ_LAST(pathlist, got_pathlist_head)(*(((struct got_pathlist_head *)((pathlist)->tqh_last))->
tqh_last))
;
224 while (pe) {
225 int cmp = got_path_cmp(pe->path, new->path,
226 pe->path_len, new->path_len);
227 if (cmp == 0) {
228 free(new); /* duplicate */
229 return NULL((void *)0);
230 } else if (cmp < 0) {
231 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)
;
232 if (inserted)
233 *inserted = new;
234 return NULL((void *)0);
235 }
236 pe = TAILQ_PREV(pe, got_pathlist_head, entry)(*(((struct got_pathlist_head *)((pe)->entry.tqe_prev))->
tqh_last))
;
237 }
238
239 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)
;
240 if (inserted)
241 *inserted = new;
242 return NULL((void *)0);
243}
244
245const struct got_error *
246got_pathlist_append(struct got_pathlist_head *pathlist,
247 const char *path, void *data)
248{
249 struct got_pathlist_entry *new;
250
251 new = malloc(sizeof(*new));
252 if (new == NULL((void *)0))
253 return got_error_from_errno("malloc");
254 new->path = path;
255 new->path_len = strlen(path);
256 new->data = data;
257 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)
;
258 return NULL((void *)0);
259}
260
261void
262got_pathlist_free(struct got_pathlist_head *pathlist)
263{
264 struct got_pathlist_entry *pe;
265
266 while ((pe = TAILQ_FIRST(pathlist)((pathlist)->tqh_first)) != NULL((void *)0)) {
267 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)
;
268 free(pe);
269 }
270}
271
272static const struct got_error *
273make_parent_dirs(const char *abspath)
274{
275 const struct got_error *err = NULL((void *)0);
276 char *parent;
6
'parent' declared without an initial value
277
278 err = got_path_dirname(&parent, abspath);
7
Calling 'got_path_dirname'
12
Returning from 'got_path_dirname'
279 if (err)
13
Assuming 'err' is null
14
Taking false branch
280 return err;
281
282 if (mkdir(parent, GOT_DEFAULT_DIR_MODE(0040000 | 0000700 | 0000040|0000010 | 0000004|0000001)) == -1) {
15
1st function call argument is an uninitialized value
283 if (errno(*__errno()) == ENOENT2) {
284 err = make_parent_dirs(parent);
285 if (err)
286 goto done;
287 if (mkdir(parent, GOT_DEFAULT_DIR_MODE(0040000 | 0000700 | 0000040|0000010 | 0000004|0000001)) == -1) {
288 err = got_error_from_errno2("mkdir", parent);
289 goto done;
290 }
291 } else
292 err = got_error_from_errno2("mkdir", parent);
293 }
294done:
295 free(parent);
296 return err;
297}
298
299const struct got_error *
300got_path_mkdir(const char *abspath)
301{
302 const struct got_error *err = NULL((void *)0);
303
304 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE(0040000 | 0000700 | 0000040|0000010 | 0000004|0000001)) == -1) {
1
Assuming the condition is true
2
Taking true branch
305 if (errno(*__errno()) == ENOENT2) {
3
Assuming the condition is true
4
Taking true branch
306 err = make_parent_dirs(abspath);
5
Calling 'make_parent_dirs'
307 if (err)
308 goto done;
309 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE(0040000 | 0000700 | 0000040|0000010 | 0000004|0000001)) == -1)
310 err = got_error_from_errno2("mkdir", abspath);
311 } else
312 err = got_error_from_errno2("mkdir", abspath);
313 }
314
315done:
316 return err;
317}
318
319int
320got_path_dir_is_empty(const char *dir)
321{
322 DIR *d;
323 struct dirent *dent;
324 int empty = 1;
325
326 d = opendir(dir);
327 if (d == NULL((void *)0))
328 return 1;
329
330 while ((dent = readdir(d)) != NULL((void *)0)) {
331 if (strcmp(dent->d_name, ".") == 0 ||
332 strcmp(dent->d_name, "..") == 0)
333 continue;
334
335 empty = 0;
336 break;
337 }
338
339 closedir(d);
340 return empty;
341}
342
343const struct got_error *
344got_path_dirname(char **parent, const char *path)
345{
346 char buf[PATH_MAX1024];
347 char *p;
348
349 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
8
Assuming the condition is true
9
Taking true branch
350 return got_error(GOT_ERR_NO_SPACE9);
10
Returning without writing to '*parent'
11
Returning pointer, which participates in a condition later
351
352 p = dirname(buf);
353 if (p == NULL((void *)0))
354 return got_error_from_errno2("dirname", path);
355
356 if (p[0] == '.' && p[1] == '\0')
357 return got_error_path(path, GOT_ERR_BAD_PATH4);
358
359 *parent = strdup(p);
360 if (*parent == NULL((void *)0))
361 return got_error_from_errno("strdup");
362
363 return NULL((void *)0);
364}
365
366const struct got_error *
367got_path_dirent_type(int *type, const char *path_parent, struct dirent *dent)
368{
369 const struct got_error *err = NULL((void *)0);
370 char *path_child;
371 struct stat sb;
372
373 if (dent->d_type != DT_UNKNOWN0) {
374 *type = dent->d_type;
375 return NULL((void *)0);
376 }
377
378 *type = DT_UNKNOWN0;
379
380 /*
381 * This is a fallback to accommodate filesystems which do not
382 * provide directory entry type information. DT_UNKNOWN directory
383 * entries occur on NFS mounts without "readdir plus" RPC.
384 */
385
386 if (asprintf(&path_child, "%s/%s", path_parent, dent->d_name) == -1)
387 return got_error_from_errno("asprintf");
388
389 if (lstat(path_child, &sb) == -1) {
390 err = got_error_from_errno2("lstat", path_child);
391 goto done;
392 }
393
394 if (S_ISFIFO(sb.st_mode)((sb.st_mode & 0170000) == 0010000))
395 *type = DT_FIFO1;
396 else if (S_ISCHR(sb.st_mode)((sb.st_mode & 0170000) == 0020000))
397 *type = DT_CHR2;
398 else if (S_ISDIR(sb.st_mode)((sb.st_mode & 0170000) == 0040000))
399 *type = DT_DIR4;
400 else if (S_ISBLK(sb.st_mode)((sb.st_mode & 0170000) == 0060000))
401 *type = DT_BLK6;
402 else if (S_ISLNK(sb.st_mode)((sb.st_mode & 0170000) == 0120000))
403 *type = DT_LNK10;
404 else if (S_ISREG(sb.st_mode)((sb.st_mode & 0170000) == 0100000))
405 *type = DT_REG8;
406 else if (S_ISSOCK(sb.st_mode)((sb.st_mode & 0170000) == 0140000))
407 *type = DT_SOCK12;
408done:
409 free(path_child);
410 return err;
411}
412
413const struct got_error *
414got_path_basename(char **s, const char *path)
415{
416 char buf[PATH_MAX1024];
417 char *base;
418
419 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
420 return got_error(GOT_ERR_NO_SPACE9);
421
422 base = basename(buf);
423 if (base == NULL((void *)0))
424 return got_error_from_errno2("basename", path);
425
426 *s = strdup(base);
427 if (*s == NULL((void *)0))
428 return got_error_from_errno("strdup");
429
430 return NULL((void *)0);
431}
432
433void
434got_path_strip_trailing_slashes(char *path)
435{
436 size_t x;
437
438 x = strlen(path);
439 while (x-- > 0 && path[x] == '/')
440 path[x] = '\0';
441}
442
443/* based on findprog() from usr.sbin/which/which.c */
444const struct got_error *
445got_path_find_prog(char **filename, const char *prog)
446{
447 const struct got_error *err = NULL((void *)0);
448 char *p;
449 int len;
450 struct stat sbuf;
451 char *path, *pathcpy;
452
453 *filename = NULL((void *)0);
454
455 path = getenv("PATH");
456 if (path == NULL((void *)0))
457 path = _PATH_DEFPATH"/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11R6/bin:/usr/local/bin:/usr/local/sbin";
458
459 /* Special case if prog contains '/' */
460 if (strchr(prog, '/')) {
461 if ((stat(prog, &sbuf) == 0) && S_ISREG(sbuf.st_mode)((sbuf.st_mode & 0170000) == 0100000) &&
462 access(prog, X_OK0x01) == 0) {
463 *filename = strdup(prog);
464 if (*filename == NULL((void *)0))
465 return got_error_from_errno("strdup");
466 }
467 return NULL((void *)0);
468 }
469
470 if ((path = strdup(path)) == NULL((void *)0))
471 return got_error_from_errno("strdup");
472 pathcpy = path;
473
474 while ((p = strsep(&pathcpy, ":")) != NULL((void *)0)) {
475 if (*p == '\0')
476 p = ".";
477
478 len = strlen(p);
479 while (len > 0 && p[len-1] == '/')
480 p[--len] = '\0'; /* strip trailing '/' */
481
482 if (asprintf(filename, "%s/%s", p, prog) == -1) {
483 err = got_error_from_errno("asprintf");
484 break;
485 }
486 if ((stat(*filename, &sbuf) == 0) && S_ISREG(sbuf.st_mode)((sbuf.st_mode & 0170000) == 0100000) &&
487 access(*filename, X_OK0x01) == 0)
488 break;
489 free(*filename);
490 *filename = NULL((void *)0);
491 continue;
492 }
493 free(path);
494 return err;
495}
496
497const struct got_error *
498got_path_create_file(const char *path, const char *content)
499{
500 const struct got_error *err = NULL((void *)0);
501 int fd = -1;
502
503 fd = open(path, O_RDWR0x0002 | O_CREAT0x0200 | O_EXCL0x0800 | O_NOFOLLOW0x0100,
504 GOT_DEFAULT_FILE_MODE(0100000 | 0000400|0000200 | 0000040 | 0000004));
505 if (fd == -1) {
506 err = got_error_from_errno2("open", path);
507 goto done;
508 }
509
510 if (content) {
511 int len = dprintf(fd, "%s\n", content);
512 if (len != strlen(content) + 1) {
513 err = got_error_from_errno("dprintf");
514 goto done;
515 }
516 }
517
518done:
519 if (fd != -1 && close(fd) == -1 && err == NULL((void *)0))
520 err = got_error_from_errno("close");
521 return err;
522}