chiark / gitweb /
Always use our own MAX/MIN definitions
authorCristian Rodríguez <crrodriguez@opensuse.org>
Mon, 1 Apr 2013 06:08:05 +0000 (03:08 -0300)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 2 Apr 2013 03:43:48 +0000 (23:43 -0400)
code in src/shared/macro.h only defined MAX/MIN in case
they were not defined previously. however the MAX/MIN
macros implemented in glibc are not of the "safe" kind but defined
as:

define MIN(a,b) (((a)<(b))?(a):(b))
define MAX(a,b) (((a)>(b))?(a):(b))

Avoid nasty side effects by using our own versions instead.

Also fix the warnings derived from this change.

[zj: - modify MAX3 macro to fix warning about _a shadowing _a,
     - do bootchart/svg.c too,
     - remove unused MIN3.]

src/bootchart/svg.c
src/journal/journal-file.c
src/libsystemd-bus/bus-socket.c
src/shared/macro.h
src/shared/prioq.c
src/shared/util.c
src/systemctl/systemctl.c

index 0fb9fffd9b834e9568900353107ed4f60b5415c7..a4086c52278d77250ccfa2caff1677c9a34486b1 100644 (file)
@@ -44,9 +44,6 @@
 #define kb_to_graph(m) ((m) * arg_scale_y * 0.0001)
 #define to_color(n) (192.0 - ((n) * 192.0))
 
-#define max(x, y) (((x) > (y)) ? (x) : (y))
-#define min(x, y) (((x) < (y)) ? (x) : (y))
-
 static char str[8092];
 
 #define svg(a...) do { snprintf(str, 8092, ## a); fputs(str, of); fflush(of); } while (0)
@@ -441,8 +438,8 @@ static void svg_io_bi_bar(void) {
                 int stop;
                 double tot;
 
-                start = max(i - ((range / 2) - 1), 0);
-                stop = min(i + (range / 2), samples - 1);
+                start = MAX(i - ((range / 2) - 1), 0);
+                stop = MIN(i + (range / 2), samples - 1);
 
                 tot = (double)(blockstat[stop].bi - blockstat[start].bi)
                       / (stop - start);
@@ -463,8 +460,8 @@ static void svg_io_bi_bar(void) {
                 double tot;
                 double pbi;
 
-                start = max(i - ((range / 2) - 1), 0);
-                stop = min(i + (range / 2), samples);
+                start = MAX(i - ((range / 2) - 1), 0);
+                stop = MIN(i + (range / 2), samples);
 
                 tot = (double)(blockstat[stop].bi - blockstat[start].bi)
                       / (stop - start);
@@ -517,8 +514,8 @@ static void svg_io_bo_bar(void) {
                 int stop;
                 double tot;
 
-                start = max(i - ((range / 2) - 1), 0);
-                stop = min(i + (range / 2), samples - 1);
+                start = MAX(i - ((range / 2) - 1), 0);
+                stop = MIN(i + (range / 2), samples - 1);
 
                 tot = (double)(blockstat[stop].bi - blockstat[start].bi)
                       / (stop - start);
@@ -539,8 +536,8 @@ static void svg_io_bo_bar(void) {
                 double tot;
                 double pbo;
 
-                start = max(i - ((range / 2) - 1), 0);
-                stop = min(i + (range / 2), samples);
+                start = MAX(i - ((range / 2) - 1), 0);
+                stop = MIN(i + (range / 2), samples);
 
                 tot = (double)(blockstat[stop].bo - blockstat[start].bo)
                       / (stop - start);
index 5b077be0da4748771fb36e83f2b96ede4954cb39..a44e126c0effbcc52fea2216fd3975d350e4648d 100644 (file)
@@ -1325,7 +1325,7 @@ int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const st
 #endif
 
         /* alloca() can't take 0, hence let's allocate at least one */
-        items = alloca(sizeof(EntryItem) * MAX(1, n_iovec));
+        items = alloca(sizeof(EntryItem) * MAX(1u, n_iovec));
 
         for (i = 0; i < n_iovec; i++) {
                 uint64_t p;
index 5b5a731233150a1380d52fb01b3a0f532f4004a4..c68c7bca682791625b4adf05a50833fb75ad0f49 100644 (file)
@@ -455,7 +455,7 @@ static int bus_socket_read_auth(sd_bus *b) {
         if (r != 0)
                 return r;
 
-        n = MAX(256, b->rbuffer_size * 2);
+        n = MAX(256u, b->rbuffer_size * 2);
 
         if (n > BUS_AUTH_SIZE_MAX)
                 n = BUS_AUTH_SIZE_MAX;
index 898784ac839945ff26803a0cfb13728fd401de06..4e5d0f4f2f8d304e10403b34a31378785c087e37 100644 (file)
@@ -71,29 +71,27 @@ static inline size_t ALIGN_TO(size_t l, size_t ali) {
         const typeof( ((type *)0)->member ) *__mptr = (ptr); \
         (type *)( (char *)__mptr - offsetof(type,member) );})
 
-#ifndef MAX
-#define MAX(a,b)                                \
-        __extension__ ({                        \
-                        typeof(a) _a = (a);     \
-                        typeof(b) _b = (b);     \
-                        _a > _b ? _a : _b;      \
+#undef MAX
+#define MAX(a,b)                                 \
+        __extension__ ({                         \
+                        typeof(a) _a = (a);      \
+                        typeof(b) _b = (b);      \
+                        _a > _b ? _a : _b;       \
                 })
-#endif
 
-#define MAX3(a,b,c)                             \
-        MAX(MAX(a,b),c)
+#define MAX3(x,y,z)                              \
+        __extension__ ({                         \
+                        typeof(x) _c = MAX(x,y); \
+                        MAX(_c, z);              \
+                })
 
-#ifndef MIN
+#undef MIN
 #define MIN(a,b)                                \
         __extension__ ({                        \
                         typeof(a) _a = (a);     \
                         typeof(b) _b = (b);     \
                         _a < _b ? _a : _b;      \
                 })
-#endif
-
-#define MIN3(a,b,c)                             \
-        MIN(MIN(a,b),c)
 
 #ifndef CLAMP
 #define CLAMP(x, low, high)                                             \
index 64c44aef8266bfef9bab49ea12cb397f6cb777f7..a2205719b47bf6dc1dfc01ccf52ec02a1af6438d 100644 (file)
@@ -159,7 +159,7 @@ int prioq_put(Prioq *q, void *data, unsigned *idx) {
                 unsigned n;
                 struct prioq_item *j;
 
-                n = MAX((q->n_items+1) * 2, 16);
+                n = MAX((q->n_items+1) * 2, 16u);
                 j = realloc(q->items, sizeof(struct prioq_item) * n);
                 if (!j)
                         return -ENOMEM;
index b516b9b0532e5163593487e7fc7f6a1e41aa5029..46c20bec9c1c43456ad707d9b87c197fbc1ec4bb 100644 (file)
@@ -5862,7 +5862,7 @@ void* greedy_realloc(void **p, size_t *allocated, size_t need) {
         if (*allocated >= need)
                 return *p;
 
-        a = MAX(64, need * 2);
+        a = MAX(64u, need * 2);
         q = realloc(*p, a);
         if (!q)
                 return NULL;
index 328b91bc35bd260bd5d0516371f0996f7d1308f9..6bd2e34f58f4aef0ef20217217844cff7b2044b0 100644 (file)
@@ -328,7 +328,7 @@ static void output_units_list(const struct unit_info *unit_infos, unsigned c) {
 
         if (!arg_full) {
                 unsigned basic_len;
-                id_len = MIN(max_id_len, 25);
+                id_len = MIN(max_id_len, 25u);
                 basic_len = 5 + id_len + 5 + active_len + sub_len;
                 if (job_count)
                         basic_len += job_len + 1;
@@ -337,7 +337,7 @@ static void output_units_list(const struct unit_info *unit_infos, unsigned c) {
                         extra_len = columns() - basic_len;
                         /* Either UNIT already got 25, or is fully satisfied.
                          * Grant up to 25 to DESC now. */
-                        incr = MIN(extra_len, 25);
+                        incr = MIN(extra_len, 25u);
                         desc_len += incr;
                         extra_len -= incr;
                         /* split the remaining space between UNIT and DESC,
@@ -463,7 +463,7 @@ static int get_unit_list(DBusConnection *bus, DBusMessage **reply,
                 if (*c >= n_units) {
                         struct unit_info *w;
 
-                        n_units = MAX(2 * *c, 16);
+                        n_units = MAX(2 * *c, 16u);
                         w = realloc(*unit_infos, sizeof(struct unit_info) * n_units);
                         if (!w)
                                 return log_oom();
@@ -543,7 +543,7 @@ static void output_unit_file_list(const UnitFileList *units, unsigned c) {
 
         if (!arg_full) {
                 unsigned basic_cols;
-                id_cols = MIN(max_id_len, 25);
+                id_cols = MIN(max_id_len, 25u);
                 basic_cols = 1 + id_cols + state_cols;
                 if (basic_cols < (unsigned) columns())
                         id_cols += MIN(columns() - basic_cols, max_id_len - id_cols);
@@ -657,7 +657,7 @@ static int list_unit_files(DBusConnection *bus, char **args) {
                         if (c >= n_units) {
                                 UnitFileList *w;
 
-                                n_units = MAX(2*c, 16);
+                                n_units = MAX(2*c, 16u);
                                 w = realloc(units, sizeof(struct UnitFileList) * n_units);
                                 if (!w)
                                         return log_oom();
@@ -694,7 +694,7 @@ static int list_dependencies_print(const char *name, int level, unsigned int bra
         int i;
         _cleanup_free_ char *n = NULL;
         size_t len = 0;
-        size_t max_len = MAX(columns(),20);
+        size_t max_len = MAX(columns(),20u);
 
         for (i = level - 1; i >= 0; i--) {
                 len += 2;