1From 965ca8368f78249ab7e64e4b7686e6b0d7d77084 Mon Sep 17 00:00:00 2001
2From: Michael Forney <mforney@mforney.org>
3Date: Tue, 12 Mar 2019 17:13:45 -0700
4Subject: [PATCH] Avoid statement expressions min/max
5
6---
7 include/c.h | 24 ++++++++++--------------
8 include/strutils.h | 3 ++-
9 lib/mbsalign.c | 2 +-
10 lib/sysfs.c | 4 ++--
11 libblkid/src/probe.c | 4 ++--
12 libblkid/src/superblocks/befs.c | 6 +++---
13 libblkid/src/superblocks/exfat.c | 2 +-
14 libblkid/src/superblocks/iso9660.c | 4 ++--
15 libblkid/src/superblocks/minix.c | 4 ++--
16 libblkid/src/superblocks/romfs.c | 2 +-
17 libfdisk/src/alignment.c | 8 ++++----
18 libfdisk/src/context.c | 4 ++--
19 libfdisk/src/gpt.c | 4 ++--
20 libsmartcols/src/calculate.c | 6 +++---
21 libsmartcols/src/column.c | 7 ++++---
22 15 files changed, 41 insertions(+), 43 deletions(-)
23
24diff --git a/include/c.h b/include/c.h
25index 3ba42306c..1ba8c2beb 100644
26--- a/include/c.h
27+++ b/include/c.h
28@@ -145,21 +145,17 @@
29 # define FALSE 0
30 #endif
31
32-#ifndef min
33-# define min(x, y) __extension__ ({ \
34- __typeof__(x) _min1 = (x); \
35- __typeof__(y) _min2 = (y); \
36- (void) (&_min1 == &_min2); \
37- _min1 < _min2 ? _min1 : _min2; })
38-#endif
39+static inline uintmax_t
40+umax(uintmax_t x, uintmax_t y)
41+{
42+ return x > y ? x : y;
43+}
44
45-#ifndef max
46-# define max(x, y) __extension__ ({ \
47- __typeof__(x) _max1 = (x); \
48- __typeof__(y) _max2 = (y); \
49- (void) (&_max1 == &_max2); \
50- _max1 > _max2 ? _max1 : _max2; })
51-#endif
52+static inline uintmax_t
53+umin(uintmax_t x, uintmax_t y)
54+{
55+ return x < y ? x : y;
56+}
57
58 #ifndef abs_diff
59 # define abs_diff(x, y) __extension__ ({ \
60diff --git a/include/strutils.h b/include/strutils.h
61index a7177fc23..273aaedb8 100644
62--- a/include/strutils.h
63+++ b/include/strutils.h
64@@ -90,7 +90,8 @@ static inline int xstrncpy(char *dest, const char *src, size_t n)
65
66 if (!len)
67 return 0;
68- len = min(len, n - 1);
69+ if (n - 1 < len)
70+ len = n - 1;
71 memcpy(dest, src, len);
72 dest[len] = 0;
73 return len;
74diff --git a/lib/mbsalign.c b/lib/mbsalign.c
75index b4ab7becd..ea531119b 100644
76--- a/lib/mbsalign.c
77+++ b/lib/mbsalign.c
78@@ -629,7 +629,7 @@ mbsalign_unibyte:
79
80 dest = mbs_align_pad (dest, dest_end, start_spaces, padchar);
81 space_left = dest_end - dest;
82- dest = mempcpy (dest, str_to_print, min (n_used_bytes, space_left));
83+ dest = mempcpy (dest, str_to_print, umin (n_used_bytes, space_left));
84 mbs_align_pad (dest, dest_end, end_spaces, padchar);
85 }
86 #ifdef HAVE_WIDECHAR
87diff --git a/lib/sysfs.c b/lib/sysfs.c
88index c888617b7..7ecfdc9a8 100644
89--- a/lib/sysfs.c
90+++ b/lib/sysfs.c
91@@ -479,9 +479,9 @@ static int sysfs_devchain_is_removable(char *chain)
92 close(fd);
93
94 if (rc > 0) {
95- if (strncmp(buf, "fixed", min(rc, 5)) == 0) {
96+ if (strncmp(buf, "fixed", umin(rc, 5)) == 0) {
97 return 0;
98- } else if (strncmp(buf, "removable", min(rc, 9)) == 0) {
99+ } else if (strncmp(buf, "removable", umin(rc, 9)) == 0) {
100 return 1;
101 }
102 }
103diff --git a/libblkid/src/probe.c b/libblkid/src/probe.c
104index a38f5990e..8aea0cd5f 100644
105--- a/libblkid/src/probe.c
106+++ b/libblkid/src/probe.c
107@@ -1021,7 +1021,7 @@ static uint64_t blkid_get_io_size(int fd)
108 for (i = 0; i < ARRAY_SIZE(ioctls); i++) {
109 r = ioctl(fd, ioctls[i], &s);
110 if (r == 0 && is_power_of_2(s) && s >= DEFAULT_SECTOR_SIZE)
111- return min(s, 1U << 16);
112+ return umin(s, 1U << 16);
113 }
114
115 return DEFAULT_SECTOR_SIZE;
116@@ -2009,7 +2009,7 @@ static void blkid_probe_log_csum_mismatch(blkid_probe pr, size_t n, const void *
117 {
118 char csum_hex[256];
119 char expected_hex[sizeof(csum_hex)];
120- int hex_size = min(sizeof(csum_hex), n * 2);
121+ int hex_size = umin(sizeof(csum_hex), n * 2);
122
123 for (int i = 0; i < hex_size; i+=2) {
124 snprintf(&csum_hex[i], sizeof(csum_hex) - i, "%02X", ((const unsigned char *) csum)[i / 2]);
125diff --git a/libblkid/src/superblocks/befs.c b/libblkid/src/superblocks/befs.c
126index 5d082a949..322118601 100644
127--- a/libblkid/src/superblocks/befs.c
128+++ b/libblkid/src/superblocks/befs.c
129@@ -264,7 +264,7 @@ static int32_t compare_keys(const char keys1[], uint16_t keylengths1[],
130
131 key1 = &keys1[keystart1];
132
133- result = strncmp(key1, key2, min(keylength1, keylength2));
134+ result = strncmp(key1, key2, umin(keylength1, keylength2));
135
136 if (result == 0)
137 return keylength1 - keylength2;
138@@ -393,8 +393,8 @@ static int get_uuid(blkid_probe pr, const struct befs_super_block *bs,
139
140 bi_size = (uint64_t)FS16_TO_CPU(bs->root_dir.len, fs_le) <<
141 FS32_TO_CPU(bs->block_shift, fs_le);
142- sd_total_size = min(bi_size - sizeof(struct befs_inode),
143- (uint64_t)FS32_TO_CPU(bi->inode_size, fs_le));
144+ sd_total_size = umin(bi_size - sizeof(struct befs_inode),
145+ (uint64_t)FS32_TO_CPU(bi->inode_size, fs_le));
146
147 offset = 0;
148
149diff --git a/libblkid/src/superblocks/exfat.c b/libblkid/src/superblocks/exfat.c
150index a7d3e0298..a67e6f5e0 100644
151--- a/libblkid/src/superblocks/exfat.c
152+++ b/libblkid/src/superblocks/exfat.c
153@@ -259,7 +259,7 @@ static int probe_exfat(blkid_probe pr, const struct blkid_idmag *mag)
154 label = find_label(pr, sb);
155 if (label)
156 blkid_probe_set_utf8label(pr, label->name,
157- min((size_t) label->length * 2, sizeof(label->name)),
158+ umin((size_t) label->length * 2, sizeof(label->name)),
159 UL_ENCODE_UTF16LE);
160 else if (errno)
161 return -errno;
162diff --git a/libblkid/src/superblocks/iso9660.c b/libblkid/src/superblocks/iso9660.c
163index a7a364f30..6567dc519 100644
164--- a/libblkid/src/superblocks/iso9660.c
165+++ b/libblkid/src/superblocks/iso9660.c
166@@ -257,8 +257,8 @@ static int probe_iso9660(blkid_probe pr, const struct blkid_idmag *mag)
167 const unsigned char *desc =
168 blkid_probe_get_buffer(pr,
169 off + (is_hs ? 8 : 0), /* High Sierra has 8 bytes before descriptor with Volume Descriptor LBN value */
170- max(sizeof(struct boot_record),
171- sizeof(struct iso_volume_descriptor)));
172+ umax(sizeof(struct boot_record),
173+ sizeof(struct iso_volume_descriptor)));
174
175 if (desc == NULL || desc[0] == ISO_VD_END)
176 break;
177diff --git a/libblkid/src/superblocks/minix.c b/libblkid/src/superblocks/minix.c
178index dba299d23..b18bcf89e 100644
179--- a/libblkid/src/superblocks/minix.c
180+++ b/libblkid/src/superblocks/minix.c
181@@ -83,8 +83,8 @@ static int probe_minix(blkid_probe pr,
182 unsigned block_size;
183
184 data = blkid_probe_get_buffer(pr, 1024,
185- max(sizeof(struct minix_super_block),
186- sizeof(struct minix3_super_block)));
187+ umax(sizeof(struct minix_super_block),
188+ sizeof(struct minix3_super_block)));
189 if (!data)
190 return errno ? -errno : 1;
191 version = get_minix_version(data, &swabme);
192diff --git a/libblkid/src/superblocks/romfs.c b/libblkid/src/superblocks/romfs.c
193index 86c09adc3..ac2f0ba78 100644
194--- a/libblkid/src/superblocks/romfs.c
195+++ b/libblkid/src/superblocks/romfs.c
196@@ -27,7 +27,7 @@ struct romfs_super_block {
197 static int romfs_verify_csum(blkid_probe pr, const struct blkid_idmag *mag,
198 const struct romfs_super_block *ros)
199 {
200- uint32_t csummed_size = min((uint32_t) 512,
201+ uint32_t csummed_size = umin((uint32_t) 512,
202 be32_to_cpu(ros->ros_full_size));
203 const unsigned char *csummed;
204 uint32_t csum;
205diff --git a/libfdisk/src/alignment.c b/libfdisk/src/alignment.c
206index 2c7a00486..6fc1b9b9d 100644
207--- a/libfdisk/src/alignment.c
208+++ b/libfdisk/src/alignment.c
209@@ -38,7 +38,7 @@
210 */
211 static int lba_is_aligned(struct fdisk_context *cxt, uintmax_t lba)
212 {
213- unsigned long granularity = max(cxt->phy_sector_size, cxt->min_io_size);
214+ unsigned long granularity = umax(cxt->phy_sector_size, cxt->min_io_size);
215 uintmax_t offset;
216
217 if (cxt->grain > granularity)
218@@ -54,7 +54,7 @@ static int lba_is_aligned(struct fdisk_context *cxt, uintmax_t lba)
219 */
220 static int lba_is_phy_aligned(struct fdisk_context *cxt, fdisk_sector_t lba)
221 {
222- unsigned long granularity = max(cxt->phy_sector_size, cxt->min_io_size);
223+ unsigned long granularity = umax(cxt->phy_sector_size, cxt->min_io_size);
224 uintmax_t offset = (lba * cxt->sector_size) % granularity;
225
226 return !((granularity + cxt->alignment_offset - offset) % granularity);
227@@ -103,7 +103,7 @@ fdisk_sector_t fdisk_align_lba(struct fdisk_context *cxt, fdisk_sector_t lba, in
228 * according the offset to be on the physical boundary.
229 */
230 /* fprintf(stderr, "LBA: %llu apply alignment_offset\n", res); */
231- res -= (max(cxt->phy_sector_size, cxt->min_io_size) -
232+ res -= (umax(cxt->phy_sector_size, cxt->min_io_size) -
233 cxt->alignment_offset) / cxt->sector_size;
234
235 if (direction == FDISK_ALIGN_UP && res < lba)
236@@ -405,7 +405,7 @@ int fdisk_apply_user_device_properties(struct fdisk_context *cxt)
237 return rc;
238
239 if (cxt->user_grain) {
240- unsigned long granularity = max(cxt->phy_sector_size, cxt->min_io_size);
241+ unsigned long granularity = umax(cxt->phy_sector_size, cxt->min_io_size);
242
243 cxt->grain = cxt->user_grain < granularity ? granularity : cxt->user_grain;
244 DBG(CXT, ul_debugobj(cxt, "new grain: %lu", cxt->grain));
245diff --git a/libfdisk/src/context.c b/libfdisk/src/context.c
246index 3b2a4d25f..b1363bbd9 100644
247--- a/libfdisk/src/context.c
248+++ b/libfdisk/src/context.c
249@@ -920,7 +920,7 @@ int fdisk_reread_changes(struct fdisk_context *cxt, struct fdisk_table *org)
250 /* the current layout */
251 fdisk_get_partitions(cxt, &tb);
252 /* maximal number of partitions */
253- nparts = max(fdisk_table_get_nents(tb), fdisk_table_get_nents(org));
254+ nparts = umax(fdisk_table_get_nents(tb), fdisk_table_get_nents(org));
255
256 while (fdisk_diff_tables(org, tb, &itr, &pa, &change) == 0) {
257 if (change == FDISK_DIFF_UNCHANGED)
258@@ -977,7 +977,7 @@ int fdisk_reread_changes(struct fdisk_context *cxt, struct fdisk_table *org)
259 /* Let's follow the Linux kernel and reduce
260 * DOS extended partition to 1 or 2 sectors.
261 */
262- sz = min(sz, (uint64_t) 2);
263+ sz = umin(sz, (uint64_t) 2);
264
265 if (partx_add_partition(cxt->dev_fd, pa->partno + 1,
266 pa->start * ssf, sz) != 0) {
267diff --git a/libfdisk/src/gpt.c b/libfdisk/src/gpt.c
268index 85040fb6b..76a6ab65b 100644
269--- a/libfdisk/src/gpt.c
270+++ b/libfdisk/src/gpt.c
271@@ -506,7 +506,7 @@ static int gpt_mknew_pmbr(struct fdisk_context *cxt)
272 pmbr->partition_record[0].end_track = 0xFF;
273 pmbr->partition_record[0].starting_lba = cpu_to_le32(1);
274 pmbr->partition_record[0].size_in_lba =
275- cpu_to_le32((uint32_t) min( cxt->total_sectors - 1ULL, 0xFFFFFFFFULL) );
276+ cpu_to_le32((uint32_t) umin(cxt->total_sectors - 1ULL, 0xFFFFFFFFULL));
277
278 return 0;
279 }
280@@ -958,7 +958,7 @@ static int valid_pmbr(struct fdisk_context *cxt)
281 /* Note that gpt_write_pmbr() overwrites PMBR, but we want to keep it valid already
282 * in memory too to disable warnings when valid_pmbr() called next time */
283 pmbr->partition_record[part].size_in_lba =
284- cpu_to_le32((uint32_t) min( cxt->total_sectors - 1ULL, 0xFFFFFFFFULL) );
285+ cpu_to_le32((uint32_t) umin(cxt->total_sectors - 1ULL, 0xFFFFFFFFULL));
286 fdisk_label_set_changed(cxt->label, 1);
287 }
288 }
289diff --git a/libsmartcols/src/calculate.c b/libsmartcols/src/calculate.c
290index 77e843397..d0e892187 100644
291--- a/libsmartcols/src/calculate.c
292+++ b/libsmartcols/src/calculate.c
293@@ -66,11 +66,11 @@ static int count_cell_width(struct libscols_table *tb,
294
295 if (scols_column_is_tree(cl)) {
296 size_t treewidth = ul_buffer_get_safe_pointer_width(buf, SCOLS_BUFPTR_TREEEND);
297- cl->width_treeart = max(cl->width_treeart, treewidth);
298+ cl->width_treeart = umax(cl->width_treeart, treewidth);
299 }
300
301 ce->width = len;
302- cl->wstat.width_max = max(len, cl->wstat.width_max);
303+ cl->wstat.width_max = umax(len, cl->wstat.width_max);
304 done:
305 scols_table_reset_cursor(tb);
306 return rc;
307@@ -178,7 +178,7 @@ static int count_column_width(struct libscols_table *tb,
308 size_t len = scols_table_is_noencoding(tb) ?
309 mbs_width(data) : mbs_safe_width(data);
310
311- st->width_min = max(st->width_min, len);
312+ st->width_min = umax(st->width_min, len);
313 } else
314 no_header = 1;
315
316diff --git a/libsmartcols/src/column.c b/libsmartcols/src/column.c
317index e83d297fc..151adcfe0 100644
318--- a/libsmartcols/src/column.c
319+++ b/libsmartcols/src/column.c
320@@ -574,7 +574,7 @@ size_t scols_wrapnl_chunksize(const struct libscols_column *cl __attribute__((un
321 mbs_width(data) :
322 mbs_safe_width(data);
323 }
324- sum = max(sum, sz);
325+ sum = umax(sum, sz);
326 data = p;
327 }
328
329@@ -1068,9 +1068,10 @@ int scols_column_greatest_wrap(
330 while (scols_column_next_wrap(cl, ce, data) == 0) {
331 size_t sz = strlen(*data);
332
333- maxsz = max(maxsz, sz);
334- if (maxsz == sz)
335+ if (maxsz <= sz) {
336+ maxsz = sz;
337 res = *data;
338+ }
339 }
340
341 *data = res;
342--
3432.49.0
344