Bug Summary

File:src/bin/df/df.c
Warning:line 371, column 3
4th function call argument is an uninitialized value

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 df.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/bin/df/obj -resource-dir /usr/local/lib/clang/13.0.0 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/bin/df/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/bin/df/df.c
1/* $OpenBSD: df.c,v 1.60 2019/06/28 13:34:59 deraadt Exp $ */
2/* $NetBSD: df.c,v 1.21.2.1 1995/11/01 00:06:11 jtc Exp $ */
3
4/*
5 * Copyright (c) 1980, 1990, 1993, 1994
6 * The Regents of the University of California. All rights reserved.
7 * (c) UNIX System Laboratories, Inc.
8 * All or some portions of this file are derived from material licensed
9 * to the University of California by American Telephone and Telegraph
10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11 * the permission of UNIX System Laboratories, Inc.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38#include <sys/stat.h>
39#include <sys/mount.h>
40
41#include <err.h>
42#include <errno(*__errno()).h>
43#include <fcntl.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
47#include <unistd.h>
48#include <util.h>
49
50int bread(int, off_t, void *, int);
51static void bsdprint(struct statfs *, long, int);
52char *getmntpt(char *);
53static void maketypelist(char *);
54static void posixprint(struct statfs *, long, int);
55static void prthuman(struct statfs *sfsp, unsigned long long);
56static void prthumanval(long long);
57static void prtstat(struct statfs *, int, int, int);
58static long regetmntinfo(struct statfs **, long);
59static int selected(const char *);
60static __dead__attribute__((__noreturn__)) void usage(void);
61
62extern int e2fs_df(int, char *, struct statfs *);
63extern int ffs_df(int, char *, struct statfs *);
64static int raw_df(char *, struct statfs *);
65
66int hflag, iflag, kflag, lflag, nflag, Pflag;
67char **typelist = NULL((void *)0);
68
69int
70main(int argc, char *argv[])
71{
72 struct stat stbuf;
73 struct statfs *mntbuf;
74 long mntsize;
75 int ch, i;
76 int width, maxwidth;
77 char *mntpt;
78
79 if (pledge("stdio rpath", NULL((void *)0)) == -1)
1
Assuming the condition is false
2
Taking false branch
80 err(1, "pledge");
81
82 while ((ch = getopt(argc, argv, "hiklnPt:")) != -1)
3
Assuming the condition is false
4
Loop condition is false. Execution continues on line 112
83 switch (ch) {
84 case 'h':
85 hflag = 1;
86 kflag = 0;
87 break;
88 case 'i':
89 iflag = 1;
90 break;
91 case 'k':
92 kflag = 1;
93 hflag = 0;
94 break;
95 case 'l':
96 lflag = 1;
97 break;
98 case 'n':
99 nflag = 1;
100 break;
101 case 'P':
102 Pflag = 1;
103 break;
104 case 't':
105 if (typelist != NULL((void *)0))
106 errx(1, "only one -t option may be specified.");
107 maketypelist(optarg);
108 break;
109 default:
110 usage();
111 }
112 argc -= optind;
113 argv += optind;
114
115 if ((iflag || hflag) && Pflag) {
5
Assuming 'iflag' is 0
6
Assuming 'hflag' is not equal to 0
7
Assuming 'Pflag' is 0
8
Taking false branch
116 warnx("-h and -i are incompatible with -P");
117 usage();
118 }
119
120 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT2);
121 if (mntsize == 0)
9
Assuming 'mntsize' is not equal to 0
10
Taking false branch
122 err(1, "retrieving information on mounted file systems");
123
124 if (!*argv) {
11
Assuming the condition is true
12
Taking true branch
125 mntsize = regetmntinfo(&mntbuf, mntsize);
126 } else {
127 mntbuf = calloc(argc, sizeof(struct statfs));
128 if (mntbuf == NULL((void *)0))
129 err(1, NULL((void *)0));
130 mntsize = 0;
131 for (; *argv; argv++) {
132 if (stat(*argv, &stbuf) == -1) {
133 if ((mntpt = getmntpt(*argv)) == 0) {
134 warn("%s", *argv);
135 continue;
136 }
137 } else if (S_ISCHR(stbuf.st_mode)((stbuf.st_mode & 0170000) == 0020000) || S_ISBLK(stbuf.st_mode)((stbuf.st_mode & 0170000) == 0060000)) {
138 if (!raw_df(*argv, &mntbuf[mntsize]))
139 ++mntsize;
140 continue;
141 } else
142 mntpt = *argv;
143 /*
144 * Statfs does not take a `wait' flag, so we cannot
145 * implement nflag here.
146 */
147 if (!statfs(mntpt, &mntbuf[mntsize]))
148 if (lflag && (mntbuf[mntsize].f_flags & MNT_LOCAL0x00001000) == 0)
149 warnx("%s is not a local file system",
150 *argv);
151 else if (!selected(mntbuf[mntsize].f_fstypename))
152 warnx("%s mounted as a %s file system",
153 *argv, mntbuf[mntsize].f_fstypename);
154 else
155 ++mntsize;
156 else
157 warn("%s", *argv);
158 }
159 }
160
161 if (mntsize
12.1
'mntsize' is not equal to 0
) {
13
Taking true branch
162 maxwidth = 11;
163 for (i = 0; i < mntsize; i++) {
14
Assuming 'i' is < 'mntsize'
15
Loop condition is true. Entering loop body
18
Assuming 'i' is >= 'mntsize'
19
Loop condition is false. Execution continues on line 169
164 width = strlen(mntbuf[i].f_mntfromname);
165 if (width > maxwidth)
16
Assuming 'width' is <= 'maxwidth'
17
Taking false branch
166 maxwidth = width;
167 }
168
169 if (Pflag
19.1
'Pflag' is 0
)
20
Taking false branch
170 posixprint(mntbuf, mntsize, maxwidth);
171 else
172 bsdprint(mntbuf, mntsize, maxwidth);
21
Calling 'bsdprint'
173 }
174
175 return (mntsize ? 0 : 1);
176}
177
178char *
179getmntpt(char *name)
180{
181 long mntsize, i;
182 struct statfs *mntbuf;
183
184 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT2);
185 for (i = 0; i < mntsize; i++) {
186 if (!strcmp(mntbuf[i].f_mntfromname, name))
187 return (mntbuf[i].f_mntonname);
188 }
189 return (0);
190}
191
192static enum { IN_LIST, NOT_IN_LIST } which;
193
194static int
195selected(const char *type)
196{
197 char **av;
198
199 /* If no type specified, it's always selected. */
200 if (typelist == NULL((void *)0))
201 return (1);
202 for (av = typelist; *av != NULL((void *)0); ++av)
203 if (!strncmp(type, *av, MFSNAMELEN16))
204 return (which == IN_LIST ? 1 : 0);
205 return (which == IN_LIST ? 0 : 1);
206}
207
208static void
209maketypelist(char *fslist)
210{
211 int i;
212 char *nextcp, **av;
213
214 if ((fslist == NULL((void *)0)) || (fslist[0] == '\0'))
215 errx(1, "empty type list");
216
217 /*
218 * XXX
219 * Note: the syntax is "noxxx,yyy" for no xxx's and
220 * no yyy's, not the more intuitive "noyyy,noyyy".
221 */
222 if (fslist[0] == 'n' && fslist[1] == 'o') {
223 fslist += 2;
224 which = NOT_IN_LIST;
225 } else
226 which = IN_LIST;
227
228 /* Count the number of types. */
229 for (i = 1, nextcp = fslist; (nextcp = strchr(nextcp, ',')) != NULL((void *)0); i++)
230 ++nextcp;
231
232 /* Build an array of that many types. */
233 if ((av = typelist = calloc(i + 1, sizeof(char *))) == NULL((void *)0))
234 err(1, NULL((void *)0));
235 av[0] = fslist;
236 for (i = 1, nextcp = fslist; (nextcp = strchr(nextcp, ',')) != NULL((void *)0); i++) {
237 *nextcp = '\0';
238 av[i] = ++nextcp;
239 }
240 /* Terminate the array. */
241 av[i] = NULL((void *)0);
242}
243
244/*
245 * Make a pass over the filesystem info in ``mntbuf'' filtering out
246 * filesystem types not in ``fsmask'' and possibly re-stating to get
247 * current (not cached) info. Returns the new count of valid statfs bufs.
248 */
249static long
250regetmntinfo(struct statfs **mntbufp, long mntsize)
251{
252 int i, j;
253 struct statfs *mntbuf;
254
255 if (!lflag && typelist == NULL((void *)0))
256 return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT1));
257
258 mntbuf = *mntbufp;
259 j = 0;
260 for (i = 0; i < mntsize; i++) {
261 if (lflag && (mntbuf[i].f_flags & MNT_LOCAL0x00001000) == 0)
262 continue;
263 if (!selected(mntbuf[i].f_fstypename))
264 continue;
265 if (nflag)
266 mntbuf[j] = mntbuf[i];
267 else
268 (void)statfs(mntbuf[i].f_mntonname, &mntbuf[j]);
269 j++;
270 }
271 return (j);
272}
273
274/*
275 * "human-readable" output: use 3 digits max.--put unit suffixes at
276 * the end. Makes output compact and easy-to-read esp. on huge disks.
277 * Code moved into libutil; this is now just a wrapper.
278 */
279static void
280prthumanval(long long bytes)
281{
282 char ret[FMT_SCALED_STRSIZE7];
283
284 if (fmt_scaled(bytes, ret) == -1) {
285 (void)printf(" %lld", bytes);
286 return;
287 }
288 (void)printf(" %7s", ret);
289}
290
291static void
292prthuman(struct statfs *sfsp, unsigned long long used)
293{
294 prthumanval(sfsp->f_blocks * sfsp->f_bsize);
295 prthumanval(used * sfsp->f_bsize);
296 prthumanval(sfsp->f_bavail * sfsp->f_bsize);
297}
298
299/*
300 * Convert statfs returned filesystem size into BLOCKSIZE units.
301 * Attempts to avoid overflow for large filesystems.
302 */
303#define fsbtoblk(num, fsbs, bs)(((fsbs) != 0 && (fsbs) < (bs)) ? (num) / ((bs) / (
fsbs)) : (num) * ((fsbs) / (bs)))
\
304 (((fsbs) != 0 && (fsbs) < (bs)) ? \
305 (num) / ((bs) / (fsbs)) : (num) * ((fsbs) / (bs)))
306
307/*
308 * Print out status about a filesystem.
309 */
310static void
311prtstat(struct statfs *sfsp, int maxwidth, int headerlen, int blocksize)
312{
313 u_int64_t used, inodes;
314 int64_t availblks;
315
316 (void)printf("%-*.*s", maxwidth, maxwidth, sfsp->f_mntfromname);
317 used = sfsp->f_blocks - sfsp->f_bfree;
318 availblks = sfsp->f_bavail + used;
319 if (hflag)
320 prthuman(sfsp, used);
321 else
322 (void)printf(" %*llu %9llu %9lld", headerlen,
323 fsbtoblk(sfsp->f_blocks, sfsp->f_bsize, blocksize)(((sfsp->f_bsize) != 0 && (sfsp->f_bsize) < (
blocksize)) ? (sfsp->f_blocks) / ((blocksize) / (sfsp->
f_bsize)) : (sfsp->f_blocks) * ((sfsp->f_bsize) / (blocksize
)))
,
324 fsbtoblk(used, sfsp->f_bsize, blocksize)(((sfsp->f_bsize) != 0 && (sfsp->f_bsize) < (
blocksize)) ? (used) / ((blocksize) / (sfsp->f_bsize)) : (
used) * ((sfsp->f_bsize) / (blocksize)))
,
325 fsbtoblk(sfsp->f_bavail, sfsp->f_bsize, blocksize)(((sfsp->f_bsize) != 0 && (sfsp->f_bsize) < (
blocksize)) ? (sfsp->f_bavail) / ((blocksize) / (sfsp->
f_bsize)) : (sfsp->f_bavail) * ((sfsp->f_bsize) / (blocksize
)))
);
326 (void)printf(" %5.0f%%",
327 availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0);
328 if (iflag) {
329 inodes = sfsp->f_files;
330 used = inodes - sfsp->f_ffree;
331 (void)printf(" %7llu %7llu %5.0f%% ", used, sfsp->f_ffree,
332 inodes == 0 ? 100.0 : (double)used / (double)inodes * 100.0);
333 } else
334 (void)printf(" ");
335 (void)printf(" %s\n", sfsp->f_mntonname);
336}
337
338/*
339 * Print in traditional BSD format.
340 */
341static void
342bsdprint(struct statfs *mntbuf, long mntsize, int maxwidth)
343{
344 int i;
345 char *header;
346 int headerlen;
347 long blocksize;
22
'blocksize' declared without an initial value
348
349 /* Print the header line */
350 if (hflag
22.1
'hflag' is not equal to 0
) {
23
Taking true branch
351 header = " Size";
352 headerlen = strlen(header);
353 (void)printf("%-*.*s %s Used Avail Capacity",
354 maxwidth, maxwidth, "Filesystem", header);
355 } else {
356 if (kflag) {
357 blocksize = 1024;
358 header = "1K-blocks";
359 headerlen = strlen(header);
360 } else
361 header = getbsize(&headerlen, &blocksize);
362 (void)printf("%-*.*s %s Used Avail Capacity",
363 maxwidth, maxwidth, "Filesystem", header);
364 }
365 if (iflag
23.1
'iflag' is 0
)
24
Taking false branch
366 (void)printf(" iused ifree %%iused");
367 (void)printf(" Mounted on\n");
368
369
370 for (i = 0; i < mntsize; i++)
25
Loop condition is true. Entering loop body
371 prtstat(&mntbuf[i], maxwidth, headerlen, blocksize);
26
4th function call argument is an uninitialized value
372 return;
373}
374
375/*
376 * Print in format defined by POSIX 1002.2, invoke with -P option.
377 */
378static void
379posixprint(struct statfs *mntbuf, long mntsize, int maxwidth)
380{
381 int i;
382 int blocksize;
383 char *blockstr;
384 struct statfs *sfsp;
385 long long used, avail;
386 double percentused;
387
388 if (kflag) {
389 blocksize = 1024;
390 blockstr = "1024-blocks";
391 } else {
392 blocksize = 512;
393 blockstr = " 512-blocks";
394 }
395
396 (void)printf(
397 "%-*.*s %s Used Available Capacity Mounted on\n",
398 maxwidth, maxwidth, "Filesystem", blockstr);
399
400 for (i = 0; i < mntsize; i++) {
401 sfsp = &mntbuf[i];
402 used = sfsp->f_blocks - sfsp->f_bfree;
403 avail = sfsp->f_bavail + used;
404 if (avail == 0)
405 percentused = 100.0;
406 else
407 percentused = (double)used / (double)avail * 100.0;
408
409 (void) printf ("%-*.*s %*lld %10lld %11lld %5.0f%% %s\n",
410 maxwidth, maxwidth, sfsp->f_mntfromname,
411 (int)strlen(blockstr),
412 fsbtoblk(sfsp->f_blocks, sfsp->f_bsize, blocksize)(((sfsp->f_bsize) != 0 && (sfsp->f_bsize) < (
blocksize)) ? (sfsp->f_blocks) / ((blocksize) / (sfsp->
f_bsize)) : (sfsp->f_blocks) * ((sfsp->f_bsize) / (blocksize
)))
,
413 fsbtoblk(used, sfsp->f_bsize, blocksize)(((sfsp->f_bsize) != 0 && (sfsp->f_bsize) < (
blocksize)) ? (used) / ((blocksize) / (sfsp->f_bsize)) : (
used) * ((sfsp->f_bsize) / (blocksize)))
,
414 fsbtoblk(sfsp->f_bavail, sfsp->f_bsize, blocksize)(((sfsp->f_bsize) != 0 && (sfsp->f_bsize) < (
blocksize)) ? (sfsp->f_bavail) / ((blocksize) / (sfsp->
f_bsize)) : (sfsp->f_bavail) * ((sfsp->f_bsize) / (blocksize
)))
,
415 percentused, sfsp->f_mntonname);
416 }
417}
418
419static int
420raw_df(char *file, struct statfs *sfsp)
421{
422 int rfd, ret = -1;
423
424 if ((rfd = open(file, O_RDONLY0x0000)) == -1) {
425 warn("%s", file);
426 return (-1);
427 }
428
429 if (ffs_df(rfd, file, sfsp) == 0) {
430 ret = 0;
431 } else if (e2fs_df(rfd, file, sfsp) == 0) {
432 ret = 0;
433 }
434
435 close (rfd);
436 return (ret);
437}
438
439int
440bread(int rfd, off_t off, void *buf, int cnt)
441{
442 int nr;
443
444 if ((nr = pread(rfd, buf, cnt, off)) != cnt) {
445 /* Probably a dismounted disk if errno == EIO. */
446 if (errno(*__errno()) != EIO5)
447 (void)fprintf(stderr(&__sF[2]), "\ndf: %lld: %s\n",
448 (long long)off, strerror(nr > 0 ? EIO5 : errno(*__errno())));
449 return (0);
450 }
451 return (1);
452}
453
454static __dead__attribute__((__noreturn__)) void
455usage(void)
456{
457 (void)fprintf(stderr(&__sF[2]),
458 "usage: %s [-hiklnP] [-t type] [[file | file_system] ...]\n",
459 getprogname());
460 exit(1);
461}