Skip to content

Commit

Permalink
EIP-712 empty array handling fix
Browse files Browse the repository at this point in the history
  • Loading branch information
apaillier-ledger committed Mar 27, 2024
1 parent 71472e0 commit 2907c49
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src_features/signMessageEIP712/path.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ static bool path_update(bool skip_if_array, bool stop_at_array) {
while (struct_field_type(field_ptr) == TYPE_CUSTOM) {
if (((field_ptr == starting_field_ptr) && skip_if_array) ||
((field_ptr != starting_field_ptr) && stop_at_array)) {
if (struct_field_is_array(field_ptr)) {
if ((path_struct->array_depths[path_struct->array_depth_count - 1].index == 0) &&
struct_field_is_array(field_ptr)) {
break;
}
}
Expand All @@ -325,11 +326,14 @@ static bool path_update(bool skip_if_array, bool stop_at_array) {
if (push_new_hash_depth(true) == false) {
return false;
}
// get the struct typehash
if (type_hash(typename, typename_len, hash) == false) {
return false;

if (!path_struct->in_empty_array) {
// get the struct typehash
if (type_hash(typename, typename_len, hash) == false) {
return false;
}
feed_last_hash_depth(hash);
}
feed_last_hash_depth(hash);

// TODO: Find a better way to show inner structs in verbose mode when it might be
// an empty array of structs in which case we don't want to show it but the
Expand Down Expand Up @@ -461,6 +465,9 @@ bool path_new_array_depth(const uint8_t *const data, uint8_t length) {
}

array_size = *data;
if (array_size == 0) {
path_struct->in_empty_array = true;
}
if (!path_update(false, array_size > 0)) {
return false;
}
Expand Down Expand Up @@ -505,10 +512,11 @@ bool path_new_array_depth(const uint8_t *const data, uint8_t length) {
}
CX_CHECK(cx_keccak_init_no_throw(old_ctx, 256));
}
if (array_size == 0) {
if (path_struct->in_empty_array) {
do {
path_advance(false);
} while (path_struct->array_depth_count != array_depth_count_bak);
} while (path_struct->array_depth_count > array_depth_count_bak);
path_struct->in_empty_array = false;
}

return true;
Expand Down Expand Up @@ -638,6 +646,7 @@ bool path_init(void) {
apdu_response_code = APDU_RESPONSE_INSUFFICIENT_MEMORY;
} else {
path_struct->depth_count = 0;
path_struct->in_empty_array = false;
}
}
return path_struct != NULL;
Expand Down
1 change: 1 addition & 0 deletions src_features/signMessageEIP712/path.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ typedef struct {
s_array_depth array_depths[MAX_ARRAY_DEPTH];
const void *root_struct;
e_root_type root_type;
bool in_empty_array;
} s_path;

bool path_set_root(const char *const struct_name, uint8_t length);
Expand Down

0 comments on commit 2907c49

Please sign in to comment.