Skip to content

Commit f47666a

Browse files
Added EIP-712 memory allocation cleanup
1 parent 98d418b commit f47666a

8 files changed

Lines changed: 90 additions & 8 deletions

File tree

src_features/signMessageEIP712/context_712.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ void eip712_context_deinit(void) {
6666
path_deinit();
6767
field_hash_deinit();
6868
ui_712_deinit();
69+
sol_typenames_deinit();
70+
app_mem_free(eip712_context);
6971
eip712_context = NULL;
7072
reset_app_context();
7173
}

src_features/signMessageEIP712/field_hash.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ bool field_hash_init(void) {
3838
* Deinitialize the field hash context
3939
*/
4040
void field_hash_deinit(void) {
41-
fh = NULL;
41+
if (fh != NULL) {
42+
app_mem_free(fh);
43+
fh = NULL;
44+
}
4245
}
4346

4447
/**

src_features/signMessageEIP712/path.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,13 @@ cx_sha3_t *get_last_hash_ctx(void) {
156156
return &((s_hash_ctx *) hash_ctx)->hash;
157157
}
158158

159+
// to be used as a \ref f_list_node_del
160+
static void delete_hash_ctx(s_hash_ctx *ctx) {
161+
app_mem_free(ctx);
162+
}
163+
159164
static void remove_last_hash_ctx(void) {
160-
flist_pop_back((s_flist_node **) &g_hash_ctxs, NULL);
165+
flist_pop_back((s_flist_node **) &g_hash_ctxs, (f_list_node_del) &delete_hash_ctx);
161166
}
162167

163168
/**
@@ -221,6 +226,7 @@ static bool push_new_hash_depth(bool init) {
221226
flist_push_back((s_flist_node **) &g_hash_ctxs, (s_flist_node *) hash_ctx);
222227
return true;
223228
end:
229+
app_mem_free(hash_ctx);
224230
return false;
225231
}
226232

@@ -788,5 +794,13 @@ bool path_init(void) {
788794
* De-initialize the path context
789795
*/
790796
void path_deinit(void) {
791-
path_struct = NULL;
797+
if (path_struct != NULL) {
798+
app_mem_free(path_struct);
799+
path_struct = NULL;
800+
}
801+
if (path_backup != NULL) {
802+
app_mem_free(path_backup);
803+
path_backup = NULL;
804+
}
805+
flist_clear((s_flist_node **) &g_hash_ctxs, (f_list_node_del) &delete_hash_ctx);
792806
}

src_features/signMessageEIP712/sol_typenames.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ bool sol_typenames_init(void) {
2222
uint8_t count = TYPES_COUNT - 1; // because 0 is custom (so not solidity)
2323

2424
if (g_sol_types != NULL) {
25-
g_sol_types = NULL;
25+
sol_typenames_deinit();
2626
return false;
2727
}
2828
if ((g_sol_types = app_mem_alloc(sizeof(*g_sol_types) * count)) == NULL) {
@@ -63,6 +63,16 @@ bool sol_typenames_init(void) {
6363
return true;
6464
}
6565

66+
void sol_typenames_deinit(void) {
67+
if (g_sol_types != NULL) {
68+
for (int i = 0; i < (TYPES_COUNT - 1); ++i) {
69+
app_mem_free(g_sol_types[i].name);
70+
}
71+
app_mem_free(g_sol_types);
72+
g_sol_types = NULL;
73+
}
74+
}
75+
6676
/**
6777
* Get typename from a given field
6878
*

src_features/signMessageEIP712/sol_typenames.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
#include "typed_data.h"
66

77
bool sol_typenames_init(void);
8+
void sol_typenames_deinit(void);
89
const char *get_struct_field_sol_typename(const s_struct_712_field *field_ptr);

src_features/signMessageEIP712/type_hash.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ static bool compare_struct_deps(const s_struct_dep *a, const s_struct_dep *b) {
139139
return true;
140140
}
141141

142+
// to be used as a \ref f_list_node_del
143+
static void delete_struct_dep(s_struct_dep *sdep) {
144+
app_mem_free(sdep);
145+
}
146+
142147
/**
143148
* Encode the structure's type and hash it
144149
*
@@ -175,6 +180,7 @@ bool type_hash(const char *struct_name, const uint8_t struct_name_length, uint8_
175180
}
176181
}
177182

183+
flist_clear((s_flist_node **) &deps, (f_list_node_del) &delete_struct_dep);
178184
// copy hash into memory
179185
CX_CHECK(cx_hash_no_throw((cx_hash_t *) &global_sha3,
180186
CX_LAST,

src_features/signMessageEIP712/typed_data.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,24 @@ bool typed_data_init(void) {
2020
return true;
2121
}
2222

23+
// to be used as a \ref f_list_node_del
24+
static void delete_field(s_struct_712_field *f) {
25+
if (f->type_name != NULL) app_mem_free(f->type_name);
26+
if (f->array_levels != NULL) app_mem_free(f->array_levels);
27+
if (f->key_name != NULL) app_mem_free(f->key_name);
28+
app_mem_free(f);
29+
}
30+
31+
// to be used as a \ref f_list_node_del
32+
static void delete_struct(s_struct_712 *s) {
33+
if (s->name != NULL) app_mem_free(s->name);
34+
if (s->fields != NULL)
35+
flist_clear((s_flist_node **) &s->fields, (f_list_node_del) &delete_field);
36+
app_mem_free(s);
37+
}
38+
2339
void typed_data_deinit(void) {
24-
g_structs = NULL;
40+
flist_clear((s_flist_node **) &g_structs, (f_list_node_del) &delete_struct);
2541
}
2642

2743
/**

src_features/signMessageEIP712/ui_logic.c

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,23 @@ typedef struct {
7171

7272
static t_ui_context *ui_ctx = NULL;
7373

74+
// to be used as a \ref f_list_node_del
75+
static void delete_filter_crc(s_filter_crc *fcrc) {
76+
app_mem_free(fcrc);
77+
}
78+
79+
// to be used as a \ref f_list_node_del
80+
static void delete_ui_pair(s_ui_712_pair *pair) {
81+
if (pair->key != NULL) app_mem_free(pair->key);
82+
if (pair->value != NULL) app_mem_free(pair->value);
83+
app_mem_free(pair);
84+
}
85+
86+
// to be used as a \ref f_list_node_del
87+
static void delete_amount_join(s_amount_join *join) {
88+
app_mem_free(join);
89+
}
90+
7491
/**
7592
* Checks on the UI context to determine if the next EIP 712 field should be shown
7693
*
@@ -485,7 +502,9 @@ static bool ui_712_format_amount_join(void) {
485502
}
486503
ui_ctx->field_flags |= UI_712_FIELD_SHOWN;
487504
ui_712_set_title(amount_join->name, strlen(amount_join->name));
488-
explicit_bzero(amount_join, sizeof(*amount_join));
505+
flist_remove((s_flist_node **) &ui_ctx->amount.joins,
506+
(s_flist_node *) amount_join,
507+
(f_list_node_del) delete_amount_join);
489508
return true;
490509
}
491510

@@ -730,7 +749,18 @@ bool ui_712_init(void) {
730749
* Deinit function that simply unsets the struct pointer to NULL
731750
*/
732751
void ui_712_deinit(void) {
733-
ui_ctx = NULL;
752+
if (ui_ctx != NULL) {
753+
app_mem_free(ui_ctx);
754+
if (ui_ctx->filters_crc != NULL)
755+
flist_clear((s_flist_node **) &ui_ctx->filters_crc,
756+
(f_list_node_del) &delete_filter_crc);
757+
if (ui_ctx->ui_pairs != NULL)
758+
flist_clear((s_flist_node **) &ui_ctx->ui_pairs, (f_list_node_del) &delete_ui_pair);
759+
if (ui_ctx->amount.joins != NULL)
760+
flist_clear((s_flist_node **) &ui_ctx->amount.joins,
761+
(f_list_node_del) &delete_amount_join);
762+
ui_ctx = NULL;
763+
}
734764
}
735765

736766
/**
@@ -1005,7 +1035,7 @@ void ui_712_delete_pairs(size_t keep) {
10051035
size = flist_size((s_flist_node **) &ui_ctx->ui_pairs);
10061036
if (size > 0) {
10071037
while (size > keep) {
1008-
flist_pop_front((s_flist_node **) &ui_ctx->ui_pairs, NULL);
1038+
flist_pop_front((s_flist_node **) &ui_ctx->ui_pairs, (f_list_node_del) &delete_ui_pair);
10091039
size -= 1;
10101040
}
10111041
}

0 commit comments

Comments
 (0)