Skip to content

Commit

Permalink
fix: compile with clang
Browse files Browse the repository at this point in the history
more safety checks upgraded to use hedley
  • Loading branch information
jaromil committed Jan 8, 2025
1 parent c780601 commit 32ba424
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 90 deletions.
4 changes: 2 additions & 2 deletions src/lua_modules.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
13 changes: 6 additions & 7 deletions src/zen_big.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions src/zen_big.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/zen_ecdh.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/zen_ecp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)");
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions src/zen_ecp.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#define __ZEN_ECP_H__

#include <zen_ecp_factory.h>
#include <hedley.h>

typedef struct {
size_t halflen; // length in bytes of a reduced coordinate
Expand Down
8 changes: 4 additions & 4 deletions src/zen_ecp2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions src/zen_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/zen_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 32ba424

Please sign in to comment.