chiark / gitweb /
util.h etc.: Provide MAX_RAW and MIN_RAW; etc.
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 3 Oct 2014 19:22:18 +0000 (20:22 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 21 Oct 2014 00:07:09 +0000 (01:07 +0100)
MAX and MIN are unsuitable for use where a constant expression is
required.  Provide MAX_RAW and MIN_RAW which are suitable but might
evaluate the left argument twice.

Remove max() in ipaddr.c and replace the call with one to MAX (not
MAX_RAW.  (The old max macro there is operator-priority-unsafe but
there is only one call site and it happens to be OK.)

We ae going to use MAX_RAW later.

No functional change.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
ipaddr.c
util.h

index e6d24075806693ce1c347eb4f1f1bb417c039799..899581f0e0a11bae619c03b8ad1d01fb32b010af 100644 (file)
--- a/ipaddr.c
+++ b/ipaddr.c
@@ -7,6 +7,7 @@
 #include <stdio.h>
 #include <string.h>
 #include "ipaddr.h"
+#include "util.h"
 
 #define DEFAULT_ALLOC 2
 #define EXTEND_ALLOC_BY 4
@@ -130,7 +131,6 @@ static void ipset_append_range(struct ipset *a, struct iprange r)
     a->d[a->l-1]=r;
 }
 
-#define max(a,b) (a>b?a:b)
 struct ipset *ipset_union(struct ipset *a, struct ipset *b)
 {
     struct ipset *c;
@@ -155,7 +155,7 @@ struct ipset *ipset_union(struct ipset *a, struct ipset *b)
            ipset_append_range(c,r);
        else if (r.a <= c->d[c->l-1].b+1)
            /* Extends (or is consumed by) the last range */
-           c->d[c->l-1].b=max(c->d[c->l-1].b, r.b);
+           c->d[c->l-1].b=MAX(c->d[c->l-1].b, r.b);
        else
            ipset_append_range(c,r);
     }
diff --git a/util.h b/util.h
index b44b911189ee0d2e28c8b8df8ffcea91ef987fc1..a5a82a431e8eed383e24b6d55f122ac2ea779154 100644 (file)
--- a/util.h
+++ b/util.h
@@ -161,6 +161,9 @@ async_linebuf_read(struct pollfd *pfd, struct buffer_if *buf,
 #define MAX(a,b) MINMAX((a),(b),>)
 #define MIN(a,b) MINMAX((a),(b),<)
 
+#define MAX_RAW(a,b) ((a)>(b)?(a):(b))
+#define MIN_RAW(a,b) ((a)<(b)?(a):(b))
+
 static inline bool_t iswouldblock(int e)
     { return e==EWOULDBLOCK || e==EAGAIN; }