• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 18607725477

17 Oct 2025 07:30PM UTC coverage: 72.189% (-0.2%) from 72.363%
18607725477

push

github

web-flow
Assorted coverity fixes (#39355)

5 of 6 new or added lines in 4 files covered. (83.33%)

858 existing lines in 51 files now uncovered.

303873 of 420941 relevant lines covered (72.19%)

1152650.64 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

57.73
/src/shared/blockdev-list.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include "sd-device.h"
4

5
#include "alloc-util.h"
6
#include "ansi-color.h"
7
#include "blockdev-list.h"
8
#include "blockdev-util.h"
9
#include "device-private.h"
10
#include "device-util.h"
11
#include "strv.h"
12
#include "terminal-util.h"
13

14
void block_device_done(BlockDevice *d) {
×
15
        assert(d);
×
16

17
        d->node = mfree(d->node);
×
18
        d->symlinks = strv_free(d->symlinks);
×
19
 }
×
20

21
void block_device_array_free(BlockDevice *d, size_t n_devices) {
×
22

23
        FOREACH_ARRAY(i, d, n_devices)
×
24
                block_device_done(d);
×
25

26
        free(d);
×
27
}
×
28

29
int blockdev_list(BlockDevListFlags flags, BlockDevice **ret_devices, size_t *ret_n_devices) {
6✔
30
        _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
6✔
31
        int r;
6✔
32

33
        assert(!!ret_devices == !!ret_n_devices);
6✔
34

35
        /* If ret_devices/ret_n_devices are passed, returns a list of matching block devices, otherwise
36
         * prints the list to stdout */
37

38
        BlockDevice *l = NULL;
6✔
39
        size_t n = 0;
6✔
40
        CLEANUP_ARRAY(l, n, block_device_array_free);
6✔
41

42
        dev_t root_devno = 0;
6✔
43
        if (FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_ROOT))
6✔
44
                if (blockdev_get_root(LOG_DEBUG, &root_devno) > 0) {
2✔
45
                        r = block_get_whole_disk(root_devno, &root_devno);
2✔
46
                        if (r < 0)
2✔
47
                                log_debug_errno(r, "Failed to get whole block device of root device: %m");
×
48
                }
49

50
        if (sd_device_enumerator_new(&e) < 0)
6✔
51
                return log_oom();
×
52

53
        r = sd_device_enumerator_add_match_subsystem(e, "block", /* match = */ true);
6✔
54
        if (r < 0)
6✔
55
                return log_error_errno(r, "Failed to add subsystem match: %m");
×
56

57
        if (FLAGS_SET(flags, BLOCKDEV_LIST_REQUIRE_LUKS)) {
6✔
58
                r = sd_device_enumerator_add_match_property(e, "ID_FS_TYPE", "crypto_LUKS");
1✔
59
                if (r < 0)
1✔
60
                        return log_error_errno(r, "Failed to add match for LUKS block devices: %m");
×
61
        }
62

63
        FOREACH_DEVICE(e, dev) {
30✔
64
                const char *node;
24✔
65

66
                r = sd_device_get_devname(dev, &node);
24✔
67
                if (r < 0) {
24✔
68
                        log_warning_errno(r, "Failed to get device node of discovered block device, ignoring: %m");
×
69
                        continue;
17✔
70
                }
71

72
                if (FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_ROOT) && root_devno != 0) {
24✔
73
                        dev_t devno;
11✔
74

75
                        r = sd_device_get_devnum(dev, &devno);
11✔
76
                        if (r < 0) {
11✔
77
                                log_warning_errno(r, "Failed to get major/minor of discovered block device, ignoring: %m");
×
78
                                continue;
2✔
79
                        }
80

81
                        if (devno == root_devno)
11✔
82
                                continue;
2✔
83
                }
84

85
                if (FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_ZRAM)) {
22✔
86
                        r = device_sysname_startswith(dev, "zram");
21✔
87
                        if (r < 0) {
21✔
88
                                log_warning_errno(r, "Failed to check device name of discovered block device '%s', ignoring: %m", node);
×
89
                                continue;
×
90
                        }
91
                        if (r > 0)
21✔
92
                                continue;
×
93
                }
94

95
                if (FLAGS_SET(flags, BLOCKDEV_LIST_REQUIRE_PARTITION_SCANNING)) {
22✔
96
                        r = blockdev_partscan_enabled(dev);
17✔
97
                        if (r < 0) {
17✔
98
                                log_warning_errno(r, "Unable to determine whether '%s' supports partition scanning, skipping device: %m", node);
×
99
                                continue;
×
100
                        }
101
                        if (r == 0) {
17✔
102
                                log_debug("Device '%s' does not support partition scanning, skipping.", node);
15✔
103
                                continue;
15✔
104
                        }
105
                }
106

107
                uint64_t size = UINT64_MAX;
7✔
108
                if (FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_EMPTY) || ret_devices) {
7✔
109

110
                        r = device_get_sysattr_u64(dev, "size", &size);
×
111
                        if (r < 0)
×
112
                                log_debug_errno(r, "Failed to acquire size of device '%s', ignoring: %m", node);
×
113
                        else
114
                                /* the 'size' sysattr is always in multiples of 512, even on 4K sector block devices! */
NEW
115
                                assert_se(MUL_ASSIGN_SAFE(&size, 512)); /* Overflow check for coverity */
×
116

117
                        if (size == 0 && FLAGS_SET(flags, BLOCKDEV_LIST_IGNORE_EMPTY)) {
×
118
                                log_debug("Device '%s' has a zero size, assuming drive without a medium, skipping.", node);
×
119
                                continue;
×
120
                        }
121
                }
122

123
                _cleanup_strv_free_ char **list = NULL;
7✔
124
                if (FLAGS_SET(flags, BLOCKDEV_LIST_SHOW_SYMLINKS)) {
7✔
125
                        FOREACH_DEVICE_DEVLINK(dev, sl)
67✔
126
                                if (strv_extend(&list, sl) < 0)
60✔
127
                                        return log_oom();
×
128

129
                        strv_sort(list);
7✔
130
                }
131

132
                if (ret_devices) {
7✔
133
                        uint64_t diskseq = UINT64_MAX;
×
134
                        r = sd_device_get_diskseq(dev, &diskseq);
×
135
                        if (r < 0)
×
136
                                log_debug_errno(r, "Failed to acquire diskseq of device '%s', ignoring: %m", node);
×
137

138
                        if (!GREEDY_REALLOC(l, n+1))
×
139
                                return log_oom();
×
140

141
                        _cleanup_free_ char *m = strdup(node);
×
142
                        if (!m)
×
143
                                return log_oom();
×
144

145
                        l[n++] = (BlockDevice) {
×
146
                                .node = TAKE_PTR(m),
×
147
                                .symlinks = TAKE_PTR(list),
×
148
                                .diskseq = diskseq,
149
                                .size = size,
150
                        };
151

152
                } else {
153
                        printf("%s\n", node);
7✔
154

155
                        if (FLAGS_SET(flags, BLOCKDEV_LIST_SHOW_SYMLINKS))
7✔
156
                                STRV_FOREACH(i, list)
67✔
157
                                        printf("%s%s%s%s\n", on_tty() ? "    " : "", ansi_grey(), *i, ansi_normal());
180✔
158
                }
159
        }
160

161
        if (ret_devices)
6✔
162
                *ret_devices = TAKE_PTR(l);
2✔
163
        if (ret_n_devices)
6✔
164
                *ret_n_devices = n;
2✔
165

166
        return 0;
167
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc