Bug Summary

File:src/usr.bin/mg/dir.c
Warning:line 57, column 7
Although the value stored to 'bufp' is used in the enclosing expression, the value is never actually read from 'bufp'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name dir.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 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/usr.bin/mg/obj -resource-dir /usr/local/lib/clang/13.0.0 -D REGEX -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/usr.bin/mg/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -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 -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c /usr/src/usr.bin/mg/dir.c
1/* $OpenBSD: dir.c,v 1.31 2019/06/28 13:35:02 deraadt Exp $ */
2
3/* This file is in the public domain. */
4
5/*
6 * Name: MG 2a
7 * Directory management functions
8 * Created: Ron Flax (ron@vsedev.vse.com)
9 * Modified for MG 2a by Mic Kaczmarczik 03-Aug-1987
10 */
11
12#include <sys/queue.h>
13#include <sys/stat.h>
14#include <signal.h>
15#include <stdio.h>
16#include <string.h>
17#include <unistd.h>
18
19#include "def.h"
20
21static char mgcwd[NFILEN1024];
22
23/*
24 * Initialize anything the directory management routines need.
25 */
26void
27dirinit(void)
28{
29 mgcwd[0] = '\0';
30 if (getcwd(mgcwd, sizeof(mgcwd)) == NULL((void *)0))
31 ewprintf("Can't get current directory!");
32 if (mgcwd[0] != '\0' && !(mgcwd[0] == '/' && mgcwd[1] == '\0'))
33 (void)strlcat(mgcwd, "/", sizeof(mgcwd));
34}
35
36/*
37 * Change current working directory.
38 */
39/* ARGSUSED */
40int
41changedir(int f, int n)
42{
43 char bufc[NFILEN1024], *bufp;
44
45 (void)strlcpy(bufc, mgcwd, sizeof(bufc));
46 if ((bufp = eread("Change default directory: ", bufc, NFILEN1024,
47 EFDEF0x0020 | EFNEW0x0008 | EFCR0x0010 | EFFILE0x0004)) == NULL((void *)0))
48 return (ABORT2);
49 else if (bufp[0] == '\0')
50 return (FALSE0);
51 /* Append trailing slash */
52 if (chdir(bufc) == -1) {
53 dobeep();
54 ewprintf("Can't change dir to %s", bufc);
55 return (FALSE0);
56 }
57 if ((bufp = getcwd(mgcwd, sizeof(mgcwd))) == NULL((void *)0)) {
Although the value stored to 'bufp' is used in the enclosing expression, the value is never actually read from 'bufp'
58 if (bufc[0] == '/')
59 (void)strlcpy(mgcwd, bufc, sizeof(mgcwd));
60 else
61 (void)strlcat(mgcwd, bufc, sizeof(mgcwd));
62 }
63 if (mgcwd[strlen(mgcwd) - 1] != '/')
64 (void)strlcat(mgcwd, "/", sizeof(mgcwd));
65 ewprintf("Current directory is now %s", mgcwd);
66 return (TRUE1);
67}
68
69/*
70 * Show current directory.
71 */
72/* ARGSUSED */
73int
74showcwdir(int f, int n)
75{
76 ewprintf("Current directory: %s", mgcwd);
77 return (TRUE1);
78}
79
80int
81getcwdir(char *buf, size_t len)
82{
83 if (strlcpy(buf, mgcwd, len) >= len)
84 return (FALSE0);
85
86 return (TRUE1);
87}
88
89/* Create the directory and it's parents. */
90/* ARGSUSED */
91int
92makedir(int f, int n)
93{
94 return (ask_makedir());
95}
96
97int
98ask_makedir(void)
99{
100
101 char bufc[NFILEN1024];
102 char *path;
103
104 if (getbufcwd(bufc, sizeof(bufc)) != TRUE1)
105 return (ABORT2);
106 if ((path = eread("Make directory: ", bufc, NFILEN1024,
107 EFDEF0x0020 | EFNEW0x0008 | EFCR0x0010 | EFFILE0x0004)) == NULL((void *)0))
108 return (ABORT2);
109 else if (path[0] == '\0')
110 return (FALSE0);
111
112 return (do_makedir(path));
113}
114
115int
116do_makedir(char *path)
117{
118 struct stat sb;
119 int finished, ishere;
120 mode_t dir_mode, f_mode, oumask;
121 char *slash;
122
123 if ((path = adjustname(path, TRUE1)) == NULL((void *)0))
124 return (FALSE0);
125
126 /* Remove trailing slashes */
127 slash = strrchr(path, '\0');
128 while (--slash > path && *slash == '/')
129 *slash = '\0';
130
131 slash = path;
132
133 oumask = umask(0);
134 f_mode = 0777 & ~oumask;
135 dir_mode = f_mode | S_IWUSR0000200 | S_IXUSR0000100;
136
137 for (;;) {
138 slash += strspn(slash, "/");
139 slash += strcspn(slash, "/");
140
141 finished = (*slash == '\0');
142 *slash = '\0';
143
144 ishere = !stat(path, &sb);
145 if (finished && ishere) {
146 dobeep();
147 ewprintf("Cannot create directory %s: file exists",
148 path);
149 return(FALSE0);
150 } else if (!finished && ishere && S_ISDIR(sb.st_mode)((sb.st_mode & 0170000) == 0040000)) {
151 *slash = '/';
152 continue;
153 }
154
155 if (mkdir(path, finished ? f_mode : dir_mode) == 0) {
156 if (f_mode > 0777 && chmod(path, f_mode) == -1) {
157 umask(oumask);
158 return (ABORT2);
159 }
160 } else {
161 if (!ishere || !S_ISDIR(sb.st_mode)((sb.st_mode & 0170000) == 0040000)) {
162 if (!ishere) {
163 dobeep();
164 ewprintf("Creating directory: "
165 "permission denied, %s", path);
166 } else
167 eerase();
168
169 umask(oumask);
170 return (FALSE0);
171 }
172 }
173
174 if (finished)
175 break;
176
177 *slash = '/';
178 }
179
180 eerase();
181 umask(oumask);
182 return (TRUE1);
183}