Skip to content

Commit

Permalink
fix: 🐛 free allocated octet in case of errors
Browse files Browse the repository at this point in the history
  • Loading branch information
matteo-cristino authored and jaromil committed Oct 19, 2023
1 parent 4e83451 commit d6a78b4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
17 changes: 11 additions & 6 deletions src/zen_big.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ static int lua_bigmax(lua_State *L) {
*/
static int newbig(lua_State *L) {
BEGIN();
char *failed_msg = NULL;
void *ud;
// kept for backward compat with zenroom 0.9
ud = luaL_testudata(L, 2, "zenroom.big");
Expand Down Expand Up @@ -362,19 +363,23 @@ static int newbig(lua_State *L) {
// octet argument, import
octet *o = o_arg(L, 1);
if(!o) {
zerror(L, "Could not allocate octet");
return 0;
failed_msg = "Could not allocate octet";
goto end;
}
if(o->len > MODBYTES) {
zerror(L, "Import of octet to BIG limit exceeded (%u > %u bytes)", o->len, MODBYTES);
return 0; }
failed_msg = "Import of octet to BIG limit exceeded";
goto end; }
big *c = big_new(L);
if(!c) {
zerror(L, "Could not allocate big");
return 0;
failed_msg = "Could not allocate big";
goto end;
}
_octet_to_big(L, c,o);
end:
o_free(L,o);
if(failed_msg) {
THROW(failed_msg);
}
END(1);
}

Expand Down
3 changes: 2 additions & 1 deletion src/zen_ecp.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ static int lua_new_ecp(lua_State *L) {
}
ecp *e = ecp_new(L); SAFE(e);
if(o->len == 2 && o->val[0] == SCHAR_MAX && o->val[1] == SCHAR_MAX) {
ECP_inf(&e->val); return 1; } // ECP Infinity
ECP_inf(&e->val);
goto end; } // ECP Infinity
if(o->len > e->totlen) { // quick and dirty safety
lua_pop(L, 1);
zerror(L, "Octet length %u instead of %u bytes", o->len, e->totlen);
Expand Down

0 comments on commit d6a78b4

Please sign in to comment.