Skip to content

Commit

Permalink
add built-time assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
esmil committed Aug 27, 2013
1 parent deb6346 commit 28633b6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions include/lem.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
# define __FUNCTION__ __func__ /* C99 */
#endif

/* Built-time assertions */
#define LEM_BUILD_ASSERT__(prefix, line) prefix##line
#define LEM_BUILD_ASSERT_(prefix, line) LEM_BUILD_ASSERT__(prefix, line)
#define LEM_BUILD_ASSERT(x) \
typedef int LEM_BUILD_ASSERT_(lem_assert_, __LINE__)[(x) ? 1 : -1]

#ifdef NDEBUG
#define lem_debug(...)
#else
Expand Down
1 change: 1 addition & 0 deletions lem/http/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ struct parse_http_state {
unsigned int w;
unsigned char state;
};
LEM_BUILD_ASSERT(sizeof(struct parse_http_state) < LEM_INPUTBUF_PSIZE);

static void
parse_http_init(lua_State *T)
Expand Down
5 changes: 5 additions & 0 deletions lem/parsers/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#include <lem-parsers.h>

#define LEM_PSTATE_CHECK(x) LEM_BUILD_ASSERT(sizeof(x) < LEM_INPUTBUF_PSIZE)

/*
* read available data
*/
Expand Down Expand Up @@ -45,6 +47,7 @@ struct parse_target_state {
size_t target;
int parts;
};
LEM_PSTATE_CHECK(struct parse_target_state);

static void
parse_target_init(lua_State *T, struct lem_inputbuf *b)
Expand Down Expand Up @@ -97,6 +100,7 @@ static const struct lem_parser parser_target = {
struct parse_all_state {
int parts;
};
LEM_PSTATE_CHECK(struct parse_all_state);

static void
parse_all_init(lua_State *T, struct lem_inputbuf *b)
Expand Down Expand Up @@ -159,6 +163,7 @@ struct parse_line_state {
int parts;
char stopbyte;
};
LEM_PSTATE_CHECK(struct parse_line_state);

static void
parse_line_init(lua_State *T, struct lem_inputbuf *b)
Expand Down

0 comments on commit 28633b6

Please sign in to comment.