summaryrefslogtreecommitdiff
path: root/pkg/util-linux/patch/0005-Avoid-statement-expressions-min-max.patch
blob: 0174a1d94110135fd990ea97af8f5ad31c731564 (plain)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
From ad5d729e363236dbdcf15b82d8575bd5d9db77ea Mon Sep 17 00:00:00 2001
From: Michael Forney <mforney@mforney.org>
Date: Tue, 12 Mar 2019 17:13:45 -0700
Subject: [PATCH] Avoid statement expressions min/max

---
 include/c.h                        | 24 ++++++++++--------------
 include/strutils.h                 |  3 ++-
 lib/mbsalign.c                     |  2 +-
 libblkid/src/superblocks/befs.c    |  6 +++---
 libblkid/src/superblocks/exfat.c   |  2 +-
 libblkid/src/superblocks/iso9660.c |  4 ++--
 libblkid/src/superblocks/minix.c   |  4 ++--
 libfdisk/src/alignment.c           |  8 ++++----
 libfdisk/src/context.c             |  4 ++--
 libfdisk/src/gpt.c                 |  4 ++--
 libsmartcols/src/calculate.c       |  8 ++++----
 libsmartcols/src/column.c          |  2 +-
 12 files changed, 34 insertions(+), 37 deletions(-)

diff --git a/include/c.h b/include/c.h
index b420b5187..fe82a8847 100644
--- a/include/c.h
+++ b/include/c.h
@@ -141,21 +141,17 @@
 # define FALSE 0
 #endif
 
-#ifndef min
-# define min(x, y) __extension__ ({		\
-	__typeof__(x) _min1 = (x);		\
-	__typeof__(y) _min2 = (y);		\
-	(void) (&_min1 == &_min2);		\
-	_min1 < _min2 ? _min1 : _min2; })
-#endif
+static inline uintmax_t
+umax(uintmax_t x, uintmax_t y)
+{
+	return x > y ? x : y;
+}
 
-#ifndef max
-# define max(x, y) __extension__ ({		\
-	__typeof__(x) _max1 = (x);		\
-	__typeof__(y) _max2 = (y);		\
-	(void) (&_max1 == &_max2);		\
-	_max1 > _max2 ? _max1 : _max2; })
-#endif
+static inline uintmax_t
+umin(uintmax_t x, uintmax_t y)
+{
+	return x < y ? x : y;
+}
 
 #ifndef cmp_numbers
 # define cmp_numbers(x, y) __extension__ ({	\
diff --git a/include/strutils.h b/include/strutils.h
index 7969a84d0..45c1768f0 100644
--- a/include/strutils.h
+++ b/include/strutils.h
@@ -70,7 +70,8 @@ static inline void xstrncpy(char *dest, const char *src, size_t n)
 
 	if (!len)
 		return;
-	len = min(len, n - 1);
+	if (n - 1 < len)
+		len = n - 1;
 	memcpy(dest, src, len);
 	dest[len] = 0;
 }
diff --git a/lib/mbsalign.c b/lib/mbsalign.c
index e251202af..f3ea1a2df 100644
--- a/lib/mbsalign.c
+++ b/lib/mbsalign.c
@@ -614,7 +614,7 @@ mbsalign_unibyte:
 
       dest = mbs_align_pad (dest, dest_end, start_spaces, padchar);
       space_left = dest_end - dest;
-      dest = mempcpy (dest, str_to_print, min (n_used_bytes, space_left));
+      dest = mempcpy (dest, str_to_print, umin (n_used_bytes, space_left));
       mbs_align_pad (dest, dest_end, end_spaces, padchar);
     }
 #ifdef HAVE_WIDECHAR
diff --git a/libblkid/src/superblocks/befs.c b/libblkid/src/superblocks/befs.c
index 211feae9f..35c2b19c4 100644
--- a/libblkid/src/superblocks/befs.c
+++ b/libblkid/src/superblocks/befs.c
@@ -257,7 +257,7 @@ static int32_t compare_keys(const char keys1[], uint16_t keylengths1[],
 
 	key1 = &keys1[keystart1];
 
-	result = strncmp(key1, key2, min(keylength1, keylength2));
+	result = strncmp(key1, key2, umin(keylength1, keylength2));
 
 	if (result == 0)
 		return keylength1 - keylength2;
@@ -386,8 +386,8 @@ static int get_uuid(blkid_probe pr, const struct befs_super_block *bs,
 
 	bi_size = (uint64_t)FS16_TO_CPU(bs->root_dir.len, fs_le) <<
 		FS32_TO_CPU(bs->block_shift, fs_le);
-	sd_total_size = min(bi_size - sizeof(struct befs_inode),
-			    (uint64_t)FS32_TO_CPU(bi->inode_size, fs_le));
+	sd_total_size = umin(bi_size - sizeof(struct befs_inode),
+			     (uint64_t)FS32_TO_CPU(bi->inode_size, fs_le));
 
 	offset = 0;
 
diff --git a/libblkid/src/superblocks/exfat.c b/libblkid/src/superblocks/exfat.c
index f586ec786..3c0448027 100644
--- a/libblkid/src/superblocks/exfat.c
+++ b/libblkid/src/superblocks/exfat.c
@@ -126,7 +126,7 @@ static int probe_exfat(blkid_probe pr, const struct blkid_idmag *mag)
 	label = find_label(pr, sb);
 	if (label)
 		blkid_probe_set_utf8label(pr, label->name,
-				min((size_t) label->length * 2, sizeof(label->name)),
+				umin((size_t) label->length * 2, sizeof(label->name)),
 				UL_ENCODE_UTF16LE);
 	else if (errno)
 		return -errno;
diff --git a/libblkid/src/superblocks/iso9660.c b/libblkid/src/superblocks/iso9660.c
index 289a325c8..ac4cc274f 100644
--- a/libblkid/src/superblocks/iso9660.c
+++ b/libblkid/src/superblocks/iso9660.c
@@ -228,8 +228,8 @@ static int probe_iso9660(blkid_probe pr, const struct blkid_idmag *mag)
 		unsigned char *desc =
 			blkid_probe_get_buffer(pr,
 					off,
-					max(sizeof(struct boot_record),
-					    sizeof(struct iso_volume_descriptor)));
+					umax(sizeof(struct boot_record),
+					     sizeof(struct iso_volume_descriptor)));
 
 		if (desc == NULL || desc[0] == ISO_VD_END)
 			break;
diff --git a/libblkid/src/superblocks/minix.c b/libblkid/src/superblocks/minix.c
index b521efb2b..d1fb64f64 100644
--- a/libblkid/src/superblocks/minix.c
+++ b/libblkid/src/superblocks/minix.c
@@ -83,8 +83,8 @@ static int probe_minix(blkid_probe pr,
 	unsigned block_size;
 
 	data = blkid_probe_get_buffer(pr, 1024,
-			max(sizeof(struct minix_super_block),
-			    sizeof(struct minix3_super_block)));
+			umax(sizeof(struct minix_super_block),
+			     sizeof(struct minix3_super_block)));
 	if (!data)
 		return errno ? -errno : 1;
 	version = get_minix_version(data, &swabme);
diff --git a/libfdisk/src/alignment.c b/libfdisk/src/alignment.c
index 3ae721913..a1a05f48a 100644
--- a/libfdisk/src/alignment.c
+++ b/libfdisk/src/alignment.c
@@ -38,7 +38,7 @@
  */
 static int lba_is_aligned(struct fdisk_context *cxt, uintmax_t lba)
 {
-	unsigned long granularity = max(cxt->phy_sector_size, cxt->min_io_size);
+	unsigned long granularity = umax(cxt->phy_sector_size, cxt->min_io_size);
 	uintmax_t offset;
 
 	if (cxt->grain > granularity)
@@ -54,7 +54,7 @@ static int lba_is_aligned(struct fdisk_context *cxt, uintmax_t lba)
  */
 static int lba_is_phy_aligned(struct fdisk_context *cxt, fdisk_sector_t lba)
 {
-	unsigned long granularity = max(cxt->phy_sector_size, cxt->min_io_size);
+	unsigned long granularity = umax(cxt->phy_sector_size, cxt->min_io_size);
 	uintmax_t offset = (lba * cxt->sector_size) % granularity;
 
 	return !((granularity + cxt->alignment_offset - offset) % granularity);
@@ -103,7 +103,7 @@ fdisk_sector_t fdisk_align_lba(struct fdisk_context *cxt, fdisk_sector_t lba, in
 			 * according the offset to be on the physical boundary.
 			 */
 			/* fprintf(stderr, "LBA: %llu apply alignment_offset\n", res); */
-			res -= (max(cxt->phy_sector_size, cxt->min_io_size) -
+			res -= (umax(cxt->phy_sector_size, cxt->min_io_size) -
 					cxt->alignment_offset) / cxt->sector_size;
 
 			if (direction == FDISK_ALIGN_UP && res < lba)
@@ -398,7 +398,7 @@ int fdisk_apply_user_device_properties(struct fdisk_context *cxt)
 	fdisk_reset_alignment(cxt);
 
 	if (cxt->user_grain) {
-		unsigned long granularity = max(cxt->phy_sector_size, cxt->min_io_size);
+		unsigned long granularity = umax(cxt->phy_sector_size, cxt->min_io_size);
 
 		cxt->grain = cxt->user_grain < granularity ? granularity : cxt->user_grain;
 		DBG(CXT, ul_debugobj(cxt, "new grain: %lu", cxt->grain));
diff --git a/libfdisk/src/context.c b/libfdisk/src/context.c
index 083b255d3..a3bfff993 100644
--- a/libfdisk/src/context.c
+++ b/libfdisk/src/context.c
@@ -919,7 +919,7 @@ int fdisk_reread_changes(struct fdisk_context *cxt, struct fdisk_table *org)
 	/* the current layout */
 	fdisk_get_partitions(cxt, &tb);
 	/* maximal number of partitions */
-	nparts = max(fdisk_table_get_nents(tb), fdisk_table_get_nents(org));
+	nparts = umax(fdisk_table_get_nents(tb), fdisk_table_get_nents(org));
 
 	while (fdisk_diff_tables(org, tb, &itr, &pa, &change) == 0) {
 		if (change == FDISK_DIFF_UNCHANGED)
@@ -976,7 +976,7 @@ int fdisk_reread_changes(struct fdisk_context *cxt, struct fdisk_table *org)
 			/* Let's follow the Linux kernel and reduce
                          * DOS extended partition to 1 or 2 sectors.
 			 */
-			sz = min(sz, (uint64_t) 2);
+			sz = umin(sz, (uint64_t) 2);
 
 		if (partx_add_partition(cxt->dev_fd, pa->partno + 1,
 					pa->start * ssf, sz) != 0) {
diff --git a/libfdisk/src/gpt.c b/libfdisk/src/gpt.c
index 5fd9b4d73..97e23767d 100644
--- a/libfdisk/src/gpt.c
+++ b/libfdisk/src/gpt.c
@@ -504,7 +504,7 @@ static int gpt_mknew_pmbr(struct fdisk_context *cxt)
 	pmbr->partition_record[0].end_track    = 0xFF;
 	pmbr->partition_record[0].starting_lba = cpu_to_le32(1);
 	pmbr->partition_record[0].size_in_lba  =
-		cpu_to_le32((uint32_t) min( cxt->total_sectors - 1ULL, 0xFFFFFFFFULL) );
+		cpu_to_le32((uint32_t) umin(cxt->total_sectors - 1ULL, 0xFFFFFFFFULL));
 
 	return 0;
 }
@@ -956,7 +956,7 @@ static int valid_pmbr(struct fdisk_context *cxt)
 			/* Note that gpt_write_pmbr() overwrites PMBR, but we want to keep it valid already
 			 * in memory too to disable warnings when valid_pmbr() called next time */
 			pmbr->partition_record[part].size_in_lba  =
-				cpu_to_le32((uint32_t) min( cxt->total_sectors - 1ULL, 0xFFFFFFFFULL) );
+				cpu_to_le32((uint32_t) umin(cxt->total_sectors - 1ULL, 0xFFFFFFFFULL));
 			fdisk_label_set_changed(cxt->label, 1);
 		}
 	}
diff --git a/libsmartcols/src/calculate.c b/libsmartcols/src/calculate.c
index 974fc340c..0c11627a7 100644
--- a/libsmartcols/src/calculate.c
+++ b/libsmartcols/src/calculate.c
@@ -57,7 +57,7 @@ static int count_cell_width(struct libscols_table *tb,
 
 	if (len == (size_t) -1)		/* ignore broken multibyte strings */
 		len = 0;
-	cl->width_max = max(len, cl->width_max);
+	cl->width_max = umax(len, cl->width_max);
 
 	if (cl->is_extreme && cl->width_avg && len > cl->width_avg * 2)
 		return 0;
@@ -66,10 +66,10 @@ static int count_cell_width(struct libscols_table *tb,
 		cl->extreme_sum += len;
 		cl->extreme_count++;
 	}
-	cl->width = max(len, cl->width);
+	cl->width = umax(len, cl->width);
 	if (scols_column_is_tree(cl)) {
 		size_t treewidth = buffer_get_safe_art_size(buf);
-		cl->width_treeart = max(cl->width_treeart, treewidth);
+		cl->width_treeart = umax(cl->width_treeart, treewidth);
 	}
 	return 0;
 }
@@ -115,7 +115,7 @@ static int count_column_width(struct libscols_table *tb,
 		if (data) {
 			size_t len = scols_table_is_noencoding(tb) ?
 					mbs_width(data) : mbs_safe_width(data);
-			cl->width_min = max(cl->width_min, len);
+			cl->width_min = umax(cl->width_min, len);
 		} else
 			no_header = 1;
 
diff --git a/libsmartcols/src/column.c b/libsmartcols/src/column.c
index c11df69f5..1285d42e6 100644
--- a/libsmartcols/src/column.c
+++ b/libsmartcols/src/column.c
@@ -347,7 +347,7 @@ size_t scols_wrapnl_chunksize(const struct libscols_column *cl __attribute__((un
 					mbs_width(data) :
 					mbs_safe_width(data);
 		}
-		sum = max(sum, sz);
+		sum = umax(sum, sz);
 		data = p;
 	}
 
-- 
2.37.3