Bug Summary

File:build-scan/../src/journal/cat.c
Warning:line 121, column 9
Value stored to 'fd' is never read

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 cat.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-cat.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/journal/cat.c
1/* SPDX-License-Identifier: LGPL-2.1+ */
2
3#include <errno(*__errno_location ()).h>
4#include <fcntl.h>
5#include <getopt.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <unistd.h>
9
10#include "sd-journal.h"
11
12#include "fd-util.h"
13#include "parse-util.h"
14#include "string-util.h"
15#include "syslog-util.h"
16#include "util.h"
17
18static const char *arg_identifier = NULL((void*)0);
19static int arg_priority = LOG_INFO6;
20static bool_Bool arg_level_prefix = true1;
21
22static void help(void) {
23 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
24 "Execute process with stdout/stderr connected to the journal.\n\n"
25 " -h --help Show this help\n"
26 " --version Show package version\n"
27 " -t --identifier=STRING Set syslog identifier\n"
28 " -p --priority=PRIORITY Set priority value (0..7)\n"
29 " --level-prefix=BOOL Control whether level prefix shall be parsed\n"
30 , program_invocation_short_name);
31}
32
33static int parse_argv(int argc, char *argv[]) {
34
35 enum {
36 ARG_VERSION = 0x100,
37 ARG_LEVEL_PREFIX
38 };
39
40 static const struct option options[] = {
41 { "help", no_argument0, NULL((void*)0), 'h' },
42 { "version", no_argument0, NULL((void*)0), ARG_VERSION },
43 { "identifier", required_argument1, NULL((void*)0), 't' },
44 { "priority", required_argument1, NULL((void*)0), 'p' },
45 { "level-prefix", required_argument1, NULL((void*)0), ARG_LEVEL_PREFIX },
46 {}
47 };
48
49 int c;
50
51 assert(argc >= 0)do { if ((__builtin_expect(!!(!(argc >= 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("argc >= 0"), "../src/journal/cat.c",
51, __PRETTY_FUNCTION__); } while (0)
;
52 assert(argv)do { if ((__builtin_expect(!!(!(argv)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("argv"), "../src/journal/cat.c", 52, __PRETTY_FUNCTION__
); } while (0)
;
53
54 while ((c = getopt_long(argc, argv, "+ht:p:", options, NULL((void*)0))) >= 0)
55
56 switch (c) {
57
58 case 'h':
59 help();
60 return 0;
61
62 case ARG_VERSION:
63 return version();
64
65 case 't':
66 if (isempty(optarg))
67 arg_identifier = NULL((void*)0);
68 else
69 arg_identifier = optarg;
70 break;
71
72 case 'p':
73 arg_priority = log_level_from_string(optarg);
74 if (arg_priority < 0) {
75 log_error("Failed to parse priority value.")({ 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/journal/cat.c", 75, __func__, "Failed to parse priority value."
) : -abs(_e); })
;
76 return -EINVAL22;
77 }
78 break;
79
80 case ARG_LEVEL_PREFIX: {
81 int k;
82
83 k = parse_boolean(optarg);
84 if (k < 0)
85 return log_error_errno(k, "Failed to parse level prefix value.")({ int _level = ((3)), _e = ((k)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/journal/cat.c", 85, __func__, "Failed to parse level prefix value."
) : -abs(_e); })
;
86
87 arg_level_prefix = k;
88 break;
89 }
90
91 case '?':
92 return -EINVAL22;
93
94 default:
95 assert_not_reached("Unhandled option")do { log_assert_failed_unreachable_realm(LOG_REALM_SYSTEMD, (
"Unhandled option"), "../src/journal/cat.c", 95, __PRETTY_FUNCTION__
); } while (0)
;
96 }
97
98 return 1;
99}
100
101int main(int argc, char *argv[]) {
102 _cleanup_close___attribute__((cleanup(closep))) int fd = -1, saved_stderr = -1;
103 int r;
104
105 log_parse_environment()log_parse_environment_realm(LOG_REALM_SYSTEMD);
106 log_open();
107
108 r = parse_argv(argc, argv);
109 if (r <= 0)
110 goto finish;
111
112 fd = sd_journal_stream_fd(arg_identifier, arg_priority, arg_level_prefix);
113 if (fd < 0) {
114 r = log_error_errno(fd, "Failed to create stream fd: %m")({ int _level = ((3)), _e = ((fd)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/journal/cat.c", 114, __func__, "Failed to create stream fd: %m"
) : -abs(_e); })
;
115 goto finish;
116 }
117
118 saved_stderr = fcntl(STDERR_FILENO2, F_DUPFD_CLOEXEC1030, 3);
119
120 r = rearrange_stdio(STDIN_FILENO0, fd, fd); /* Invalidates fd on succcess + error! */
121 fd = -1;
Value stored to 'fd' is never read
122 if (r < 0) {
123 log_error_errno(r, "Failed to rearrange stdout/stderr: %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/journal/cat.c", 123, __func__, "Failed to rearrange stdout/stderr: %m"
) : -abs(_e); })
;
124 goto finish;
125 }
126
127 if (argc <= optind)
128 (void) execl("/bin/cat", "/bin/cat", NULL((void*)0));
129 else
130 (void) execvp(argv[optind], argv + optind);
131 r = -errno(*__errno_location ());
132
133 /* Let's try to restore a working stderr, so we can print the error message */
134 if (saved_stderr >= 0)
135 (void) dup3(saved_stderr, STDERR_FILENO2, 0);
136
137 log_error_errno(r, "Failed to execute process: %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/journal/cat.c", 137, __func__, "Failed to execute process: %m"
) : -abs(_e); })
;
138
139finish:
140 return r < 0 ? EXIT_FAILURE1 : EXIT_SUCCESS0;
141}