Bug Summary

File:build-scan/../src/machine-id-setup/machine-id-setup-main.c
Warning:line 111, column 34
Null pointer passed to 1st parameter expecting 'nonnull'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name machine-id-setup-main.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 static -mframe-pointer=all -relaxed-aliasing -menable-no-infs -menable-no-nans -menable-unsafe-fp-math -fno-signed-zeros -mreassociate -freciprocal-math -fdenormal-fp-math=preserve-sign,preserve-sign -ffp-contract=fast -fno-rounding-math -ffast-math -ffinite-math-only -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /usr/lib64/clang/12.0.0 -include config.h -I systemd-machine-id-setup.p -I . -I .. -I src/basic -I ../src/basic -I src/shared -I ../src/shared -I src/systemd -I ../src/systemd -I src/journal -I ../src/journal -I src/journal-remote -I ../src/journal-remote -I src/nspawn -I ../src/nspawn -I src/resolve -I ../src/resolve -I src/timesync -I ../src/timesync -I ../src/time-wait-sync -I src/login -I ../src/login -I src/udev -I ../src/udev -I src/libudev -I ../src/libudev -I src/core -I ../src/core -I ../src/libsystemd/sd-bus -I ../src/libsystemd/sd-device -I ../src/libsystemd/sd-hwdb -I ../src/libsystemd/sd-id128 -I ../src/libsystemd/sd-netlink -I ../src/libsystemd/sd-network -I src/libsystemd-network -I ../src/libsystemd-network -D _FILE_OFFSET_BITS=64 -internal-isystem /usr/local/include -internal-isystem /usr/lib64/clang/12.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wwrite-strings -Wno-unused-parameter -Wno-missing-field-initializers -Wno-unused-result -Wno-format-signedness -Wno-error=nonnull -std=gnu99 -fconst-strings -fdebug-compilation-dir /home/mrc0mmand/repos/@redhat-plumbers/systemd-rhel8/build-scan -ferror-limit 19 -fvisibility hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -analyzer-output=html -faddrsig -o /tmp/scan-build-2021-07-16-221226-1465241-1 -x c ../src/machine-id-setup/machine-id-setup-main.c
1/* SPDX-License-Identifier: LGPL-2.1+ */
2
3#include <errno(*__errno_location ()).h>
4#include <getopt.h>
5#include <stdio.h>
6#include <stdlib.h>
7
8#include "id128-util.h"
9#include "log.h"
10#include "machine-id-setup.h"
11#include "path-util.h"
12#include "util.h"
13
14static char *arg_root = NULL((void*)0);
15static bool_Bool arg_commit = false0;
16static bool_Bool arg_print = false0;
17
18static void help(void) {
19 printf("%s [OPTIONS...]\n\n"
20 "Initialize /etc/machine-id from a random source.\n\n"
21 " -h --help Show this help\n"
22 " --version Show package version\n"
23 " --root=ROOT Filesystem root\n"
24 " --commit Commit transient ID\n"
25 " --print Print used machine ID\n"
26 , program_invocation_short_name);
27}
28
29static int parse_argv(int argc, char *argv[]) {
30
31 enum {
32 ARG_VERSION = 0x100,
33 ARG_ROOT,
34 ARG_COMMIT,
35 ARG_PRINT,
36 };
37
38 static const struct option options[] = {
39 { "help", no_argument0, NULL((void*)0), 'h' },
40 { "version", no_argument0, NULL((void*)0), ARG_VERSION },
41 { "root", required_argument1, NULL((void*)0), ARG_ROOT },
42 { "commit", no_argument0, NULL((void*)0), ARG_COMMIT },
43 { "print", no_argument0, NULL((void*)0), ARG_PRINT },
44 {}
45 };
46
47 int c, r;
48
49 assert(argc >= 0)do { if ((__builtin_expect(!!(!(argc >= 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("argc >= 0"), "../src/machine-id-setup/machine-id-setup-main.c"
, 49, __PRETTY_FUNCTION__); } while (0)
;
2
Taking false branch
3
Loop condition is false. Exiting loop
50 assert(argv)do { if ((__builtin_expect(!!(!(argv)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("argv"), "../src/machine-id-setup/machine-id-setup-main.c"
, 50, __PRETTY_FUNCTION__); } while (0)
;
4
Assuming 'argv' is non-null
5
Taking false branch
6
Loop condition is false. Exiting loop
51
52 while ((c = getopt_long(argc, argv, "hqcv", options, NULL((void*)0))) >= 0)
7
Assuming the condition is true
8
Loop condition is true. Entering loop body
12
Assuming the condition is false
13
Loop condition is false. Execution continues on line 84
53
54 switch (c) {
9
Control jumps to 'case ARG_COMMIT:' at line 69
55
56 case 'h':
57 help();
58 return 0;
59
60 case ARG_VERSION:
61 return version();
62
63 case ARG_ROOT:
64 r = parse_path_argument_and_warn(optarg, true1, &arg_root);
65 if (r < 0)
66 return r;
67 break;
68
69 case ARG_COMMIT:
70 arg_commit = true1;
10
The value 1 is assigned to 'arg_commit', which participates in a condition later
71 break;
11
Execution continues on line 52
72
73 case ARG_PRINT:
74 arg_print = true1;
75 break;
76
77 case '?':
78 return -EINVAL22;
79
80 default:
81 assert_not_reached("Unhandled option")do { log_assert_failed_unreachable_realm(LOG_REALM_SYSTEMD, (
"Unhandled option"), "../src/machine-id-setup/machine-id-setup-main.c"
, 81, __PRETTY_FUNCTION__); } while (0)
;
82 }
83
84 if (optind < argc) {
14
Assuming 'optind' is >= 'argc'
15
Taking false branch
85 log_error("Extraneous arguments")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/machine-id-setup/machine-id-setup-main.c", 85, __func__
, "Extraneous arguments") : -abs(_e); })
;
86 return -EINVAL22;
87 }
88
89 return 1;
90}
91
92int main(int argc, char *argv[]) {
93 char buf[SD_ID128_STRING_MAX33];
94 sd_id128_t id;
95 int r;
96
97 log_parse_environment()log_parse_environment_realm(LOG_REALM_SYSTEMD);
98 log_open();
99
100 r = parse_argv(argc, argv);
1
Calling 'parse_argv'
16
Returning from 'parse_argv'
101 if (r
16.1
'r' is > 0
<= 0)
17
Taking false branch
102 goto finish;
103
104 if (arg_commit
17.1
'arg_commit' is true
) {
18
Taking true branch
105 const char *etc_machine_id;
106
107 r = machine_id_commit(arg_root);
108 if (r < 0)
19
Assuming 'r' is >= 0
20
Taking false branch
109 goto finish;
110
111 etc_machine_id = prefix_roota(arg_root, "/etc/machine-id")({ const char* _path = ("/etc/machine-id"), *_root = (arg_root
), *_ret; char *_p, *_n; size_t _l; while (_path[0] == '/' &&
_path[1] == '/') _path ++; if (empty_or_root(_root)) _ret = _path
; else { _l = strlen(_root) + 1 + strlen(_path) + 1; _n = __builtin_alloca
(_l); _p = stpcpy(_n, _root); while (_p > _n && _p
[-1] == '/') _p--; if (_path[0] != '/') *(_p++) = '/'; strcpy
(_p, _path); _ret = _n; } _ret; })
;
21
'_root' initialized to a null pointer value
22
Loop condition is false. Execution continues on line 111
23
Assuming the condition is false
24
Taking false branch
25
Null pointer passed to 1st parameter expecting 'nonnull'
112 r = id128_read(etc_machine_id, ID128_PLAIN, &id);
113 if (r < 0) {
114 log_error_errno(r, "Failed to read machine ID back: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/machine-id-setup/machine-id-setup-main.c", 114, __func__
, "Failed to read machine ID back: %m") : -abs(_e); })
;
115 goto finish;
116 }
117 } else {
118 r = machine_id_setup(arg_root, SD_ID128_NULL((const sd_id128_t) { .qwords = { 0, 0 }}), &id);
119 if (r < 0)
120 goto finish;
121 }
122
123 if (arg_print)
124 puts(sd_id128_to_string(id, buf));
125
126finish:
127 free(arg_root);
128 return r < 0 ? EXIT_FAILURE1 : EXIT_SUCCESS0;
129}