From fccc496c09730a24336c0c0cc26a951b70e3a36a Mon Sep 17 00:00:00 2001 From: pilcrow Date: Sun, 18 Sep 2011 21:50:39 -0500 Subject: [PATCH] Safer macro parenthesization and do/while use --- src/decode.h | 44 ++++++++++++++++---------------- src/defrag.c | 2 +- src/flow.c | 2 +- src/queue.h | 2 +- src/stream-tcp.c | 4 +- src/suricata.h | 12 ++++++--- src/tm-threads.c | 6 ++-- src/util-cuda.c | 5 ++- src/util-mem.h | 62 +++++++++++++++++++++++----------------------- src/util-mpm-ac-gfbs.c | 2 +- src/util-mpm-ac.c | 2 +- src/util-mpm-b2g-cuda.c | 4 ++- 12 files changed, 77 insertions(+), 70 deletions(-) diff --git a/src/decode.h b/src/decode.h index 7a02078..73f6547 100644 --- a/src/decode.h +++ b/src/decode.h @@ -134,31 +134,31 @@ typedef struct Address_ { /* Set the TCP ports into the Ports of the Packet. * Make sure p->tcph is initialized and validated. */ -#define SET_TCP_SRC_PORT(pkt, prt) do { \ - SET_PORT(TCP_GET_SRC_PORT((pkt)), *prt); \ +#define SET_TCP_SRC_PORT(pkt, prt) do { \ + SET_PORT(TCP_GET_SRC_PORT((pkt)), *(prt)); \ } while (0) -#define SET_TCP_DST_PORT(pkt, prt) do { \ - SET_PORT(TCP_GET_DST_PORT((pkt)), *prt); \ +#define SET_TCP_DST_PORT(pkt, prt) do { \ + SET_PORT(TCP_GET_DST_PORT((pkt)), *(prt)); \ } while (0) /* Set the UDP ports into the Ports of the Packet. * Make sure p->udph is initialized and validated. */ -#define SET_UDP_SRC_PORT(pkt, prt) do { \ - SET_PORT(UDP_GET_SRC_PORT((pkt)), *prt); \ +#define SET_UDP_SRC_PORT(pkt, prt) do { \ + SET_PORT(UDP_GET_SRC_PORT((pkt)), *(prt)); \ } while (0) -#define SET_UDP_DST_PORT(pkt, prt) do { \ - SET_PORT(UDP_GET_DST_PORT((pkt)), *prt); \ +#define SET_UDP_DST_PORT(pkt, prt) do { \ + SET_PORT(UDP_GET_DST_PORT((pkt)), *(prt)); \ } while (0) /* Set the SCTP ports into the Ports of the Packet. * Make sure p->sctph is initialized and validated. */ -#define SET_SCTP_SRC_PORT(pkt, prt) do { \ - SET_PORT(SCTP_GET_SRC_PORT((pkt)), *prt); \ +#define SET_SCTP_SRC_PORT(pkt, prt) do { \ + SET_PORT(SCTP_GET_SRC_PORT((pkt)), *(prt)); \ } while (0) -#define SET_SCTP_DST_PORT(pkt, prt) do { \ - SET_PORT(SCTP_GET_DST_PORT((pkt)), *prt); \ +#define SET_SCTP_DST_PORT(pkt, prt) do { \ + SET_PORT(SCTP_GET_DST_PORT((pkt)), *(prt)); \ } while (0) @@ -175,18 +175,18 @@ typedef struct Address_ { #define GET_PKT_LEN(p) ((p)->pktlen) #define GET_PKT_DATA(p) ((((p)->ext_pkt) == NULL ) ? (p)->pkt : (p)->ext_pkt) -#define GET_PKT_DIRECT_DATA(p) (p)->pkt -#define GET_PKT_DIRECT_MAX_SIZE(p) default_packet_size +#define GET_PKT_DIRECT_DATA(p) ((p)->pkt) +#define GET_PKT_DIRECT_MAX_SIZE(p) (default_packet_size) #define SET_PKT_LEN(p, len) do { \ - (p)->pktlen = len; \ + (p)->pktlen = (len); \ } while (0) /* Port is just a uint16_t */ typedef uint16_t Port; #define SET_PORT(v, p) ((p) = (v)) -#define COPY_PORT(a,b) (b) = (a) +#define COPY_PORT(a,b) ((b) = (a)) #define CMP_ADDR(a1, a2) \ (((a1)->addr_data32[3] == (a2)->addr_data32[3] && \ @@ -194,11 +194,11 @@ typedef uint16_t Port; (a1)->addr_data32[1] == (a2)->addr_data32[1] && \ (a1)->addr_data32[0] == (a2)->addr_data32[0])) #define CMP_PORT(p1, p2) \ - ((p1 == p2)) + ((p1) == (p2)) /*Given a packet pkt offset to the start of the ip header in a packet *We determine the ip version. */ -#define IP_GET_RAW_VER(pkt) (((pkt[0] & 0xf0) >> 4)) +#define IP_GET_RAW_VER(pkt) ((((pkt)[0] & 0xf0) >> 4)) #define PKT_IS_IPV4(p) (((p)->ip4h != NULL)) #define PKT_IS_IPV6(p) (((p)->ip6h != NULL)) @@ -213,7 +213,7 @@ typedef uint16_t Port; /* Retrieve proto regardless of IP version */ #define IP_GET_IPPROTO(p) \ - (PKT_IS_IPV4(p)? IPV4_GET_IPPROTO(p) : (PKT_IS_IPV6(p)? IPV6_GET_NH(p) : 0)) + (PKT_IS_IPV4((p))? IPV4_GET_IPPROTO((p)) : (PKT_IS_IPV6((p))? IPV6_GET_NH((p)) : 0)) /* structure to store the sids/gids/etc the detection engine * found in this packet */ @@ -459,11 +459,11 @@ typedef struct Packet_ #endif } Packet; -#define DEFAULT_PACKET_SIZE 1500 + ETHERNET_HEADER_LEN +#define DEFAULT_PACKET_SIZE (1500 + ETHERNET_HEADER_LEN) /* storage: maximum ip packet size + link header */ -#define MAX_PAYLOAD_SIZE IPV6_HEADER_LEN + 65536 + 28 +#define MAX_PAYLOAD_SIZE (IPV6_HEADER_LEN + 65536 + 28) intmax_t default_packet_size; -#define SIZE_OF_PACKET default_packet_size + sizeof(Packet) +#define SIZE_OF_PACKET (default_packet_size + sizeof(Packet)) typedef struct PacketQueue_ { Packet *top; diff --git a/src/defrag.c b/src/defrag.c index a186e78..0f54d3e 100644 --- a/src/defrag.c +++ b/src/defrag.c @@ -65,7 +65,7 @@ /** * Maximum allowed timeout, 24 hours. */ -#define TIMEOUT_MAX 60 * 60 * 24 +#define TIMEOUT_MAX (60 * 60 * 24) /** * Minimum allowed timeout, 1 second. diff --git a/src/flow.c b/src/flow.c index c3a2dea..b65f981 100644 --- a/src/flow.c +++ b/src/flow.c @@ -69,7 +69,7 @@ //#define FLOW_DEFAULT_HASHSIZE 262144 #define FLOW_DEFAULT_HASHSIZE 65536 //#define FLOW_DEFAULT_MEMCAP 128 * 1024 * 1024 /* 128 MB */ -#define FLOW_DEFAULT_MEMCAP 32 * 1024 * 1024 /* 32 MB */ +#define FLOW_DEFAULT_MEMCAP (32 * 1024 * 1024) /* 32 MB */ #define FLOW_DEFAULT_PREALLOC 10000 diff --git a/src/queue.h b/src/queue.h index bbf40a1..0a10f71 100644 --- a/src/queue.h +++ b/src/queue.h @@ -83,7 +83,7 @@ */ #if defined(QUEUE_MACRO_DEBUG) || (defined(_KERNEL) && defined(DIAGNOSTIC)) -#define _Q_INVALIDATE(a) (a) = ((void *)-1) +#define _Q_INVALIDATE(a) ((a) = ((void *)-1)) #else #define _Q_INVALIDATE(a) #endif diff --git a/src/stream-tcp.c b/src/stream-tcp.c index fac765e..20000db 100644 --- a/src/stream-tcp.c +++ b/src/stream-tcp.c @@ -69,8 +69,8 @@ #define STREAMTCP_DEFAULT_SESSIONS 262144 #define STREAMTCP_DEFAULT_PREALLOC 32768 -#define STREAMTCP_DEFAULT_MEMCAP 32 * 1024 * 1024 /* 32mb */ -#define STREAMTCP_DEFAULT_REASSEMBLY_MEMCAP 64 * 1024 * 1024 /* 64mb */ +#define STREAMTCP_DEFAULT_MEMCAP (32 * 1024 * 1024) /* 32mb */ +#define STREAMTCP_DEFAULT_REASSEMBLY_MEMCAP (64 * 1024 * 1024) /* 64mb */ #define STREAMTCP_DEFAULT_TOSERVER_CHUNK_SIZE 2560 #define STREAMTCP_DEFAULT_TOCLIENT_CHUNK_SIZE 2560 diff --git a/src/suricata.h b/src/suricata.h index d05d35a..4ac88b2 100644 --- a/src/suricata.h +++ b/src/suricata.h @@ -52,10 +52,14 @@ enum { }; /** You can use this macros to set/check if we have real drop capabilities */ -#define SET_ENGINE_MODE_IPS(engine_mode) (engine_mode = ENGINE_MODE_IPS); -#define SET_ENGINE_MODE_IDS(engine_mode) (engine_mode = ENGINE_MODE_IDS); -#define IS_ENGINE_MODE_IPS(engine_mode) (engine_mode == ENGINE_MODE_IPS) -#define IS_ENGINE_MODE_IDS(engine_mode) (engine_mode == ENGINE_MODE_IDS) +#define SET_ENGINE_MODE_IPS(engine_mode) do { \ + (engine_mode) = ENGINE_MODE_IPS; \ + } while (0) +#define SET_ENGINE_MODE_IDS(engine_mode) do { \ + (engine_mode) = ENGINE_MODE_IDS; \ + } while (0) +#define IS_ENGINE_MODE_IPS(engine_mode) ((engine_mode) == ENGINE_MODE_IPS) +#define IS_ENGINE_MODE_IDS(engine_mode) ((engine_mode) == ENGINE_MODE_IDS) /* queue's between various other threads * XXX move to the TmQueue structure later diff --git a/src/tm-threads.c b/src/tm-threads.c index 68f16f2..bd6fc17 100644 --- a/src/tm-threads.c +++ b/src/tm-threads.c @@ -55,9 +55,9 @@ #include #include #define cpu_set_t thread_affinity_policy_data_t -#define CPU_SET(cpu_id, new_mask) (*(new_mask)).affinity_tag = (cpu_id + 1) -#define CPU_ISSET(cpu_id, new_mask) ((*(new_mask)).affinity_tag == (cpu_id + 1)) -#define CPU_ZERO(new_mask) (*(new_mask)).affinity_tag = THREAD_AFFINITY_TAG_NULL +#define CPU_SET(cpu_id, new_mask) ((*(new_mask)).affinity_tag = ((cpu_id) + 1)) +#define CPU_ISSET(cpu_id, new_mask) (((*(new_mask)).affinity_tag == ((cpu_id) + 1))) +#define CPU_ZERO(new_mask) ((*(new_mask)).affinity_tag = THREAD_AFFINITY_TAG_NULL) #endif /* OS_FREEBSD */ /* prototypes */ diff --git a/src/util-cuda.c b/src/util-cuda.c index fbffab5..3bdc9de 100644 --- a/src/util-cuda.c +++ b/src/util-cuda.c @@ -4718,8 +4718,9 @@ static const char *sc_cuda_test_kernel_32_bit = int SCCudaTest02(void) { -#define ALIGN_UP(offset, alignment) \ - (offset) = ((offset) + (alignment) - 1) & ~((alignment) - 1) +#define ALIGN_UP(offset, alignment) do { \ + (offset) = ((offset) + (alignment) - 1) & ~((alignment) - 1); \ + } while (0) #define N 256 CUcontext context; CUmodule module; diff --git a/src/util-mem.h b/src/util-mem.h index ea2ef88..6f24146 100644 --- a/src/util-mem.h +++ b/src/util-mem.h @@ -49,20 +49,20 @@ SC_ATOMIC_EXTERN(unsigned int, engine_stage); extern size_t global_mem; \ extern uint8_t print_mem_flag; \ \ - ptrmem = malloc(a); \ - if (ptrmem == NULL && a > 0) { \ + ptrmem = malloc((a)); \ + if (ptrmem == NULL && (a) > 0) { \ SCLogError(SC_ERR_MEM_ALLOC, "SCMalloc failed: %s, while trying " \ - "to allocate %"PRIuMAX" bytes", strerror(errno), (uintmax_t)a); \ + "to allocate %"PRIuMAX" bytes", strerror(errno), (uintmax_t)(a)); \ if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {\ SCLogError(SC_ERR_FATAL, "Out of memory. The engine cannot be initialized. Exiting..."); \ exit(EXIT_FAILURE); \ } \ } \ \ - global_mem += a; \ + global_mem += (a); \ if (print_mem_flag == 1) \ SCLogInfo("SCMalloc return at %p of size %"PRIuMAX, \ - ptrmem, (uintmax_t)a); \ + ptrmem, (uintmax_t)(a)); \ \ (void*)ptrmem; \ }) @@ -72,20 +72,20 @@ SC_ATOMIC_EXTERN(unsigned int, engine_stage); extern size_t global_mem; \ extern uint8_t print_mem_flag; \ \ - ptrmem = realloc(x, a); \ - if (ptrmem == NULL && a > 0) { \ + ptrmem = realloc((x), (a)); \ + if (ptrmem == NULL && (a) > 0) { \ SCLogError(SC_ERR_MEM_ALLOC, "SCRealloc failed: %s, while trying " \ - "to allocate %"PRIuMAX" bytes", strerror(errno), (uintmax_t)a); \ + "to allocate %"PRIuMAX" bytes", strerror(errno), (uintmax_t)(a)); \ if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {\ SCLogError(SC_ERR_FATAL, "Out of memory. The engine cannot be initialized. Exiting..."); \ exit(EXIT_FAILURE); \ } \ } \ \ - global_mem += a; \ + global_mem += (a); \ if (print_mem_flag == 1) \ SCLogInfo("SCRealloc return at %p (old:%p) of size %"PRIuMAX, \ - ptrmem, x, (uintmax_t)a); \ + ptrmem, (x), (uintmax_t)(a)); \ \ (void*)ptrmem; \ }) @@ -95,8 +95,8 @@ SC_ATOMIC_EXTERN(unsigned int, engine_stage); extern size_t global_mem; \ extern uint8_t print_mem_flag; \ \ - ptrmem = calloc(nm, a); \ - if (ptrmem == NULL && a > 0) { \ + ptrmem = calloc((nm), (a)); \ + if (ptrmem == NULL && (a) > 0) { \ SCLogError(SC_ERR_MEM_ALLOC, "SCCalloc failed: %s, while trying " \ "to allocate %"PRIuMAX" bytes", strerror(errno), (uintmax_t)a); \ if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {\ @@ -105,10 +105,10 @@ SC_ATOMIC_EXTERN(unsigned int, engine_stage); } \ } \ \ - global_mem += a*nm; \ + global_mem += (a)*(nm); \ if (print_mem_flag == 1) \ - SCLogInfo("SCCalloc return at %p of size %"PRIuMAX" nm %"PRIuMAX, \ - ptrmem, (uintmax_t)a, (uintmax_t)nm); \ + SCLogInfo("SCCalloc return at %p of size %"PRIuMAX" (nm) %"PRIuMAX, \ + ptrmem, (uintmax_t)(a), (uintmax_t)(nm)); \ \ (void*)ptrmem; \ }) @@ -117,10 +117,10 @@ SC_ATOMIC_EXTERN(unsigned int, engine_stage); char *ptrmem = NULL; \ extern size_t global_mem; \ extern uint8_t print_mem_flag; \ + size_t len = strlen((a)); \ \ - ptrmem = strdup(a); \ + ptrmem = strdup((a)); \ if (ptrmem == NULL) { \ - size_t len = strlen(a); \ SCLogError(SC_ERR_MEM_ALLOC, "SCStrdup failed: %s, while trying " \ "to allocate %"PRIuMAX" bytes", strerror(errno), (uintmax_t)len); \ if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {\ @@ -140,20 +140,20 @@ SC_ATOMIC_EXTERN(unsigned int, engine_stage); #define SCFree(a) ({ \ extern uint8_t print_mem_flag; \ if (print_mem_flag == 1) \ - SCLogInfo("SCFree at %p", a); \ - free(a); \ + SCLogInfo("SCFree at %p", (a)); \ + free((a)); \ }) -#else /* DBG_MEM_ALLOC */ +#else /* !DBG_MEM_ALLOC */ #define SCMalloc(a) ({ \ void *ptrmem = NULL; \ \ - ptrmem = malloc(a); \ + ptrmem = malloc((a)); \ if (ptrmem == NULL) { \ if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {\ SCLogError(SC_ERR_MEM_ALLOC, "SCMalloc failed: %s, while trying " \ - "to allocate %"PRIuMAX" bytes", strerror(errno), (uintmax_t)a); \ + "to allocate %"PRIuMAX" bytes", strerror(errno), (uintmax_t)(a)); \ SCLogError(SC_ERR_FATAL, "Out of memory. The engine cannot be initialized. Exiting..."); \ exit(EXIT_FAILURE); \ } \ @@ -164,11 +164,11 @@ SC_ATOMIC_EXTERN(unsigned int, engine_stage); #define SCRealloc(x, a) ({ \ void *ptrmem = NULL; \ \ - ptrmem = realloc(x, a); \ + ptrmem = realloc((x), (a)); \ if (ptrmem == NULL) { \ if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {\ SCLogError(SC_ERR_MEM_ALLOC, "SCRealloc failed: %s, while trying " \ - "to allocate %"PRIuMAX" bytes", strerror(errno), (uintmax_t)a); \ + "to allocate %"PRIuMAX" bytes", strerror(errno), (uintmax_t)(a)); \ SCLogError(SC_ERR_FATAL, "Out of memory. The engine cannot be initialized. Exiting..."); \ exit(EXIT_FAILURE); \ } \ @@ -179,11 +179,11 @@ SC_ATOMIC_EXTERN(unsigned int, engine_stage); #define SCCalloc(nm, a) ({ \ void *ptrmem = NULL; \ \ - ptrmem = calloc(nm, a); \ + ptrmem = calloc((nm), (a)); \ if (ptrmem == NULL) { \ if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {\ SCLogError(SC_ERR_MEM_ALLOC, "SCCalloc failed: %s, while trying " \ - "to allocate %"PRIuMAX" bytes", strerror(errno), (uintmax_t)a); \ + "to allocate %"PRIuMAX" bytes", strerror(errno), (uintmax_t)(a)); \ SCLogError(SC_ERR_FATAL, "Out of memory. The engine cannot be initialized. Exiting..."); \ exit(EXIT_FAILURE); \ } \ @@ -194,10 +194,10 @@ SC_ATOMIC_EXTERN(unsigned int, engine_stage); #define SCStrdup(a) ({ \ char *ptrmem = NULL; \ \ - ptrmem = strdup(a); \ + ptrmem = strdup((a)); \ if (ptrmem == NULL) { \ if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {\ - size_t len = strlen(a); \ + size_t len = strlen((a)); \ SCLogError(SC_ERR_MEM_ALLOC, "SCStrdup failed: %s, while trying " \ "to allocate %"PRIuMAX" bytes", strerror(errno), (uintmax_t)len); \ SCLogError(SC_ERR_FATAL, "Out of memory. The engine cannot be initialized. Exiting..."); \ @@ -217,7 +217,7 @@ SC_ATOMIC_EXTERN(unsigned int, engine_stage); if (posix_memalign(&ptrmem, (b), (a)) != 0) { \ if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {\ SCLogError(SC_ERR_MEM_ALLOC, "SCMallocAligned(posix_memalign) failed: %s, while trying " \ - "to allocate %"PRIuMAX" bytes, alignment %"PRIuMAX, strerror(errno), (uintmax_t)a, (uintmax_t)b); \ + "to allocate %"PRIuMAX" bytes, alignment %"PRIuMAX, strerror(errno), (uintmax_t)(a), (uintmax_t)(b)); \ SCLogError(SC_ERR_FATAL, "Out of memory. The engine cannot be initialized. Exiting..."); \ exit(EXIT_FAILURE); \ } \ @@ -226,7 +226,7 @@ SC_ATOMIC_EXTERN(unsigned int, engine_stage); }) #define SCFree(a) ({ \ - free(a); \ + free((a)); \ }) /** \brief Free aligned memory @@ -236,7 +236,7 @@ SC_ATOMIC_EXTERN(unsigned int, engine_stage); * _mm_free. */ #define SCFreeAligned(a) ({ \ - free(a); \ + free((a)); \ }) diff --git a/src/util-mpm-ac-gfbs.c b/src/util-mpm-ac-gfbs.c index 42c9fd4..e22a596 100644 --- a/src/util-mpm-ac-gfbs.c +++ b/src/util-mpm-ac-gfbs.c @@ -65,7 +65,7 @@ void SCACGfbsPrintSearchStats(MpmThreadCtx *mpm_thread_ctx); void SCACGfbsRegisterTests(void); /* a placeholder to denote a failure transition in the goto table */ -#define SC_AC_GFBS_FAIL -1 +#define SC_AC_GFBS_FAIL (-1) /* size of the hash table used to speed up pattern insertions initially */ #define INIT_HASH_SIZE 65536 diff --git a/src/util-mpm-ac.c b/src/util-mpm-ac.c index a723cf9..7b514c7 100644 --- a/src/util-mpm-ac.c +++ b/src/util-mpm-ac.c @@ -72,7 +72,7 @@ void SCACPrintSearchStats(MpmThreadCtx *mpm_thread_ctx); void SCACRegisterTests(void); /* a placeholder to denote a failure transition in the goto table */ -#define SC_AC_FAIL -1 +#define SC_AC_FAIL (-1) /* size of the hash table used to speed up pattern insertions initially */ #define INIT_HASH_SIZE 65536 diff --git a/src/util-mpm-b2g-cuda.c b/src/util-mpm-b2g-cuda.c index e42b244..f20328f 100644 --- a/src/util-mpm-b2g-cuda.c +++ b/src/util-mpm-b2g-cuda.c @@ -1451,7 +1451,9 @@ static int B2gCudaMpmStreamDataInit(B2gCudaMpmThreadCtxData *tctx, MpmCudaConf * goto error; } -#define ALIGN_UP(offset, alignment) (offset) = ((offset) + (alignment) - 1) & ~((alignment) - 1) +#define ALIGN_UP(offset, alignment) do { \ + (offset) = ((offset) + (alignment) - 1) & ~((alignment) - 1); \ + } while (0) int offset = 0; -- 1.7.4.1