Clean up mem allocation alignment macro usage + small refactoring of the typehash dependencies search
This commit is contained in:
@@ -22,7 +22,7 @@ bool eip712_context_init(void)
|
||||
// init global variables
|
||||
mem_init();
|
||||
|
||||
if ((eip712_context = MEM_ALLOC_AND_ALIGN_TO_TYPE(sizeof(*eip712_context), *eip712_context)) == NULL)
|
||||
if ((eip712_context = MEM_ALLOC_AND_ALIGN_TYPE(*eip712_context)) == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ bool field_hash_init(void)
|
||||
{
|
||||
if (fh == NULL)
|
||||
{
|
||||
if ((fh = MEM_ALLOC_AND_ALIGN_TO_TYPE(sizeof(*fh), *fh)) == NULL)
|
||||
if ((fh = MEM_ALLOC_AND_ALIGN_TYPE(*fh)) == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define MEM_ALLOC_AND_ALIGN_TO_TYPE(size, type) (mem_alloc_and_align(size, __alignof__(type)))
|
||||
#define MEM_ALLOC_AND_ALIGN_TYPE(type) mem_alloc_and_align(sizeof(type), __alignof__(type))
|
||||
|
||||
char *mem_alloc_and_copy_char(char c);
|
||||
void *mem_alloc_and_copy(const void *data, size_t size);
|
||||
|
||||
@@ -359,7 +359,7 @@ bool path_set_root(const char *const struct_name, uint8_t name_length)
|
||||
// TODO: Move elsewhere
|
||||
cx_sha3_t *hash_ctx;
|
||||
const uint8_t *thash_ptr;
|
||||
if ((hash_ctx = MEM_ALLOC_AND_ALIGN_TO_TYPE(sizeof(*hash_ctx), *hash_ctx)) == NULL)
|
||||
if ((hash_ctx = MEM_ALLOC_AND_ALIGN_TYPE(*hash_ctx)) == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -638,7 +638,7 @@ bool path_init(void)
|
||||
{
|
||||
if (path_struct == NULL)
|
||||
{
|
||||
path_struct = MEM_ALLOC_AND_ALIGN_TO_TYPE(sizeof(*path_struct), *path_struct);
|
||||
path_struct = MEM_ALLOC_AND_ALIGN_TYPE(*path_struct);
|
||||
}
|
||||
return path_struct != NULL;
|
||||
}
|
||||
|
||||
@@ -85,10 +85,10 @@ static bool encode_and_hash_type(const void *const struct_ptr)
|
||||
* @param[in,out] deps pointer to the first dependency pointer
|
||||
*/
|
||||
static void sort_dependencies(uint8_t deps_count,
|
||||
void **deps)
|
||||
const void **deps)
|
||||
{
|
||||
bool changed;
|
||||
void *tmp_ptr;
|
||||
const void *tmp_ptr;
|
||||
const char *name1, *name2;
|
||||
uint8_t namelen1, namelen2;
|
||||
int str_cmp_result;
|
||||
@@ -120,13 +120,13 @@ static void sort_dependencies(uint8_t deps_count,
|
||||
*
|
||||
* @param[in] structs_array pointer to structs array
|
||||
* @param[out] deps_count count of how many struct dependencie pointers
|
||||
* @param[in] deps pointer to the first dependency pointer
|
||||
* @param[in] first_dep pointer to the first dependency pointer
|
||||
* @param[in] struct_ptr pointer to the struct we are getting the dependencies of
|
||||
* @return \ref false in case of a memory allocation error, \ref true otherwise
|
||||
*/
|
||||
static bool get_struct_dependencies(const void *const structs_array,
|
||||
static const void **get_struct_dependencies(const void *const structs_array,
|
||||
uint8_t *const deps_count,
|
||||
void *const *const deps,
|
||||
const void **first_dep,
|
||||
const void *const struct_ptr)
|
||||
{
|
||||
uint8_t fields_count;
|
||||
@@ -151,7 +151,7 @@ static bool get_struct_dependencies(const void *const structs_array,
|
||||
for (dep_idx = 0; dep_idx < *deps_count; ++dep_idx)
|
||||
{
|
||||
// it's a match!
|
||||
if (*(deps + dep_idx) == arg_struct_ptr)
|
||||
if (*(first_dep + dep_idx) == arg_struct_ptr)
|
||||
{
|
||||
break;
|
||||
}
|
||||
@@ -159,18 +159,23 @@ static bool get_struct_dependencies(const void *const structs_array,
|
||||
// if it's not present in the array, add it and recurse into it
|
||||
if (dep_idx == *deps_count)
|
||||
{
|
||||
if ((new_dep = mem_alloc(sizeof(void*))) == NULL)
|
||||
*deps_count += 1;
|
||||
if ((new_dep = MEM_ALLOC_AND_ALIGN_TYPE(void*)) == NULL)
|
||||
{
|
||||
return false;
|
||||
return NULL;
|
||||
}
|
||||
if (*deps_count == 1)
|
||||
{
|
||||
first_dep = new_dep;
|
||||
}
|
||||
*new_dep = arg_struct_ptr;
|
||||
*deps_count += 1;
|
||||
get_struct_dependencies(structs_array, deps_count, deps, arg_struct_ptr);
|
||||
// TODO: Move away from recursive calls
|
||||
get_struct_dependencies(structs_array, deps_count, first_dep, arg_struct_ptr);
|
||||
}
|
||||
}
|
||||
field_ptr = get_next_struct_field(field_ptr);
|
||||
}
|
||||
return true;
|
||||
return first_dep;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -190,17 +195,13 @@ const uint8_t *type_hash(const void *const structs_array,
|
||||
struct_name,
|
||||
struct_name_length);
|
||||
uint8_t deps_count = 0;
|
||||
void **deps;
|
||||
const void **deps;
|
||||
uint8_t *hash_ptr;
|
||||
void *mem_loc_bak = mem_alloc(0);
|
||||
|
||||
cx_keccak_init(&global_sha3, 256); // init hash
|
||||
// get list of structs (own + dependencies), properly ordered
|
||||
if ((deps = MEM_ALLOC_AND_ALIGN_TO_TYPE(0, *deps)) == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
if (get_struct_dependencies(structs_array, &deps_count, deps, struct_ptr) == false)
|
||||
deps = get_struct_dependencies(structs_array, &deps_count, NULL, struct_ptr);
|
||||
if ((deps_count > 0) && (deps == NULL))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -352,7 +352,7 @@ void ui_712_end_sign(void)
|
||||
*/
|
||||
bool ui_712_init(void)
|
||||
{
|
||||
if ((ui_ctx = MEM_ALLOC_AND_ALIGN_TO_TYPE(sizeof(*ui_ctx), *ui_ctx)))
|
||||
if ((ui_ctx = MEM_ALLOC_AND_ALIGN_TYPE(*ui_ctx)))
|
||||
{
|
||||
ui_ctx->shown = false;
|
||||
ui_ctx->end_reached = false;
|
||||
|
||||
Reference in New Issue
Block a user