Bug Summary

File:build-scan/../src/basic/lockfile-util.c
Warning:line 78, 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 lockfile-util.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 2 -fhalf-no-semantic-interposition -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 src/basic/libbasic.a.p -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 -I . -I .. -I /usr/include/blkid -I /usr/include/libmount -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 default -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/basic/lockfile-util.c
1/* SPDX-License-Identifier: LGPL-2.1+ */
2
3#include <errno(*__errno_location ()).h>
4#include <fcntl.h>
5#include <stdio.h>
6#include <string.h>
7#include <sys/file.h>
8#include <sys/stat.h>
9
10#include "alloc-util.h"
11#include "fd-util.h"
12#include "fs-util.h"
13#include "lockfile-util.h"
14#include "macro.h"
15#include "path-util.h"
16
17int make_lock_file(const char *p, int operation, LockFile *ret) {
18 _cleanup_close___attribute__((cleanup(closep))) int fd = -1;
19 _cleanup_free___attribute__((cleanup(freep))) char *t = NULL((void*)0);
20 int r;
21
22 /*
23 * We use UNPOSIX locks if they are available. They have nice
24 * semantics, and are mostly compatible with NFS. However,
25 * they are only available on new kernels. When we detect we
26 * are running on an older kernel, then we fall back to good
27 * old BSD locks. They also have nice semantics, but are
28 * slightly problematic on NFS, where they are upgraded to
29 * POSIX locks, even though locally they are orthogonal to
30 * POSIX locks.
31 */
32
33 t = strdup(p);
34 if (!t)
35 return -ENOMEM12;
36
37 for (;;) {
38 struct flock fl = {
39 .l_type = (operation & ~LOCK_NB4) == LOCK_EX2 ? F_WRLCK1 : F_RDLCK0,
40 .l_whence = SEEK_SET0,
41 };
42 struct stat st;
43
44 fd = open(p, O_CREAT0100|O_RDWR02|O_NOFOLLOW0400000|O_CLOEXEC02000000|O_NOCTTY0400, 0600);
45 if (fd < 0)
46 return -errno(*__errno_location ());
47
48 r = fcntl(fd, (operation & LOCK_NB4) ? F_OFD_SETLK37 : F_OFD_SETLKW38, &fl);
49 if (r < 0) {
50
51 /* If the kernel is too old, use good old BSD locks */
52 if (errno(*__errno_location ()) == EINVAL22)
53 r = flock(fd, operation);
54
55 if (r < 0)
56 return errno(*__errno_location ()) == EAGAIN11 ? -EBUSY16 : -errno(*__errno_location ());
57 }
58
59 /* If we acquired the lock, let's check if the file
60 * still exists in the file system. If not, then the
61 * previous exclusive owner removed it and then closed
62 * it. In such a case our acquired lock is worthless,
63 * hence try again. */
64
65 r = fstat(fd, &st);
66 if (r < 0)
67 return -errno(*__errno_location ());
68 if (st.st_nlink > 0)
69 break;
70
71 fd = safe_close(fd);
72 }
73
74 ret->path = t;
75 ret->fd = fd;
76 ret->operation = operation;
77
78 fd = -1;
Value stored to 'fd' is never read
79 t = NULL((void*)0);
80
81 return r;
82}
83
84int make_lock_file_for(const char *p, int operation, LockFile *ret) {
85 const char *fn;
86 char *t;
87
88 assert(p)do { if ((__builtin_expect(!!(!(p)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("p"), "../src/basic/lockfile-util.c", 88
, __PRETTY_FUNCTION__); } while (0)
;
89 assert(ret)do { if ((__builtin_expect(!!(!(ret)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("ret"), "../src/basic/lockfile-util.c", 89
, __PRETTY_FUNCTION__); } while (0)
;
90
91 fn = basename(p);
92 if (!filename_is_valid(fn))
93 return -EINVAL22;
94
95 t = newa(char, strlen(p) + 2 + 4 + 1)({ do { if ((__builtin_expect(!!(!(!size_multiply_overflow(sizeof
(char), strlen(p) + 2 + 4 + 1))),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("!size_multiply_overflow(sizeof(char), strlen(p) + 2 + 4 + 1)"
), "../src/basic/lockfile-util.c", 95, __PRETTY_FUNCTION__); }
while (0); (char*) __builtin_alloca (sizeof(char)*(strlen(p)
+ 2 + 4 + 1)); })
;
96 stpcpy(stpcpy(stpcpy(mempcpy(t, p, fn - p), ".#"), fn), ".lck");
97
98 return make_lock_file(t, operation, ret);
99}
100
101void release_lock_file(LockFile *f) {
102 int r;
103
104 if (!f)
105 return;
106
107 if (f->path) {
108
109 /* If we are the exclusive owner we can safely delete
110 * the lock file itself. If we are not the exclusive
111 * owner, we can try becoming it. */
112
113 if (f->fd >= 0 &&
114 (f->operation & ~LOCK_NB4) == LOCK_SH1) {
115 static const struct flock fl = {
116 .l_type = F_WRLCK1,
117 .l_whence = SEEK_SET0,
118 };
119
120 r = fcntl(f->fd, F_OFD_SETLK37, &fl);
121 if (r < 0 && errno(*__errno_location ()) == EINVAL22)
122 r = flock(f->fd, LOCK_EX2|LOCK_NB4);
123
124 if (r >= 0)
125 f->operation = LOCK_EX2|LOCK_NB4;
126 }
127
128 if ((f->operation & ~LOCK_NB4) == LOCK_EX2)
129 unlink_noerrno(f->path);
130
131 f->path = mfree(f->path);
132 }
133
134 f->fd = safe_close(f->fd);
135 f->operation = 0;
136}