clang -cc1 -cc1 -triple amd64-unknown-openbsd7.4 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name files.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/config/obj -resource-dir /usr/local/llvm16/lib/clang/16 -I /usr/src/usr.sbin/config -I . -internal-isystem /usr/local/llvm16/lib/clang/16/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/usr.sbin/config/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/config/files.c
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | |
22 | |
23 | |
24 | |
25 | |
26 | |
27 | |
28 | |
29 | |
30 | |
31 | |
32 | |
33 | |
34 | |
35 | |
36 | |
37 | |
38 | |
39 | |
40 | |
41 | |
42 | |
43 | |
44 | #include <errno.h> |
45 | #include <stdio.h> |
46 | #include <stdlib.h> |
47 | #include <string.h> |
48 | |
49 | #include "config.h" |
50 | |
51 | extern const char *yyfile; |
52 | |
53 | |
54 | |
55 | |
56 | |
57 | |
58 | |
59 | static struct hashtab *basetab; |
60 | static struct hashtab *pathtab; |
61 | |
62 | static struct files **nextfile; |
63 | static struct files **unchecked; |
64 | |
65 | static struct objects **nextobject; |
66 | |
67 | static int checkaux(const char *, void *); |
68 | static int fixcount(const char *, void *); |
69 | static int fixfsel(const char *, void *); |
70 | static int fixsel(const char *, void *); |
71 | static int expr_eval(struct nvlist *, |
72 | int (*)(const char *, void *), void *); |
73 | static void expr_free(struct nvlist *); |
74 | |
75 | #ifdef DEBUG |
76 | static void pr0(); |
77 | #endif |
78 | |
79 | void |
80 | initfiles(void) |
81 | { |
82 | |
83 | basetab = ht_new(); |
84 | pathtab = ht_new(); |
85 | nextfile = &allfiles; |
86 | unchecked = &allfiles; |
87 | nextobject = &allobjects; |
88 | } |
89 | |
90 | void |
91 | addfile(struct nvlist *nvpath, struct nvlist *optx, int flags, const char *rule) |
92 | { |
93 | struct files *fi; |
94 | const char *dotp, *dotp1, *tail, *path, *tail1 = NULL; |
| 1 | 'path' declared without an initial value | |
|
95 | struct nvlist *nv; |
96 | size_t baselen; |
97 | int needc, needf; |
98 | char base[200]; |
99 | |
100 | |
101 | needc = flags & FI_NEEDSCOUNT; |
102 | needf = flags & FI_NEEDSFLAG; |
103 | if (needc && needf) { |
| |
104 | error("cannot mix needs-count and needs-flag"); |
105 | goto bad; |
106 | } |
107 | if (optx == NULL && (needc || needf)) { |
| 3 | | Assuming 'optx' is not equal to NULL | |
|
108 | error("nothing to %s", needc ? "count" : "flag"); |
109 | goto bad; |
110 | } |
111 | |
112 | for (nv = nvpath; nv; nv = nv->nv_next) { |
| 4 | | Loop condition is false. Execution continues on line 145 | |
|
113 | path = nv->nv_name; |
114 | |
115 | |
116 | tail = strrchr(path, '/'); |
117 | if (tail == NULL) |
118 | tail = path; |
119 | else |
120 | tail++; |
121 | dotp = strrchr(tail, '.'); |
122 | if (dotp == NULL || dotp[1] == 0 || |
123 | (baselen = dotp - tail) >= sizeof(base)) { |
124 | error("invalid pathname `%s'", path); |
125 | goto bad; |
126 | } |
127 | |
128 | |
129 | |
130 | |
131 | |
132 | if (tail1 && |
133 | (dotp - tail != dotp1 - tail1 || |
134 | strncmp(tail1, tail, dotp - tail))) |
135 | error("different production from %s %s", |
136 | nvpath->nv_name, tail); |
137 | tail1 = tail; |
138 | dotp1 = dotp; |
139 | } |
140 | |
141 | |
142 | |
143 | |
144 | |
145 | fi = emalloc(sizeof *fi); |
146 | if (ht_insert(pathtab, path, fi)) { |
| 5 | | 2nd function call argument is an uninitialized value |
|
147 | free(fi); |
148 | if ((fi = ht_lookup(pathtab, path)) == NULL) |
149 | panic("addfile: ht_lookup(%s)", path); |
150 | error("duplicate file %s", path); |
151 | xerror(fi->fi_srcfile, fi->fi_srcline, |
152 | "here is the original definition"); |
153 | } |
154 | memcpy(base, tail, baselen); |
155 | base[baselen] = 0; |
156 | fi->fi_next = NULL; |
157 | fi->fi_srcfile = yyfile; |
158 | fi->fi_srcline = currentline(); |
159 | fi->fi_flags = flags; |
160 | fi->fi_nvpath = nvpath; |
161 | fi->fi_base = intern(base); |
162 | fi->fi_optx = optx; |
163 | fi->fi_optf = NULL; |
164 | fi->fi_mkrule = rule; |
165 | *nextfile = fi; |
166 | nextfile = &fi->fi_next; |
167 | return; |
168 | bad: |
169 | expr_free(optx); |
170 | } |
171 | |
172 | void |
173 | addobject(const char *path, struct nvlist *optx, int flags) |
174 | { |
175 | struct objects *oi; |
176 | |
177 | |
178 | |
179 | |
180 | |
181 | oi = emalloc(sizeof *oi); |
182 | if (ht_insert(pathtab, path, oi)) { |
183 | free(oi); |
184 | if ((oi = ht_lookup(pathtab, path)) == NULL) |
185 | panic("addfile: ht_lookup(%s)", path); |
186 | error("duplicate file %s", path); |
187 | xerror(oi->oi_srcfile, oi->oi_srcline, |
188 | "here is the original definition"); |
189 | } |
190 | oi->oi_next = NULL; |
191 | oi->oi_srcfile = yyfile; |
192 | oi->oi_srcline = currentline(); |
193 | oi->oi_flags = flags; |
194 | oi->oi_path = path; |
195 | oi->oi_optx = optx; |
196 | oi->oi_optf = NULL; |
197 | *nextobject = oi; |
198 | nextobject = &oi->oi_next; |
199 | } |
200 | |
201 | |
202 | |
203 | |
204 | |
205 | |
206 | |
207 | void |
208 | checkfiles(void) |
209 | { |
210 | struct files *fi, *last; |
211 | |
212 | last = NULL; |
213 | for (fi = *unchecked; fi != NULL; last = fi, fi = fi->fi_next) |
214 | if ((fi->fi_flags & FI_NEEDSCOUNT) != 0) |
215 | (void)expr_eval(fi->fi_optx, checkaux, fi); |
216 | if (last != NULL) |
217 | unchecked = &last->fi_next; |
218 | } |
219 | |
220 | |
221 | |
222 | |
223 | |
224 | static int |
225 | checkaux(const char *name, void *context) |
226 | { |
227 | struct files *fi = context; |
228 | |
229 | if (ht_lookup(devbasetab, name) == NULL) { |
230 | xerror(fi->fi_srcfile, fi->fi_srcline, |
231 | "`%s' is not a countable device", |
232 | name); |
233 | |
234 | fi->fi_flags |= FI_HIDDEN; |
235 | } |
236 | return (0); |
237 | } |
238 | |
239 | |
240 | |
241 | |
242 | |
243 | |
244 | int |
245 | fixfiles(void) |
246 | { |
247 | struct files *fi, *ofi; |
248 | struct nvlist *flathead, **flatp; |
249 | int err, sel; |
250 | |
251 | err = 0; |
252 | for (fi = allfiles; fi != NULL; fi = fi->fi_next) { |
253 | |
254 | if (fi->fi_flags & FI_HIDDEN) |
255 | continue; |
256 | |
257 | |
258 | if (fi->fi_optx != NULL) { |
259 | flathead = NULL; |
260 | flatp = &flathead; |
261 | sel = expr_eval(fi->fi_optx, |
262 | fi->fi_flags & FI_NEEDSCOUNT ? fixcount : |
263 | fi->fi_flags & FI_NEEDSFLAG ? fixfsel : |
264 | fixsel, |
265 | &flatp); |
266 | fi->fi_optf = flathead; |
267 | if (!sel) |
268 | continue; |
269 | } |
270 | |
271 | |
272 | if (ht_insert(basetab, fi->fi_base, fi)) { |
273 | if ((ofi = ht_lookup(basetab, fi->fi_base)) == NULL) |
274 | panic("fixfiles ht_lookup(%s)", fi->fi_base); |
275 | |
276 | |
277 | |
278 | |
279 | if (fi->fi_nvpath != ofi->fi_nvpath) { |
280 | if (ht_replace(basetab, fi->fi_base, fi) != 1) |
281 | panic("fixfiles ht_replace(%s)", |
282 | fi->fi_base); |
283 | ofi->fi_flags &= ~FI_SEL; |
284 | ofi->fi_flags |= FI_HIDDEN; |
285 | } else { |
286 | xerror(fi->fi_srcfile, fi->fi_srcline, |
287 | "object file collision on %s.o, from %s", |
288 | fi->fi_base, fi->fi_nvpath->nv_name); |
289 | xerror(ofi->fi_srcfile, ofi->fi_srcline, |
290 | "here is the previous file: %s", |
291 | ofi->fi_nvpath->nv_name); |
292 | err = 1; |
293 | } |
294 | } |
295 | fi->fi_flags |= FI_SEL; |
296 | } |
297 | return (err); |
298 | } |
299 | |
300 | |
301 | |
302 | |
303 | |
304 | int |
305 | fixobjects(void) |
306 | { |
307 | struct objects *oi; |
308 | struct nvlist *flathead, **flatp; |
309 | int err, sel; |
310 | |
311 | err = 0; |
312 | for (oi = allobjects; oi != NULL; oi = oi->oi_next) { |
313 | |
314 | if (oi->oi_optx != NULL) { |
315 | flathead = NULL; |
316 | flatp = &flathead; |
317 | sel = expr_eval(oi->oi_optx, |
318 | oi->oi_flags & OI_NEEDSFLAG ? fixfsel : |
319 | fixsel, |
320 | &flatp); |
321 | oi->oi_optf = flathead; |
322 | if (!sel) |
323 | continue; |
324 | } |
325 | |
326 | oi->oi_flags |= OI_SEL; |
327 | } |
328 | return (err); |
329 | } |
330 | |
331 | |
332 | |
333 | |
334 | |
335 | |
336 | |
337 | |
338 | |
339 | |
340 | static int |
341 | fixcount(const char *name, void *context) |
342 | { |
343 | struct nvlist ***p = context; |
344 | struct devbase *dev; |
345 | struct nvlist *nv; |
346 | |
347 | dev = ht_lookup(devbasetab, name); |
348 | if (dev == NULL) |
349 | panic("fixcount(%s)", name); |
350 | nv = newnv(name, NULL, NULL, dev->d_umax, NULL); |
351 | **p = nv; |
352 | *p = &nv->nv_next; |
353 | (void)ht_insert(needcnttab, name, nv); |
354 | return (dev->d_umax != 0); |
355 | } |
356 | |
357 | |
358 | |
359 | |
360 | |
361 | static int |
362 | fixfsel(const char *name, void *context) |
363 | { |
364 | struct nvlist ***p = context; |
365 | struct nvlist *nv; |
366 | int sel; |
367 | |
368 | sel = ht_lookup(selecttab, name) != NULL; |
369 | nv = newnv(name, NULL, NULL, sel, NULL); |
370 | **p = nv; |
371 | *p = &nv->nv_next; |
372 | return (sel); |
373 | } |
374 | |
375 | |
376 | |
377 | |
378 | static int |
379 | fixsel(const char *name, void *context) |
380 | { |
381 | |
382 | return (ht_lookup(selecttab, name) != NULL); |
383 | } |
384 | |
385 | |
386 | |
387 | |
388 | |
389 | |
390 | |
391 | |
392 | |
393 | static int |
394 | expr_eval(struct nvlist *expr, int (*fn)(const char *, void *), void *context) |
395 | { |
396 | int lhs, rhs; |
397 | |
398 | switch (expr->nv_int) { |
399 | |
400 | case FX_ATOM: |
401 | return ((*fn)(expr->nv_name, context)); |
402 | |
403 | case FX_NOT: |
404 | return (!expr_eval(expr->nv_next, fn, context)); |
405 | |
406 | case FX_AND: |
407 | lhs = expr_eval(expr->nv_ptr, fn, context); |
408 | rhs = expr_eval(expr->nv_next, fn, context); |
409 | return (lhs & rhs); |
410 | |
411 | case FX_OR: |
412 | lhs = expr_eval(expr->nv_ptr, fn, context); |
413 | rhs = expr_eval(expr->nv_next, fn, context); |
414 | return (lhs | rhs); |
415 | } |
416 | panic("expr_eval %d", expr->nv_int); |
417 | return (0); |
418 | } |
419 | |
420 | |
421 | |
422 | |
423 | static void |
424 | expr_free(struct nvlist *expr) |
425 | { |
426 | struct nvlist *rhs; |
427 | |
428 | |
429 | for (; expr != NULL; expr = rhs) { |
430 | switch (expr->nv_int) { |
431 | |
432 | |
433 | case FX_ATOM: |
434 | case FX_NOT: |
435 | break; |
436 | |
437 | |
438 | case FX_AND: |
439 | case FX_OR: |
440 | expr_free(expr->nv_ptr); |
441 | break; |
442 | |
443 | default: |
444 | panic("expr_free %d", expr->nv_int); |
445 | } |
446 | rhs = expr->nv_next; |
447 | nvfree(expr); |
448 | } |
449 | } |
450 | |
451 | #ifdef DEBUG |
452 | |
453 | |
454 | |
455 | void |
456 | prexpr(struct nvlist *expr) |
457 | { |
458 | printf("expr ="); |
459 | pr0(expr); |
460 | printf("\n"); |
461 | (void)fflush(stdout); |
462 | } |
463 | |
464 | static void |
465 | pr0(struct nvlist *e) |
466 | { |
467 | |
468 | switch (e->nv_int) { |
469 | case FX_ATOM: |
470 | printf(" %s", e->nv_name); |
471 | return; |
472 | case FX_NOT: |
473 | printf(" (!"); |
474 | break; |
475 | case FX_AND: |
476 | printf(" (&"); |
477 | break; |
478 | case FX_OR: |
479 | printf(" (|"); |
480 | break; |
481 | default: |
482 | printf(" (?%d?", e->nv_int); |
483 | break; |
484 | } |
485 | if (e->nv_ptr) |
486 | pr0(e->nv_ptr); |
487 | pr0(e->nv_next); |
488 | printf(")"); |
489 | } |
490 | #endif |