From 5e70ea17365974d3f7c5caf1f5e65768538380bb Mon Sep 17 00:00:00 2001 From: pscott Date: Wed, 19 May 2021 16:05:56 +0200 Subject: [PATCH] Update uint256_to_decimal to right-align value --- doc/ethapp.asc | 1 + ethereum-plugin-sdk | 2 +- src/utils.c | 34 ++++++++++++++++++++++++++++------ src/utils.h | 2 +- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/doc/ethapp.asc b/doc/ethapp.asc index 698148d..1df3372 100644 --- a/doc/ethapp.asc +++ b/doc/ethapp.asc @@ -522,6 +522,7 @@ The following standard Status Words are returned for all APDUs - some specific S | 6501 | TransactionType not supported | 6502 | Output buffer too small for snprintf input | 6503 | Plugin error +| 6504 | Failed to convert from int256 | 6700 | Incorrect length | 6982 | Security status not satisfied (Canceled by user) | 6A80 | Invalid data diff --git a/ethereum-plugin-sdk b/ethereum-plugin-sdk index ff4873f..44bab0d 160000 --- a/ethereum-plugin-sdk +++ b/ethereum-plugin-sdk @@ -1 +1 @@ -Subproject commit ff4873f532e05562e03daa9285162ea8859c5027 +Subproject commit 44bab0dda2792fe35c9e0a252a01bd3b615d8523 diff --git a/src/utils.c b/src/utils.c index fe60b3c..8bdb76f 100644 --- a/src/utils.c +++ b/src/utils.c @@ -73,9 +73,26 @@ uint32_t u32_from_BE(uint8_t *in, uint8_t size, bool strict) { return res; } -bool uint256_to_decimal(const uint8_t *value, char *out, size_t out_len) { - uint16_t n[16]; - memcpy((uint8_t *) n, value, 32); +bool uint256_to_decimal(const uint8_t *value, size_t value_len, char *out, size_t out_len) { + if (value_len > INT256_LENGTH) { + // value len is bigger than INT256_LENGTH ?! + return false; + } + + uint16_t n[16] = {0}; + // Copy and right-align the number + memcpy((uint8_t *) n + INT256_LENGTH - value_len, value, INT256_LENGTH - value_len); + + // Special case when value is 0 + if (allzeroes(n, INT256_LENGTH)) { + if (out_len < 2) { + // Not enough space to hold "0" and \0. + return false; + } + strncpy(out, "0", out_len); + return true; + } + uint16_t *p = n; for (int i = 0; i < 16; i++) { n[i] = __builtin_bswap16(*p++); @@ -100,13 +117,18 @@ bool uint256_to_decimal(const uint8_t *value, char *out, size_t out_len) { } void amountToString(const uint8_t *amount, - uint8_t amount_size __attribute__((unused)), + uint8_t amount_size, uint8_t decimals, const char *ticker, char *out_buffer, uint8_t out_buffer_size) { - char tmp_buffer[100]; - uint256_to_decimal(amount, tmp_buffer, 100); + char tmp_buffer[100] = {0}; + + bool success = uint256_to_decimal(amount, amount_size, tmp_buffer, sizeof(tmp_buffer)); + + if (!success) { + THROW(0x6504); + } uint8_t amount_len = strnlen(tmp_buffer, sizeof(tmp_buffer)); uint8_t ticker_len = strnlen(ticker, MAX_TICKER_LEN); diff --git a/src/utils.h b/src/utils.h index fb40d92..17a59b5 100644 --- a/src/utils.h +++ b/src/utils.h @@ -32,7 +32,7 @@ int local_strchr(char* string, char ch); // throw if the size is > 4. uint32_t u32_from_BE(uint8_t* in, uint8_t size, bool strict); -bool uint256_to_decimal(const uint8_t* value, char* out, size_t out_len); +bool uint256_to_decimal(const uint8_t* value, size_t value_len, char* out, size_t out_len); void amountToString(const uint8_t* amount, uint8_t amount_len,