From 32ba42485009777569bfb61bb12978d7beac1bea Mon Sep 17 00:00:00 2001 From: Jaromil Date: Thu, 9 Jan 2025 00:34:51 +0100 Subject: [PATCH] fix: compile with clang more safety checks upgraded to use hedley --- src/lua_modules.c | 4 ++-- src/zen_big.c | 13 +++++------ src/zen_big.h | 6 +++++ src/zen_ecdh.c | 4 ++-- src/zen_ecp.c | 8 +++---- src/zen_ecp.h | 1 + src/zen_ecp2.c | 8 +++---- src/zen_error.c | 12 +++++----- src/zen_error.h | 6 ++--- src/zen_octet.c | 58 +++++++++++++++++++++++------------------------ src/zen_parse.c | 6 ++--- src/zen_qp.c | 18 +++++++-------- src/zen_random.c | 4 ++-- src/zen_rsa.c | 12 +++++----- src/zenroom.c | 18 ++++----------- 15 files changed, 88 insertions(+), 90 deletions(-) diff --git a/src/lua_modules.c b/src/lua_modules.c index 3817af64b..2392df504 100644 --- a/src/lua_modules.c +++ b/src/lua_modules.c @@ -101,8 +101,8 @@ int zen_load_string(lua_State *L, const char *code, return(res); } +HEDLEY_NON_NULL(1,2); int zen_exec_extension(lua_State *L, zen_extension_t *p) { - SAFE(p); // HEREs(p->name); #ifdef __EMSCRIPTEN__ if(p->code) { // HEREs(p->code); @@ -137,8 +137,8 @@ int nop(lua_State *L) { #ifndef S_SPLINT_S // src/lua_modules.c:139:15: Parse Error. Attempting to continue. +HEDLEY_NON_NULL(1); int zen_require(lua_State *L) { - SAFE(L); size_t len; const char *s = lua_tolstring(L, 1, &len); // HEREs(s); diff --git a/src/zen_big.c b/src/zen_big.c index 64f6d52ec..76405d4e4 100644 --- a/src/zen_big.c +++ b/src/zen_big.c @@ -188,7 +188,6 @@ big* big_arg(lua_State *L,int n) { // allocates a new big in LUA, duplicating the one in arg big *big_dup(lua_State *L, big *s) { - SAFE(s); big *n = big_new(L); if(s->doublesize) { dbig_init(L,n); @@ -317,7 +316,7 @@ static int lua_biginfo(lua_State *L) { // TODO: fix this to return something usable in modmul static int lua_bigmax(lua_State *L) { BEGIN(); - big *b = big_new(L); SAFE(b); + big *b = big_new(L); big_init(L, b); register int c; for(c=0 ; c < b->len ; c++) b->val[c] = 0xffffffff; @@ -339,9 +338,9 @@ static int newbig(lua_State *L) { ud = luaL_testudata(L, 2, "zenroom.big"); if(ud) { warning(L, "use of RNG deprecated"); - big *res = big_new(L); big_init(L,res); SAFE(res); + big *res = big_new(L); big_init(L,res); // random with modulus - big *modulus = (big*)ud; SAFE(modulus); + big *modulus = (big*)ud; Z(L); BIG_randomnum(res->val,modulus->val,Z->random_generator); return 1; @@ -353,7 +352,7 @@ static int newbig(lua_State *L) { if(tn) { // if(n > 0xffff) // warning(L, "Import of number to BIG limit exceeded (>16bit)"); - big *c = big_new(L); SAFE(c); + big *c = big_new(L); big_init(L,c); BIG_zero(c->val); if((int)n>0) @@ -436,7 +435,7 @@ static int big_from_decimal_string(lua_State *L) { if(!s) { return 0; } - big *num = big_new(L); SAFE(num); + big *num = big_new(L); big_init(L,num); BIG_zero(num->val); @@ -970,7 +969,7 @@ static int big_modrand(lua_State *L) { static int big_random(lua_State *L) { BEGIN(); - big *res = big_new(L); big_init(L,res); SAFE(res); + big *res = big_new(L); big_init(L,res); Z(L); BIG_randomnum(res->val,(chunk*)CURVE_Order,Z->random_generator); END(1); diff --git a/src/zen_big.h b/src/zen_big.h index d79a0f721..45655fcd6 100644 --- a/src/zen_big.h +++ b/src/zen_big.h @@ -44,22 +44,28 @@ typedef struct { HEDLEY_MALLOC HEDLEY_RETURNS_NON_NULL HEDLEY_WARN_UNUSED_RESULT +HEDLEY_NON_NULL(1) big* big_new(lua_State *L); HEDLEY_MALLOC HEDLEY_RETURNS_NON_NULL HEDLEY_WARN_UNUSED_RESULT +HEDLEY_NON_NULL(1,2) big* big_dup(lua_State *L, big *c); +HEDLEY_NON_NULL(1,2) void big_free(lua_State *L, HEDLEY_NO_ESCAPE big *c); HEDLEY_MALLOC HEDLEY_RETURNS_NON_NULL HEDLEY_WARN_UNUSED_RESULT +HEDLEY_NON_NULL(1) big* big_arg(lua_State *L, int n); // internal initialisation of double or single big +HEDLEY_NON_NULL(1,2) int big_init(lua_State *L,big *n); +HEDLEY_NON_NULL(1,2) int dbig_init(lua_State *L,big *n); // internal conversion from d/big to octet diff --git a/src/zen_ecdh.c b/src/zen_ecdh.c index 7d8a2f48f..c9dc39826 100644 --- a/src/zen_ecdh.c +++ b/src/zen_ecdh.c @@ -1006,7 +1006,7 @@ static int ecdh_order(lua_State *L) { lerror(L, "%s: ECDH order not implemented", __func__); return 0; } - big *o = big_new(L); SAFE(o); + big *o = big_new(L); big_init(L,o); BIG_fromBytesLen(o->val, ECDH.order, ECDH.mod_size); END(1); @@ -1024,7 +1024,7 @@ static int ecdh_prime(lua_State *L) { lerror(L, "%s: ECDH modulus not implemented", __func__); return 0; } - big *p = big_new(L); SAFE(p); + big *p = big_new(L); big_init(L,p); BIG_fromBytesLen(p->val, ECDH.prime, ECDH.mod_size); END(1); diff --git a/src/zen_ecp.c b/src/zen_ecp.c index a81fc4d84..db2874ab7 100644 --- a/src/zen_ecp.c +++ b/src/zen_ecp.c @@ -240,7 +240,7 @@ static int lua_new_ecp(lua_State *L) { failed_msg = "Could not allocate octet"; goto end; } - ecp *e = ecp_new(L); SAFE(e); + ecp *e = ecp_new(L); if(o->len > e->totlen) { // double safety lua_pop(L, 1); zerror(L, "%s: octet length %u instead of %u bytes", __func__, o->len, e->totlen); @@ -266,7 +266,7 @@ static int lua_new_ecp(lua_State *L) { */ static int ecp_generator(lua_State *L) { BEGIN(); - ecp *e = ecp_new(L); SAFE(e); + ecp *e = ecp_new(L); /* if(!ECP_set(&e->val, (chunk*)CURVE_Gx, (chunk*)CURVE_Gy)) { lerror(L, "ECP generator value out of curve (stack corruption)"); @@ -335,7 +335,7 @@ static int ecp_mapit(lua_State *L) { lerror(L, "Invalid argument to ECP.mapit(), not an hash"); lua_pushnil(L); } else { - ecp *e = ecp_new(L); SAFE(e); + ecp *e = ecp_new(L); func(L, "mapit on o->len %u", o->len); ECP_mapit(&e->val, o); o_free(L, o); @@ -716,7 +716,7 @@ static int ecp_get_y(lua_State *L) { static int ecp_prime(lua_State *L) { BEGIN(); - big *p = big_new(L); big_init(L,p); SAFE(p); + big *p = big_new(L); big_init(L,p); BIG_rcopy(p->val, CURVE_Prime); END(1); } diff --git a/src/zen_ecp.h b/src/zen_ecp.h index a8c7197ec..c7be62241 100644 --- a/src/zen_ecp.h +++ b/src/zen_ecp.h @@ -22,6 +22,7 @@ #define __ZEN_ECP_H__ #include +#include typedef struct { size_t halflen; // length in bytes of a reduced coordinate diff --git a/src/zen_ecp2.c b/src/zen_ecp2.c index fde1017c3..3e00ac83c 100644 --- a/src/zen_ecp2.c +++ b/src/zen_ecp2.c @@ -146,7 +146,7 @@ static int lua_new_ecp2(lua_State *L) { void *tyi = luaL_testudata(L, 4, "zenroom.big"); if(tx && txi && ty && tyi) { - ecp2 *e = ecp2_new(L); SAFE(e); + ecp2 *e = ecp2_new(L); big *x, *xi, *y, *yi; x = big_arg(L, 1); xi = big_arg(L, 2); @@ -172,10 +172,10 @@ static int lua_new_ecp2(lua_State *L) { } // If x is on the curve then y is calculated from the curve equation. if(tx && txi) { - ecp2 *e = ecp2_new(L); SAFE(e); + ecp2 *e = ecp2_new(L); big *x, *xi; - x = big_arg(L, 1); SAFE(x); - xi = big_arg(L, 2); SAFE(xi); + x = big_arg(L, 1); + xi = big_arg(L, 2); if(!x || !xi) { failed_msg = "Could not create BIG"; goto end_big_big; diff --git a/src/zen_error.c b/src/zen_error.c index 982e7da2a..d01fbb615 100644 --- a/src/zen_error.c +++ b/src/zen_error.c @@ -184,7 +184,7 @@ int notice(void *L, const char *format, ...) { va_start(arg, format); Z_FORMAT_ARG(L); if(Z && Z->debuglevel<1) return 0; - octet *o = o_alloc(L, MAX_ERRMSG); SAFE(o); + octet *o = o_alloc(L, MAX_ERRMSG); mutt_vsnprintf(o->val, o->max-5, format, arg); o->len = strlen(o->val); zen_log(L, LOG_INFO, o); @@ -197,7 +197,7 @@ int func(void *L, const char *format, ...) { va_start(arg, format); Z_FORMAT_ARG(L); if(Z && Z->debuglevel<3) return 0; - octet *o = o_alloc(L, MAX_ERRMSG); SAFE(o); + octet *o = o_alloc(L, MAX_ERRMSG); mutt_vsnprintf(o->val, o->max-5, format, arg); o->len = strlen(o->val); zen_log(L, LOG_VERBOSE, o); @@ -210,7 +210,7 @@ int trace(void *L, const char *format, ...) { va_start(arg, format); Z_FORMAT_ARG(L); if(Z && Z->debuglevel<4) return 0; - octet *o = o_alloc(L, MAX_ERRMSG); SAFE(o); + octet *o = o_alloc(L, MAX_ERRMSG); mutt_vsnprintf(o->val, o->max-5, format, arg); o->len = strlen(o->val); zen_log(L, LOG_VERBOSE, o); @@ -222,7 +222,7 @@ int zerror(void *L, const char *format, ...) { va_list arg; va_start(arg, format); Z_FORMAT_ARG(L); - octet *o = o_alloc(L, MAX_ERRMSG); SAFE(o); + octet *o = o_alloc(L, MAX_ERRMSG); mutt_vsnprintf(o->val, o->max-5, format, arg); o->len = strlen(o->val); zen_log(L, LOG_ERROR, o); @@ -235,7 +235,7 @@ int act(void *L, const char *format, ...) { va_start(arg, format); Z_FORMAT_ARG(L); if(Z && Z->debuglevel<2) return 0; - octet *o = o_alloc(L, MAX_ERRMSG); SAFE(o); + octet *o = o_alloc(L, MAX_ERRMSG); // new octet is pushed to stack mutt_vsnprintf(o->val, o->max-5, format, arg); o->len = strlen(o->val); @@ -249,7 +249,7 @@ int warning(void *L, const char *format, ...) { va_start(arg, format); Z_FORMAT_ARG(L); if(Z && Z->debuglevel<1) return 0; - octet *o = o_alloc(L, MAX_ERRMSG); SAFE(o); + octet *o = o_alloc(L, MAX_ERRMSG); mutt_vsnprintf(o->val, o->max-5, format, arg); o->len = strlen(o->val); zen_log(L, LOG_WARN, o); diff --git a/src/zen_error.h b/src/zen_error.h index f73b24ed7..7f24028b8 100644 --- a/src/zen_error.h +++ b/src/zen_error.h @@ -29,9 +29,9 @@ // macro to obtain Z context from a lua_State #define Z(l) \ - if(HEDLEY_UNLIKELY(l==NULL) \ + if(HEDLEY_UNLIKELY(l==NULL)) \ lerror(l,"NULL lua_State in %s",__func__); \ - zenroom_t *Z; lua_getallocf(l, &Z) + zenroom_t *Z; lua_getallocf(l, (void**)&Z) // tracing wrappers for all C->Lua functions #define BEGIN() trace(L, "vv begin %s",__func__) @@ -87,7 +87,7 @@ int warning(void *L, const char *format, ...); // WARN void json_start(void *L); void json_end(void *L); -#define SAFE(x) HEDLEY_REQUIRE_MSG(x!=NULL, __func__) +// #define SAFE(x) HEDLEY_REQUIRE(x != NULL) // if(!x) lerror(L, "NULL variable in %s",__func__) // useful for debugging diff --git a/src/zen_octet.c b/src/zen_octet.c index 00c8b669a..3ed968f54 100644 --- a/src/zen_octet.c +++ b/src/zen_octet.c @@ -337,7 +337,7 @@ octet *o_dup(lua_State *L, octet *o) { } void push_buffer_to_octet(lua_State *L, char *p, size_t len) { - octet* o = o_new(L, len); SAFE(o); + octet* o = o_new(L, len); // newuserdata already pushes the object in lua's stack // memcpy(o->val, p, len); register uint32_t i; @@ -430,9 +430,9 @@ static int filloctet(lua_State *L) { BEGIN(); int i; octet *o = (octet*) luaL_testudata(L, 1, "zenroom.octet"); - SAFE(o); + octet *fill = (octet*) luaL_testudata(L, 2, "zenroom.octet"); - SAFE(fill); + for(i=0; imax; i++) o->val[i] = fill->val[i % fill->len]; o->len = o->max; @@ -601,7 +601,7 @@ static int from_number(lua_State *L) { lerror(L, "O.from_number input is not a number"); return 0; } const uint64_t v = n; - octet *o = o_new(L, 16); SAFE(o); + octet *o = o_new(L, 16); // conversion from int64 to binary // TODO: check endian portability issues register uint8_t i = 0; @@ -631,7 +631,7 @@ static int from_rawlen (lua_State *L) { if(!tn) { lerror(L, "O.new 2nd arg is not a number"); return 0; } - octet *o = o_new(L, (int)n); SAFE(o); + octet *o = o_new(L, (int)n); register int c; for(c=0;cval[c] = s[c]; o->len = (int)n; @@ -648,7 +648,7 @@ static int from_base64(lua_State *L) { return 0; } int nlen = B64decoded_len(len); octet *o = o_new(L, nlen); // 4 byte header - SAFE(o); + OCT_frombase64(o, (char*)s); END(1); } @@ -663,7 +663,7 @@ static int from_url64(lua_State *L) { return 0; } int nlen = B64decoded_len(len); // func(L,"U64 decode len: %u -> %u",len,nlen); - octet *o = o_new(L, nlen); SAFE(o); + octet *o = o_new(L, nlen); o->len = U64decode(o->val, (char*)s); // func(L,"u64 return len: %u",o->len); END(1); @@ -714,7 +714,7 @@ static int from_string(lua_State *L) { zerror(L, "%s: invalid string size: %u", __func__, len); lerror(L, "operation aborted"); return 0; } - octet *o = o_new(L, len); SAFE(o); + octet *o = o_new(L, len); register int i = 0; for(i=0;s[i];i++) o->val[i]=s[i]; o->len = i; @@ -741,7 +741,7 @@ static int from_hex(lua_State *L) { zerror(L, "hex sequence too long: %u bytes", len<<1); // fatal lua_pushboolean(L, 0); END(1); } - octet *o = o_new(L, len>>1); SAFE(o); + octet *o = o_new(L, len>>1); if ( (s[0] == '0') && (s[1] == 'x') ) { // ethereum elides the leftmost 0 char when value <= 0F if((len&1)==1) { // odd length means elision @@ -772,7 +772,7 @@ static int from_bin(lua_State *L) { zerror(L, "invalid binary sequence size: %u", len); lerror(L, "operation aborted"); return 0; } - octet *o = o_new(L, len+4); SAFE(o); + octet *o = o_new(L, len+4); register char *S = (char*)s; register int p; // position in whole string register int i; // increased only when 1 or 0 is found @@ -829,7 +829,7 @@ static int from_segwit_address(lua_State *L) { lua_pushboolean(L, 0); END(1); } - octet *o = o_new(L, witprog_len); SAFE(o); + octet *o = o_new(L, witprog_len); register size_t i; for(i=0; ival[i] = (char)witprog[i]; @@ -920,7 +920,7 @@ static int to_segwit_address(lua_State *L) { static int to_mnemonic(lua_State *L) { BEGIN(); - octet *o = o_arg(L,1); SAFE(o); + octet *o = o_arg(L,1); if(!o->len) { lua_pushnil(L); o_free(L,o); return 1; } if(o->len > 32) { zerror(L, "%s :: octet bigger than 32 bytes cannot be encoded to mnemonic"); @@ -948,7 +948,7 @@ static int from_mnemonic(lua_State *L) { lua_pushboolean(L, 0); END(1); } // From bip39 it can be at most 32bytes - octet *o = o_alloc(L, 32); SAFE(o); + octet *o = o_alloc(L, 32); if(!mnemonic_check_and_bits(s, &(o->len), o->val)) { zerror(L, "%s :: words cannot be encoded with bip39 format", __func__); lua_pushboolean(L, 0); @@ -1183,7 +1183,7 @@ static int to_base58(lua_State *L) { static int to_base45 (lua_State *L) { BEGIN(); - octet *o = o_arg(L, 1); SAFE(o); + octet *o = o_arg(L, 1); int newlen = b45encode(NULL, o->val, o->len); char *b = malloc(newlen); b45encode(b, o->val, o->len); @@ -1203,7 +1203,7 @@ static int from_base45(lua_State *L) { lerror(L, "base45 string contains invalid characters"); return 0; } - octet *o = o_new(L, len); SAFE(o); + octet *o = o_new(L, len); len = b45decode(o->val, s); if(len < 0) { lerror(L, "base45 invalid string"); @@ -1315,7 +1315,7 @@ This is the default format when `print()` is used on an octet. */ int to_hex(lua_State *L) { BEGIN(); - octet *o = o_arg(L, 1); SAFE(o); + octet *o = o_arg(L,1); if(!o->len) { lua_pushnil(L); goto end; } push_octet_to_hex_string(L, o); end: @@ -1325,7 +1325,7 @@ int to_hex(lua_State *L) { static int to_bin(lua_State *L) { BEGIN(); - octet *o = o_arg(L,1); SAFE(o); + octet *o = o_arg(L,1); if(!o->len) { lua_pushnil(L); goto end; } char *s = malloc(o->len*8+2); int i; @@ -1396,7 +1396,7 @@ static int zero(lua_State *L) { return 0; } func(L, "Creating a zero filled octet of %u bytes", len); - octet *n = o_new(L,len); SAFE(n); + octet *n = o_new(L,len); register int i; for(i=0; ival[i]=0x0; n->len = len; @@ -1606,7 +1606,7 @@ static int eq(lua_State *L) { static int size(lua_State *L) { BEGIN(); - octet *o = o_arg(L, 1); SAFE(o); + octet *o = o_arg(L, 1); lua_pushinteger(L, o->len); o_free(L, o); END(1); @@ -1614,7 +1614,7 @@ static int size(lua_State *L) { static int max(lua_State *L) { BEGIN(); - octet *o = o_arg(L, 1); SAFE(o); + octet *o = o_arg(L, 1); lua_pushinteger(L, o->max); o_free(L, o); END(1); @@ -1623,8 +1623,8 @@ static int max(lua_State *L) { static int new_random(lua_State *L) { BEGIN(); int tn; - lua_Number n = lua_tonumberx(L, 1, &tn); SAFE(n); - octet *o = o_new(L,(int)n); SAFE(o); + lua_Number n = lua_tonumberx(L, 1, &tn); + octet *o = o_new(L,(int)n); Z(L); OCT_rand(o, Z->random_generator, (int)n); END(1); @@ -1707,7 +1707,7 @@ static int compact_ascii(lua_State *L) { static int entropy_bytefreq(lua_State *L) { BEGIN(); - octet *o = o_arg(L, 1); SAFE(o); + octet *o = o_arg(L, 1); register int i; // register // byte frequency table char *bfreq = malloc(0xff); @@ -1730,7 +1730,7 @@ static int entropy_bytefreq(lua_State *L) { static int entropy(lua_State *L) { BEGIN(); - octet *o = o_arg(L,1); SAFE(o); + octet *o = o_arg(L,1); register int i; // register // byte frequency table char *bfreq = malloc(0xff+0x0f); @@ -1859,7 +1859,7 @@ static int charcount(lua_State *L) { register int c; const char *s = lua_tostring(L, 2); luaL_argcheck(L, s != NULL, 1, "string expected"); - octet *o = o_arg(L,1); SAFE(o); + octet *o = o_arg(L,1); needle = *s; // single char const char *hay = (const char*)o->val; for(p=hay, c=0; c < o->len; p++, c++) if(needle==*p) count++; @@ -1932,7 +1932,7 @@ static int elide_at_start(lua_State *L) { if (i != prefix->len) { lua_pushnil(L); } else { - octet* res = o_new(L, o->len - prefix->len); SAFE(res); + octet* res = o_new(L, o->len - prefix->len); if (i < o->len) { memmove(res->val, o->val + i, o->len - i); res->len = o->len - prefix->len; @@ -1970,7 +1970,7 @@ static int fillrepeat(lua_State *L) { failed_msg = "size is not a positive number"; goto end; } - octet* res = o_new(L, size); SAFE(res); + octet* res = o_new(L, size); res->len = size; int i; for(i=0; ilen; i++) { @@ -1987,8 +1987,8 @@ static int fillrepeat(lua_State *L) { static int lesser_than(lua_State *L) { BEGIN(); - octet *l = o_arg(L,1); SAFE(l); - octet *r = o_arg(L,2); SAFE(r); + octet *l = o_arg(L,1); + octet *r = o_arg(L,2); size_t minlen = (l->len < r->len) ? l->len : r->len; if( memcmp(l->val,r->val,minlen) < 0 ) lua_pushboolean(L, 1); else lua_pushboolean(L, 0); diff --git a/src/zen_parse.c b/src/zen_parse.c index 607f6085d..ab553406e 100644 --- a/src/zen_parse.c +++ b/src/zen_parse.c @@ -39,7 +39,7 @@ static char low[MAX_LINE]; // 1KB max for a single zencode line static int lua_parse_prefix(lua_State* L) { const char *line; size_t size; - line = luaL_checklstring(L,1,&size); SAFE(line); + line = luaL_checklstring(L,1,&size); register unsigned short int c; unsigned short fspace = 0; // skip space in front @@ -71,8 +71,8 @@ static int lua_strcasecmp(lua_State *L) { const char *a, *b; size_t la, lb; char *ta, *tb; - a = luaL_checklstring(L,1,&la); SAFE(a); - b = luaL_checklstring(L,2,&lb); SAFE(b); + a = luaL_checklstring(L,1,&la); + b = luaL_checklstring(L,2,&lb); if(la>MAX_LINE) lerror(L, "strcasecmp: arg #1 MAX_LINE limit hit"); if(lb>MAX_LINE) lerror(L, "strcasecmp: arg #2 MAX_LINE limit hit"); ta = malloc(la+1); diff --git a/src/zen_qp.c b/src/zen_qp.c index a8d5bf425..f23c2dcb5 100644 --- a/src/zen_qp.c +++ b/src/zen_qp.c @@ -386,9 +386,9 @@ static int qp_signature_check(lua_State *L){ static int qp_kem_keygen(lua_State *L) { BEGIN(); lua_createtable(L, 0, 2); - octet *private = o_new(L, PQCLEAN_KYBER512_CLEAN_CRYPTO_SECRETKEYBYTES); SAFE(private); + octet *private = o_new(L, PQCLEAN_KYBER512_CLEAN_CRYPTO_SECRETKEYBYTES); lua_setfield(L, -2, "private"); - octet *public = o_new(L, PQCLEAN_KYBER512_CLEAN_CRYPTO_PUBLICKEYBYTES); SAFE(public); + octet *public = o_new(L, PQCLEAN_KYBER512_CLEAN_CRYPTO_PUBLICKEYBYTES); lua_setfield(L, -2, "public"); PQCLEAN_KYBER512_CLEAN_crypto_kem_keypair((unsigned char*)public->val, (unsigned char*)private->val); @@ -565,7 +565,7 @@ static int qp_ml_kem_512_keygen(lua_State *L) { for(int j=1; j<3; j++){ void *ud =luaL_testudata(L,j,"zenroom.octet"); if (ud){ - octet * rnd = (octet*) ud; SAFE(rnd); + octet * rnd = (octet*) ud; if (rnd->len != 32) { failed_msg = "Wrong seed size"; goto end; @@ -578,9 +578,9 @@ static int qp_ml_kem_512_keygen(lua_State *L) { } } lua_createtable(L, 0, 2); - octet *private = o_new(L, pqcrystals_ml_kem_512_ref_SECRETKEYBYTES); SAFE(private); + octet *private = o_new(L, pqcrystals_ml_kem_512_ref_SECRETKEYBYTES); lua_setfield(L, -2, "private"); - octet *public = o_new(L, pqcrystals_ml_kem_512_ref_PUBLICKEYBYTES); SAFE(public); + octet *public = o_new(L, pqcrystals_ml_kem_512_ref_PUBLICKEYBYTES); lua_setfield(L, -2, "public"); pqcrystals_ml_kem_512_ref_keypair_derand((unsigned char*)public->val, (unsigned char*)private->val, randbytes); @@ -677,7 +677,7 @@ static int qp_ml_kem_512_enc(lua_State *L) { pk = o_arg(L, 1); void *ud = luaL_testudata(L, 2, "zenroom.octet"); if (ud){ - octet *rnd = (octet *) ud; SAFE(rnd); + octet *rnd = (octet *) ud; if (rnd->len != 32) { failed_msg = "Wrong seed size"; goto end; @@ -979,7 +979,7 @@ static int ml_dsa_44_keypair(lua_State *L) { lua_setfield(L, -2, "public"); void *ud =luaL_testudata(L,1,"zenroom.octet"); if (ud){ - octet * rnd = (octet*) ud; SAFE(rnd); + octet * rnd = (octet*) ud; if (rnd->len != 32) { failed_msg = "Wrong seed size"; goto end; @@ -1083,7 +1083,7 @@ static int ml_dsa_44_signature(lua_State *L) { void *ud =luaL_testudata(L,3,"zenroom.octet"); if (ud){ - octet * ctx = (octet*) ud; SAFE(ctx); + octet * ctx = (octet*) ud; if (ctx->len > 255) { failed_msg = "Wrong ctx size"; goto end; @@ -1157,7 +1157,7 @@ static int ml_dsa_44_verify(lua_State *L) {/********************************* } void *ud =luaL_testudata(L,4,"zenroom.octet"); if (ud){ - octet * ctx = (octet*) ud; SAFE(ctx); + octet * ctx = (octet*) ud; int result = pqcrystals_ml_dsa_44_ref_verify((unsigned char*)sig->val, (size_t)sig->len, (unsigned char*)m->val, m->len, diff --git a/src/zen_random.c b/src/zen_random.c index f1b701e39..cc1d40d79 100644 --- a/src/zen_random.c +++ b/src/zen_random.c @@ -117,7 +117,7 @@ static int rng_int32(lua_State *L) { static int rng_seed(lua_State *L) { BEGIN(); Z(L); - octet *in = o_arg(L, 1); SAFE(in); + octet *in = o_arg(L, 1); if(in->len < 4) { zerror(L, "Random seed error: too small (%u bytes)", in->len); lua_pushnil(L); @@ -126,7 +126,7 @@ static int rng_seed(lua_State *L) { AMCL_(RAND_seed)(Z->random_generator, in->len, in->val); o_dup(L,in); // push seed to Lua stack for setglobal lua_setglobal(L, "RNGSEED"); - octet *rr = o_new(L, PRNG_PREROLL); SAFE(rr); + octet *rr = o_new(L, PRNG_PREROLL); for(register int i=0;ival[i] = RAND_byte(Z->random_generator); rr->len = PRNG_PREROLL; diff --git a/src/zen_rsa.c b/src/zen_rsa.c index 1014b1043..315204873 100644 --- a/src/zen_rsa.c +++ b/src/zen_rsa.c @@ -84,13 +84,13 @@ static int rsa_keypair(lua_State *L) { void* q =luaL_testudata(L,2,"zenroom.octet"); if ((p) && (q)){ octet *P = o_alloc(L, sizeof(p)); - P = (octet*) p; SAFE(P); + P = (octet*) p; if (P->len > RSA_4096_PRIVATE_KEY_BIG_BYTES) { failed_msg = "Wrong prime size"; goto end; } octet *Q = o_alloc(L, sizeof(q)); - Q = (octet*) q; SAFE(Q); + Q = (octet*) q; if (Q->len > RSA_4096_PRIVATE_KEY_BIG_BYTES) { failed_msg = "Wrong prime size"; goto end; @@ -109,13 +109,13 @@ static int rsa_keypair(lua_State *L) { void* q =luaL_testudata(L,3,"zenroom.octet"); if ((p) && (q)){ octet *P = o_alloc(L, sizeof(p)); - P = (octet*) p; SAFE(P); + P = (octet*) p; if (P->len > RSA_4096_PRIVATE_KEY_BIG_BYTES) { failed_msg = "Wrong prime size"; goto end; } octet *Q = o_alloc(L, sizeof(q)); - Q = (octet*) q; SAFE(Q); + Q = (octet*) q; if (Q->len > RSA_4096_PRIVATE_KEY_BIG_BYTES) { failed_msg = "Wrong prime size"; goto end; @@ -132,14 +132,14 @@ static int rsa_keypair(lua_State *L) { } lua_createtable(L, 0, 2); - octet *private = o_new(L, RSA_4096_PRIVATE_KEY_BYTES); SAFE(private); + octet *private = o_new(L, RSA_4096_PRIVATE_KEY_BYTES); if(private == NULL) { failed_msg = "Could not allocate private key"; goto end; } lua_setfield(L, -2, "private"); - octet *public = o_new(L, RSA_4096_PUBLIC_KEY_BYTES); SAFE(public); + octet *public = o_new(L, RSA_4096_PUBLIC_KEY_BYTES); if(public == NULL) { failed_msg = "Could not allocate public key"; goto end; diff --git a/src/zenroom.c b/src/zenroom.c index 5f8c01624..18da9f0c5 100644 --- a/src/zenroom.c +++ b/src/zenroom.c @@ -354,19 +354,10 @@ void zen_teardown(zenroom_t *ZZ) { free(ZZ); } -#define SAFE_EXEC \ - if(!ZZ) { \ - _err("Execution error: Zenroom is not initialized\n"); \ - return ERR_INIT; \ - } \ - if(!ZZ->lua) { \ - _err( "Execution error: Lua is not initialised\n"); \ - ZZ->exitcode = ERR_INIT; \ - return ZZ->exitcode; \ - } - +HEDLEY_NON_NULL(1,2) int zen_exec_zencode(zenroom_t *ZZ, const char *script) { - SAFE_EXEC; + HEDLEY_ASSUME(ZZ!=NULL); + HEDLEY_ASSUME(ZZ->lua!=NULL); int ret; lua_State* L = (lua_State*)ZZ->lua; // introspection on code being executed @@ -407,7 +398,8 @@ int protect_exec_lua(lua_State *L) { } int zen_exec_lua(zenroom_t *ZZ, const char *script) { - SAFE_EXEC; + HEDLEY_ASSUME(ZZ!=NULL); + HEDLEY_ASSUME(ZZ->lua!=NULL); lua_State *L = (lua_State*)ZZ->lua; // introspection on code being executed zen_setenv(L,"CODE",(char*)script);