Use snprintf instead of u32_to_str
This commit is contained in:
39
src/utils.c
39
src/utils.c
@@ -72,45 +72,6 @@ uint32_t u32_from_BE(uint8_t *in, uint8_t size, bool strict) {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Converts a uint32_t to a string.
|
|
||||||
void u32_to_str(char *dest, uint32_t in, uint8_t dest_size) {
|
|
||||||
uint8_t i = 0;
|
|
||||||
|
|
||||||
// Get the first digit (in case it's 0).
|
|
||||||
dest[i] = in % 10 + '0';
|
|
||||||
in /= 10;
|
|
||||||
i++;
|
|
||||||
|
|
||||||
// Get every digit.
|
|
||||||
while (in != 0) {
|
|
||||||
if (i >= dest_size) {
|
|
||||||
THROW(6502);
|
|
||||||
}
|
|
||||||
dest[i] = in % 10 + '0';
|
|
||||||
in /= 10;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Null terminate the string.
|
|
||||||
dest[i] = '\0';
|
|
||||||
i--;
|
|
||||||
|
|
||||||
// Reverse the string
|
|
||||||
uint8_t end = i;
|
|
||||||
char tmp;
|
|
||||||
i = 0;
|
|
||||||
while (i < end) {
|
|
||||||
// Swap the first and last elements.
|
|
||||||
tmp = dest[i];
|
|
||||||
dest[i] = dest[end];
|
|
||||||
dest[end] = tmp;
|
|
||||||
|
|
||||||
// Decrease the interval size.
|
|
||||||
i++;
|
|
||||||
end--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void amountToString(uint8_t *amount,
|
void amountToString(uint8_t *amount,
|
||||||
uint8_t amount_size,
|
uint8_t amount_size,
|
||||||
uint8_t decimals,
|
uint8_t decimals,
|
||||||
|
|||||||
@@ -28,9 +28,6 @@ void convertUint256BE(uint8_t* data, uint32_t length, uint256_t* target);
|
|||||||
|
|
||||||
int local_strchr(char* string, char ch);
|
int local_strchr(char* string, char ch);
|
||||||
|
|
||||||
// `itoa` for uint32_t.
|
|
||||||
void u32_to_str(char* dest, uint8_t dest_size, uint32_t in);
|
|
||||||
|
|
||||||
// Converts a list of bytes (in BE) of length `size` to a uint32_t. `strict` will make the function
|
// Converts a list of bytes (in BE) of length `size` to a uint32_t. `strict` will make the function
|
||||||
// throw if the size is > 4.
|
// throw if the size is > 4.
|
||||||
uint32_t u32_from_BE(uint8_t* in, uint8_t size, bool strict);
|
uint32_t u32_from_BE(uint8_t* in, uint8_t size, bool strict);
|
||||||
|
|||||||
@@ -389,7 +389,13 @@ void finalizeParsing(bool direct) {
|
|||||||
if (genericUI) {
|
if (genericUI) {
|
||||||
if (txContext.txType == LEGACY) {
|
if (txContext.txType == LEGACY) {
|
||||||
uint32_t id = u32_from_BE(txContext.content->v, txContext.content->vLength, true);
|
uint32_t id = u32_from_BE(txContext.content->v, txContext.content->vLength, true);
|
||||||
u32_to_str((char *) strings.common.chainID, sizeof(strings.common.chainID), id);
|
uint8_t res =
|
||||||
|
snprintf(strings.common.chainID, sizeof(strings.common.chainID), "%u", id);
|
||||||
|
if (res >= sizeof(strings.common.chainID)) {
|
||||||
|
// If the return value is higher or equal to the size passed in as parameter, then
|
||||||
|
// the output was truncated. Return the appropriate error code.
|
||||||
|
THROW(0x6502);
|
||||||
|
}
|
||||||
} else if (txContext.txType == EIP2930) {
|
} else if (txContext.txType == EIP2930) {
|
||||||
uint256_t chainID;
|
uint256_t chainID;
|
||||||
convertUint256BE(tmpContent.txContent.chainID.value,
|
convertUint256BE(tmpContent.txContent.chainID.value,
|
||||||
|
|||||||
Reference in New Issue
Block a user