Skip to content

Commit

Permalink
Use _Static_assert in C code instead of static_assert
Browse files Browse the repository at this point in the history
  • Loading branch information
yellowman committed May 28, 2021
1 parent 8432b04 commit 09ffbfa
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/core/configurator/cpipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ static int cpipe_fd[2] = {INVALID_FD, INVALID_FD};
static const char *module = "Configuration pipe";

// Size of the request must allow atomic write! (see write() for help)
static_assert(sizeof(struct ipx_cpipe_req) <= PIPE_BUF, "non-atomic write!");
_Static_assert(sizeof(struct ipx_cpipe_req) <= PIPE_BUF, "non-atomic write!");

int
ipx_cpipe_init()
Expand Down
2 changes: 1 addition & 1 deletion src/core/message_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ struct ipx_msg {
unsigned int ref_cnt;
}; // TODO: 64 bytes alignment

static_assert(offsetof(struct ipx_msg, type) == 0,
_Static_assert(offsetof(struct ipx_msg, type) == 0,
"Message type must be the first element of each IPFIXcol message.");

/**
Expand Down
2 changes: 1 addition & 1 deletion src/core/message_garbage.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ struct ipx_msg_garbage {
ipx_msg_garbage_cb object_destructor;
};

static_assert(offsetof(struct ipx_msg_garbage, msg_header.type) == 0,
_Static_assert(offsetof(struct ipx_msg_garbage, msg_header.type) == 0,
"Message header must be the first element of each IPFIXcol message.");

// Create a garbage message
Expand Down
4 changes: 2 additions & 2 deletions src/core/message_ipfix.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
#include <stdlib.h> // free

// Check correctness of structure implementation
static_assert(offsetof(struct ipx_msg_ipfix, msg_header.type) == 0,
_Static_assert(offsetof(struct ipx_msg_ipfix, msg_header.type) == 0,
"Message header must be the first element of each IPFIXcol message.");

size_t
Expand Down Expand Up @@ -190,4 +190,4 @@ ipx_msg_ipfix_add_drec_ref(struct ipx_msg_ipfix **msg_ref)
const size_t offset = msg->rec_info.cnt_valid * msg->rec_info.rec_size;
msg->rec_info.cnt_valid++;
return ((struct ipx_ipfix_record *) (((uint8_t *) msg->recs) + offset));
}
}
2 changes: 1 addition & 1 deletion src/core/message_session.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ struct ipx_msg_session {
const struct ipx_session *session;
};

static_assert(offsetof(struct ipx_msg_session, msg_header.type) == 0,
_Static_assert(offsetof(struct ipx_msg_session, msg_header.type) == 0,
"Message header must be the first element of each IPFIXcol message.");

ipx_msg_session_t *
Expand Down
4 changes: 2 additions & 2 deletions src/core/message_terminate.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ struct ipx_msg_terminate {
enum ipx_msg_terminate_type type;
};

static_assert(offsetof(struct ipx_msg_terminate, msg_header.type) == 0,
_Static_assert(offsetof(struct ipx_msg_terminate, msg_header.type) == 0,
"Message type must be the first element of each IPFIXcol message.");

ipx_msg_terminate_t *
Expand Down Expand Up @@ -84,4 +84,4 @@ enum ipx_msg_terminate_type
ipx_msg_terminate_get_type(const ipx_msg_terminate_t *msg)
{
return msg->type;
}
}
12 changes: 6 additions & 6 deletions src/core/netflow2ipfix/netflow5.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
#include "../verbose.h"

// Simple static asserts to prevent unexpected structure modifications!
static_assert(IPX_NF5_MSG_HDR_LEN == 24U, "NetFlow v5 header size is not valid!");
static_assert(IPX_NF5_MSG_REC_LEN == 48U, "NetFlow v5 record size is not valid!");
_Static_assert(IPX_NF5_MSG_HDR_LEN == 24U, "NetFlow v5 header size is not valid!");
_Static_assert(IPX_NF5_MSG_REC_LEN == 48U, "NetFlow v5 record size is not valid!");

/// Auxiliary definition to represent size of 1 byte
#define BYTES_1 (1U)
Expand Down Expand Up @@ -78,7 +78,7 @@ static const uint16_t nf5_tmpl_set[] = {

/// Number of fields (including header fields) in the Template Set
#define NF5_TSET_ITEMS (sizeof(nf5_tmpl_set) / sizeof(nf5_tmpl_set[0]))
static_assert(NF5_TSET_ITEMS % 2 == 0, "Number of fields MUST be even!");
_Static_assert(NF5_TSET_ITEMS % 2 == 0, "Number of fields MUST be even!");

/**
* @brief Structure of a NetFlow v5 record after convertion to IPFIX
Expand Down Expand Up @@ -130,8 +130,8 @@ struct __attribute__((__packed__)) new_ipx_rec {
/// Size of the second part to copy (without tailing padding)
#define PART2_LEN (offsetof(struct ipx_nf5_rec, _pad2) - offsetof(struct ipx_nf5_rec, port_src))

static_assert(PART1_LEN == offsetof(struct new_ipx_rec, ts_first), "Different Part 1 size");
static_assert(PART2_LEN ==
_Static_assert(PART1_LEN == offsetof(struct new_ipx_rec, ts_first), "Different Part 1 size");
_Static_assert(PART2_LEN ==
offsetof(struct new_ipx_rec, sampling_alg) - offsetof(struct new_ipx_rec, port_src),
"Different Part 2 size");

Expand Down Expand Up @@ -552,4 +552,4 @@ void
ipx_nf5_conv_verb(ipx_nf5_conv_t *conv, enum ipx_verb_level v_new)
{
conv->conf.vlevel = v_new;
}
}
6 changes: 3 additions & 3 deletions src/core/netflow2ipfix/netflow9.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
(UINT16_MAX - FDS_IPFIX_MSG_HDR_LEN - FDS_IPFIX_SET_HDR_LEN)

// Simple static asserts to prevent unexpected structure modifications!
static_assert(IPX_NF9_MSG_HDR_LEN == 20U, "NetFlow v9 Message header size is not valid!");
static_assert(IPX_NF9_SET_HDR_LEN == 4U, "NetFlow v9 Set header size is not valid!");
_Static_assert(IPX_NF9_MSG_HDR_LEN == 20U, "NetFlow v9 Message header size is not valid!");
_Static_assert(IPX_NF9_SET_HDR_LEN == 4U, "NetFlow v9 Set header size is not valid!");

/// Auxiliary conversion structure from NetFlow Options Field to IPFIX Inf. Element ID
struct nf2ipx_opts {
Expand Down Expand Up @@ -1274,4 +1274,4 @@ void
ipx_nf9_conv_verb(ipx_nf9_conv_t *conv, enum ipx_verb_level v_new)
{
conv->vlevel = v_new;
}
}
4 changes: 2 additions & 2 deletions src/core/odid_range.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ range_is_empty(const char *str)
static int
range_str2uint32(const char *str, uint32_t *res)
{
static_assert(ULONG_MAX >= UINT32_MAX, "Unsigned long is too small.");
_Static_assert(ULONG_MAX >= UINT32_MAX, "Unsigned long is too small.");

errno = 0;
char *end_ptr = NULL;
Expand Down Expand Up @@ -433,4 +433,4 @@ ipx_orange_print(const ipx_orange_t *range)
break;
}
}
}
}
4 changes: 2 additions & 2 deletions src/plugins/input/tcp/tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ socket_process(ipx_ctx_t *ctx, struct tcp_pair *pair)
{
const char *err_str;
struct fds_ipfix_msg_hdr hdr;
static_assert(sizeof(hdr) == FDS_IPFIX_MSG_HDR_LEN, "Invalid size of IPFIX Message header");
_Static_assert(sizeof(hdr) == FDS_IPFIX_MSG_HDR_LEN, "Invalid size of IPFIX Message header");

// Get the message header (do not move pointer)
ssize_t len = recv(pair->fd, &hdr, FDS_IPFIX_MSG_HDR_LEN, MSG_WAITALL | MSG_PEEK);
Expand Down Expand Up @@ -1026,4 +1026,4 @@ ipx_plugin_session_close(ipx_ctx_t *ctx, void *cfg, const struct ipx_session *se
IPX_CTX_WARNING(ctx, "Received a request to close a unknown Transport Session!", '\0');
return;
}
}
}

0 comments on commit 09ffbfa

Please sign in to comment.