From 98c15cbb658f226d52108ea2cb5b3bc27bc6a977 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Fri, 2 Aug 2013 15:10:20 +0200 Subject: built_in: Add min_t() and max_t() macros and use them Introduce non-typechecking versions of min_t() and max_t() and use them where a cast would be needed. The macros were taken from the Linux Kernel, release under GPL v2. Signed-off-by: Tobias Klauser --- built_in.h | 18 ++++++++++++++++++ geoip.c | 6 +++--- mac80211.c | 2 +- proto_ipv4.c | 4 ++-- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/built_in.h b/built_in.h index 9f70561..6f6625e 100644 --- a/built_in.h +++ b/built_in.h @@ -109,6 +109,15 @@ typedef uint8_t u8; }) #endif /* max */ +#ifndef max_t +# define max_t(type, a, b) \ + ({ \ + type ___max1 = (a); \ + type ___max2 = (b); \ + ___max1 > ___max2 ? ___max1 : ___max2; \ + }) +#endif /* max_t */ + #ifndef min # define min(a, b) \ ({ \ @@ -118,6 +127,15 @@ typedef uint8_t u8; }) #endif /* min */ +#ifndef min_t +# define min_t(type, a, b) \ + ({ \ + type ___min1 = (a); \ + type ___min2 = (b); \ + ___min1 < ___min2 ? ___min1 : ___min2; \ + }) +#endif /* min_t */ + #ifndef ispow2 # define ispow2(x) ({ !!((x) && !((x) & ((x) - 1))); }) #endif diff --git a/geoip.c b/geoip.c index d79236c..433dbbe 100644 --- a/geoip.c +++ b/geoip.c @@ -198,15 +198,15 @@ again: raw[sizeof(raw) - 1] = 0; for (i = 0; i < ret; i++) { - if (!strncmp(raw + i, "Content-Length: ", min((size_t)(ret - i), lenl))) { + if (!strncmp(raw + i, "Content-Length: ", min_t(size_t, ret - i, lenl))) { ptr = raw + i + lenl; rtotlen = strtoul(ptr, NULL, 10); } - if (!strncmp(raw + i, "HTTP/1.1 200 OK", min((size_t)(ret - i), lent))) + if (!strncmp(raw + i, "HTTP/1.1 200 OK", min_t(size_t, ret - i, lent))) good = 1; - if (!strncmp(raw + i, "\r\n\r\n", min((size_t)(ret - i), lenc))) { + if (!strncmp(raw + i, "\r\n\r\n", min_t(size_t, ret - i, lenc))) { ptr = raw + i + lenc; len = ret - i - lenc; found = 1; diff --git a/mac80211.c b/mac80211.c index 0154416..efb4af8 100644 --- a/mac80211.c +++ b/mac80211.c @@ -56,7 +56,7 @@ static void get_mac80211_phydev(const char *device, char *phydev_path, } xfree(pathstr); - phydev_path[min(num, phydev_len - 1)] = 0; + phydev_path[min_t(size_t, num, phydev_len - 1)] = 0; } static inline struct nl_msg *nl80211_nlmsg_xalloc(void) diff --git a/proto_ipv4.c b/proto_ipv4.c index e11098f..303319e 100644 --- a/proto_ipv4.c +++ b/proto_ipv4.c @@ -121,7 +121,7 @@ static void ipv4(struct pkt_buff *pkt) tprintf(") ]\n"); } - opts_len = max((uint8_t) ip->h_ihl, sizeof(*ip) / sizeof(uint32_t)) * + opts_len = max_t(uint8_t, ip->h_ihl, sizeof(*ip) / sizeof(uint32_t)) * sizeof(uint32_t) - sizeof(*ip); for (opt = pkt_pull(pkt, opts_len); opt && opts_len > 0; opt++) { @@ -184,7 +184,7 @@ static void ipv4_less(struct pkt_buff *pkt) ntohs(ip->h_tot_len)); /* cut off IP options and everything that is not part of IPv4 payload */ - pkt_pull(pkt, max((uint8_t) ip->h_ihl, sizeof(*ip) / sizeof(uint32_t)) + pkt_pull(pkt, max_t(uint8_t, ip->h_ihl, sizeof(*ip) / sizeof(uint32_t)) * sizeof(uint32_t) - sizeof(*ip)); /* XXX there coul still be an Ethernet trailer included or others */ #if 0 -- cgit v1.2.3-54-g00ecf