From 28633b6935340dd79a80bdccf491c27d82d8169f Mon Sep 17 00:00:00 2001 From: Emil Renner Berthing Date: Tue, 27 Aug 2013 21:28:16 +0200 Subject: add built-time assertions --- include/lem.h | 6 ++++++ lem/http/core.c | 1 + lem/parsers/core.c | 5 +++++ 3 files changed, 12 insertions(+) diff --git a/include/lem.h b/include/lem.h index 5b35459..2b548b8 100644 --- a/include/lem.h +++ b/include/lem.h @@ -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 diff --git a/lem/http/core.c b/lem/http/core.c index 2186294..e9a6628 100644 --- a/lem/http/core.c +++ b/lem/http/core.c @@ -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) diff --git a/lem/parsers/core.c b/lem/parsers/core.c index affde27..918e95f 100644 --- a/lem/parsers/core.c +++ b/lem/parsers/core.c @@ -18,6 +18,8 @@ #include +#define LEM_PSTATE_CHECK(x) LEM_BUILD_ASSERT(sizeof(x) < LEM_INPUTBUF_PSIZE) + /* * read available data */ @@ -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) @@ -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) @@ -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) -- cgit v1.2.1