From 161919c0120999d6a740a52ebcdbea275f6a3e8d Mon Sep 17 00:00:00 2001 From: Alexandre Paillier Date: Thu, 24 Mar 2022 15:44:18 +0100 Subject: [PATCH] Simpler computing the string's length in memory --- src_features/signMessageEIP712/entrypoint.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src_features/signMessageEIP712/entrypoint.c b/src_features/signMessageEIP712/entrypoint.c index c70c56b..c15872d 100644 --- a/src_features/signMessageEIP712/entrypoint.c +++ b/src_features/signMessageEIP712/entrypoint.c @@ -376,14 +376,11 @@ void get_struct_type_string(const uint8_t *const ptr, // set length typestr_length = mem_alloc(sizeof(uint16_t)); - *typestr_length = 0; // add name typestr = memmove(mem_alloc(struct_name_length), struct_name, struct_name_length); - *typestr_length += struct_name_length; *(char*)mem_alloc(sizeof(char)) = '('; - *typestr_length += 1; field_ptr = get_struct_fields_array(struct_ptr, &fields_count); for (uint8_t idx = 0; idx < fields_count; ++idx) @@ -391,13 +388,11 @@ void get_struct_type_string(const uint8_t *const ptr, if (idx > 0) { *(char*)mem_alloc(sizeof(char)) = ','; - *typestr_length += 1; } name = get_struct_field_typename(field_ptr, &length); memmove(mem_alloc(sizeof(char) * length), name, length); - *typestr_length += length; if (struct_field_has_typesize(field_ptr)) { @@ -415,7 +410,7 @@ void get_struct_type_string(const uint8_t *const ptr, break; } // max value = 256, 3 characters max - *typestr_length += format_uint_into_mem(field_size, 3); + format_uint_into_mem(field_size, 3); } if (struct_field_is_array(field_ptr)) @@ -438,21 +433,22 @@ void get_struct_type_string(const uint8_t *const ptr, break; } *(char*)mem_alloc(sizeof(char)) = ']'; - *typestr_length += 1; lvl_ptr = get_next_struct_field_array_lvl(lvl_ptr); } } *(char*)mem_alloc(sizeof(char)) = ' '; - *typestr_length += 1; name = get_struct_field_keyname(field_ptr, &length); memmove(mem_alloc(sizeof(char) * length), name, length); - *typestr_length += length; field_ptr = get_next_struct_field(field_ptr); } *(char*)mem_alloc(sizeof(char)) = ')'; - *typestr_length += 1; + + // - 2 since : + // * typestr_length is one byte before the start of the string + // * mem_alloc's return will point to one byte after the end of the string + *typestr_length = (mem_alloc(0) - (void*)typestr_length - 2); // FIXME: DBG fwrite(typestr, sizeof(char), *typestr_length, stdout);