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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= <thomas@t-8ch.de>
Date: Thu, 4 Apr 2024 07:24:58 +0200
Subject: libblkid: topology/ioctl: simplify ioctl handling
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit
Coverity complains about the data copy within the union.
Instead unroll the loop which is less code and easier to follow.
Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
(cherry picked from commit 2ab95860db0b7423906911b8b9e9f231332c2c11)
---
libblkid/src/topology/ioctl.c | 85 ++++++++++++++-----------------------------
1 file changed, 27 insertions(+), 58 deletions(-)
diff --git a/libblkid/src/topology/ioctl.c b/libblkid/src/topology/ioctl.c
index 4be20e8..7b15c9e 100644
--- a/libblkid/src/topology/ioctl.c
+++ b/libblkid/src/topology/ioctl.c
@@ -18,69 +18,38 @@
#include "topology.h"
-/*
- * ioctl topology values
- */
-static const struct topology_val {
-
- long ioc;
- size_t kernel_size;
-
- /* functions to set probing result */
- int (*set_ulong)(blkid_probe, unsigned long);
- int (*set_int)(blkid_probe, int);
- int (*set_u64)(blkid_probe, uint64_t);
-
-} topology_vals[] = {
- { BLKALIGNOFF, sizeof(int),
- .set_int = blkid_topology_set_alignment_offset },
- { BLKIOMIN, sizeof(int),
- .set_ulong = blkid_topology_set_minimum_io_size },
- { BLKIOOPT, sizeof(int),
- .set_ulong = blkid_topology_set_optimal_io_size },
- { BLKPBSZGET, sizeof(int),
- .set_ulong = blkid_topology_set_physical_sector_size },
- { BLKGETDISKSEQ, sizeof(uint64_t),
- .set_u64 = blkid_topology_set_diskseq },
- /* we read BLKSSZGET in topology.c */
-};
-
static int probe_ioctl_tp(blkid_probe pr,
const struct blkid_idmag *mag __attribute__((__unused__)))
{
- size_t i;
-
- for (i = 0; i < ARRAY_SIZE(topology_vals); i++) {
- const struct topology_val *val = &topology_vals[i];
- int rc = 1;
- union {
- int s32;
- uint64_t u64;
- } data = { 0 };
-
- if (ioctl(pr->fd, val->ioc, &data) == -1)
- goto nothing;
-
- /* Convert from kernel to libblkid type */
- if (val->kernel_size == 4)
- data.u64 = data.s32;
-
- if (val->set_int)
- rc = val->set_int(pr, data.u64);
- else if (val->set_ulong)
- rc = val->set_ulong(pr, data.u64);
- else
- rc = val->set_u64(pr, data.u64);
-
- if (rc)
- goto err;
- }
+ uint64_t u64;
+ int s32;
+
+ if (ioctl(pr->fd, BLKALIGNOFF, &s32) == -1)
+ return 1;
+ if (blkid_topology_set_alignment_offset(pr, s32))
+ return -1;
+
+ if (ioctl(pr->fd, BLKIOMIN, &s32) == -1)
+ return 1;
+ if (blkid_topology_set_minimum_io_size(pr, s32))
+ return -1;
+
+ if (ioctl(pr->fd, BLKIOOPT, &s32) == -1)
+ return 1;
+ if (blkid_topology_set_optimal_io_size(pr, s32))
+ return -1;
+
+ if (ioctl(pr->fd, BLKPBSZGET, &s32) == -1)
+ return 1;
+ if (blkid_topology_set_physical_sector_size(pr, s32))
+ return -1;
+
+ if (ioctl(pr->fd, BLKGETDISKSEQ, &u64) == -1)
+ return 1;
+ if (blkid_topology_set_physical_sector_size(pr, u64))
+ return -1;
return 0;
-nothing:
- return 1;
-err:
- return -1;
}
const struct blkid_idinfo ioctl_tp_idinfo =
|