summaryrefslogtreecommitdiff
path: root/pkg/util-linux/patch/0005-Avoid-statement-expressions-min-max.patch
blob: 6674de25ad6f2e357b4ebc713232482f772e2d32 (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
From 2cfdfe606f814c7672baede286c904c051b00e99 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 +-
 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 +-
 8 files changed, 26 insertions(+), 29 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/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.31.1