Bug Summary

File:src/usr.sbin/makefs/makefs.c
Warning:line 182, column 4
Called function pointer is null (null dereference)

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.4 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name makefs.c -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 -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -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.sbin/makefs/obj -resource-dir /usr/local/llvm16/lib/clang/16 -I /usr/src/usr.sbin/makefs -internal-isystem /usr/local/llvm16/lib/clang/16/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/usr.sbin/makefs/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fcf-protection=branch -fno-jump-tables -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/scan/2024-01-11-140451-98009-1 -x c /usr/src/usr.sbin/makefs/makefs.c
1/* $OpenBSD: makefs.c,v 1.22 2022/12/04 23:50:51 cheloha Exp $ */
2/* $NetBSD: makefs.c,v 1.53 2015/11/27 15:10:32 joerg Exp $ */
3
4/*
5 * Copyright (c) 2001-2003 Wasabi Systems, Inc.
6 * All rights reserved.
7 *
8 * Written by Luke Mewburn for Wasabi Systems, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed for the NetBSD Project by
21 * Wasabi Systems, Inc.
22 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
23 * or promote products derived from this software without specific prior
24 * written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#include <assert.h>
40#include <errno(*__errno()).h>
41#include <limits.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#include <unistd.h>
46#include <util.h>
47
48#include "makefs.h"
49
50/*
51 * list of supported file systems and dispatch functions
52 */
53typedef struct {
54 const char *type;
55 void (*prepare_options)(fsinfo_t *);
56 int (*parse_options)(const char *, fsinfo_t *);
57 void (*cleanup_options)(fsinfo_t *);
58 void (*make_fs)(const char *, const char *, fsnode *,
59 fsinfo_t *);
60} fstype_t;
61
62static fstype_t fstypes[] = {
63#define ENTRY(name){ "name", name_prep_opts, name_parse_opts, name_cleanup_opts,
name_makefs }
{ \
64 # name, name ## _prep_opts, name ## _parse_opts, \
65 name ## _cleanup_opts, name ## _makefs \
66}
67 ENTRY(ffs){ "ffs", ffs_prep_opts, ffs_parse_opts, ffs_cleanup_opts, ffs_makefs
}
,
68 ENTRY(cd9660){ "cd9660", cd9660_prep_opts, cd9660_parse_opts, cd9660_cleanup_opts
, cd9660_makefs }
,
69 ENTRY(msdos){ "msdos", msdos_prep_opts, msdos_parse_opts, msdos_cleanup_opts
, msdos_makefs }
,
70 { .type = NULL((void *)0) },
71};
72
73int Tflag;
74time_t stampts;
75struct timespec start_time;
76
77static fstype_t *get_fstype(const char *);
78static time_t get_tstamp(const char *);
79static long long strsuftoll(const char *, const char *, long long, long long);
80static __dead__attribute__((__noreturn__)) void usage(void);
81
82int
83main(int argc, char *argv[])
84{
85 fstype_t *fstype;
86 fsinfo_t fsoptions;
87 fsnode *root;
88 int ch, len;
89
90 if ((fstype = get_fstype(DEFAULT_FSTYPE"ffs")) == NULL((void *)0))
1
Taking false branch
91 errx(1, "Unknown default fs type `%s'.", DEFAULT_FSTYPE"ffs");
92
93 /* set default fsoptions */
94 (void)memset(&fsoptions, 0, sizeof(fsoptions));
95 fsoptions.fd = -1;
96 fsoptions.sectorsize = -1;
97
98 if (fstype->prepare_options)
2
Assuming field 'prepare_options' is null
3
Taking false branch
99 fstype->prepare_options(&fsoptions);
100
101 ch = clock_gettime(CLOCK_REALTIME0, &start_time);
102 if (ch == -1)
4
Assuming the condition is false
5
Taking false branch
103 err(1, "Unable to get system time");
104
105
106 while ((ch = getopt(argc, argv, "b:f:M:m:O:o:s:S:t:T:")) != -1) {
6
Assuming the condition is true
7
Loop condition is true. Entering loop body
107 switch (ch) {
8
Control jumps to 'case 116:' at line 175
108 case 'b':
109 len = strlen(optarg) - 1;
110 if (optarg[len] == '%') {
111 optarg[len] = '\0';
112 fsoptions.freeblockpc =
113 strsuftoll("free block percentage",
114 optarg, 0, 99);
115 } else {
116 fsoptions.freeblocks =
117 strsuftoll("free blocks",
118 optarg, 0, LLONG_MAX0x7fffffffffffffffLL);
119 }
120 break;
121
122 case 'f':
123 len = strlen(optarg) - 1;
124 if (optarg[len] == '%') {
125 optarg[len] = '\0';
126 fsoptions.freefilepc =
127 strsuftoll("free file percentage",
128 optarg, 0, 99);
129 } else {
130 fsoptions.freefiles =
131 strsuftoll("free files",
132 optarg, 0, LLONG_MAX0x7fffffffffffffffLL);
133 }
134 break;
135
136 case 'M':
137 fsoptions.minsize =
138 strsuftoll("minimum size", optarg, 1LL, LLONG_MAX0x7fffffffffffffffLL);
139 break;
140
141 case 'm':
142 fsoptions.maxsize =
143 strsuftoll("maximum size", optarg, 1LL, LLONG_MAX0x7fffffffffffffffLL);
144 break;
145
146 case 'O':
147 fsoptions.offset =
148 strsuftoll("offset", optarg, 0LL, LLONG_MAX0x7fffffffffffffffLL);
149 break;
150
151 case 'o':
152 {
153 char *p;
154
155 while ((p = strsep(&optarg, ",")) != NULL((void *)0)) {
156 if (*p == '\0')
157 errx(1, "Empty option");
158 if (! fstype->parse_options(p, &fsoptions))
159 usage();
160 }
161 break;
162 }
163
164 case 's':
165 fsoptions.minsize = fsoptions.maxsize =
166 strsuftoll("size", optarg, 1LL, LLONG_MAX0x7fffffffffffffffLL);
167 break;
168
169 case 'S':
170 fsoptions.sectorsize =
171 (int)strsuftoll("sector size", optarg,
172 1LL, INT_MAX0x7fffffff);
173 break;
174
175 case 't':
176 /* Check current one and cleanup if necessary. */
177 if (fstype->cleanup_options)
9
Assuming field 'cleanup_options' is null
10
Taking false branch
178 fstype->cleanup_options(&fsoptions);
179 fsoptions.fs_specific = NULL((void *)0);
180 if ((fstype = get_fstype(optarg)) == NULL((void *)0))
11
Taking false branch
181 errx(1, "Unknown fs type `%s'.", optarg);
182 fstype->prepare_options(&fsoptions);
12
Called function pointer is null (null dereference)
183 break;
184
185 case 'T':
186 Tflag = 1;
187 stampts = get_tstamp(optarg);
188 break;
189
190 default:
191 usage();
192 }
193 }
194 argc -= optind;
195 argv += optind;
196
197 if (argc != 2)
198 usage();
199
200 if (unveil(argv[0], "rwc") == -1)
201 err(1, "unveil %s", argv[0]);
202 if (unveil(argv[1], "rw") == -1)
203 err(1, "unveil %s", argv[1]);
204 if (pledge("stdio rpath wpath cpath", NULL((void *)0)) == -1)
205 err(1, "pledge");
206
207 /* walk the tree */
208 root = walk_dir(argv[1], ".", NULL((void *)0), NULL((void *)0));
209
210 /* build the file system */
211 fstype->make_fs(argv[0], argv[1], root, &fsoptions);
212
213 free_fsnodes(root);
214
215 exit(0);
216}
217
218int
219set_option(const option_t *options, const char *option, char *buf, size_t len)
220{
221 char *var, *val;
222 int retval;
223
224 assert(option != NULL)((option != ((void *)0)) ? (void)0 : __assert2("/usr/src/usr.sbin/makefs/makefs.c"
, 224, __func__, "option != NULL"))
;
225
226 var = estrdup(option);
227 for (val = var; *val; val++)
228 if (*val == '=') {
229 *val++ = '\0';
230 break;
231 }
232 retval = set_option_var(options, var, val, buf, len);
233 free(var);
234 return retval;
235}
236
237int
238set_option_var(const option_t *options, const char *var, const char *val,
239 char *buf, size_t len)
240{
241 char *s;
242 size_t i;
243
244#define NUM(type)if (!*val) { *(type *)options[i].value = 1; break; } *(type *
)options[i].value = (type)strsuftoll(options[i].name, val, options
[i].minimum, options[i].maximum); break
\
245 if (!*val) { \
246 *(type *)options[i].value = 1; \
247 break; \
248 } \
249 *(type *)options[i].value = (type)strsuftoll(options[i].name, val, \
250 options[i].minimum, options[i].maximum); break
251
252 for (i = 0; options[i].name != NULL((void *)0); i++) {
253 if (strcmp(options[i].name, var) != 0)
254 continue;
255 switch (options[i].type) {
256 case OPT_BOOL:
257 *(int *)options[i].value = 1;
258 break;
259 case OPT_STRARRAY:
260 strlcpy((void *)options[i].value, val, (size_t)
261 options[i].maximum);
262 break;
263 case OPT_STRPTR:
264 s = estrdup(val);
265 *(char **)options[i].value = s;
266 break;
267 case OPT_STRBUF:
268 if (buf == NULL((void *)0))
269 abort();
270 strlcpy(buf, val, len);
271 break;
272 case OPT_INT64:
273 NUM(uint64_t)if (!*val) { *(uint64_t *)options[i].value = 1; break; } *(uint64_t
*)options[i].value = (uint64_t)strsuftoll(options[i].name, val
, options[i].minimum, options[i].maximum); break
;
274 case OPT_INT32:
275 NUM(uint32_t)if (!*val) { *(uint32_t *)options[i].value = 1; break; } *(uint32_t
*)options[i].value = (uint32_t)strsuftoll(options[i].name, val
, options[i].minimum, options[i].maximum); break
;
276 case OPT_INT16:
277 NUM(uint16_t)if (!*val) { *(uint16_t *)options[i].value = 1; break; } *(uint16_t
*)options[i].value = (uint16_t)strsuftoll(options[i].name, val
, options[i].minimum, options[i].maximum); break
;
278 case OPT_INT8:
279 NUM(uint8_t)if (!*val) { *(uint8_t *)options[i].value = 1; break; } *(uint8_t
*)options[i].value = (uint8_t)strsuftoll(options[i].name, val
, options[i].minimum, options[i].maximum); break
;
280 default:
281 warnx("Unknown type %d in option %s", options[i].type,
282 val);
283 return 0;
284 }
285 return i;
286 }
287 warnx("Unknown option `%s'", var);
288 return -1;
289}
290
291
292static fstype_t *
293get_fstype(const char *type)
294{
295 int i;
296
297 for (i = 0; fstypes[i].type != NULL((void *)0); i++)
298 if (strcmp(fstypes[i].type, type) == 0)
299 return (&fstypes[i]);
300 return (NULL((void *)0));
301}
302
303option_t *
304copy_opts(const option_t *o)
305{
306 size_t i;
307 for (i = 0; o[i].name; i++)
308 continue;
309 i++;
310 return memcpy(ecalloc(i, sizeof(*o)), o, i * sizeof(*o));
311}
312
313static time_t
314get_tstamp(const char *b)
315{
316 time_t when;
317 char *eb;
318
319 errno(*__errno()) = 0;
320 when = strtoll(b, &eb, 0);
321 if (b == eb || *eb || errno(*__errno())) {
322 errx(1, "Cannot get timestamp from `%s'",
323 optarg);
324 }
325 return when;
326}
327
328/* XXX */
329static long long
330strsuftoll(const char *desc, const char *val, long long min, long long max)
331{
332 long long res;
333
334 if (scan_scaled((char *)val, &res) == -1)
335 err(1, "%s", desc);
336 if (res < min || res > max)
337 errc(1, ERANGE34, "%s", desc);
338 return res;
339}
340
341static void
342usage(void)
343{
344 extern char *__progname;
345
346 fprintf(stderr(&__sF[2]),
347"usage: %s [-b free-blocks] [-f free-files] [-M minimum-size]\n"
348"\t[-m maximum-size] [-O offset] [-o fs-options] [-S sector-size]\n"
349"\t[-s image-size] [-T timestamp] [-t fs-type] image-file directory\n",
350 __progname);
351
352 exit(1);
353}