Fix all the defects identified by Clang Static Analyzer

Some fixes are dirty, but it is a start to enforce scan-build on
every commit.

Signed-off-by: pscott <scott.piriou@ledger.fr>
This commit is contained in:
Jean-Baptiste Bédrune
2021-07-16 12:04:39 +02:00
committed by pscott
parent c27d59d792
commit cd78581ffd
15 changed files with 158 additions and 144 deletions

View File

@@ -94,7 +94,9 @@ eth_plugin_result_t eth_plugin_perform_init(uint8_t *contractAddress,
if (memcmp(init->selector, (const void *) PIC(selectors[j]), SELECTOR_SIZE) == 0) { if (memcmp(init->selector, (const void *) PIC(selectors[j]), SELECTOR_SIZE) == 0) {
if ((INTERNAL_ETH_PLUGINS[i].availableCheck == NULL) || if ((INTERNAL_ETH_PLUGINS[i].availableCheck == NULL) ||
((PluginAvailableCheck) PIC(INTERNAL_ETH_PLUGINS[i].availableCheck))()) { ((PluginAvailableCheck) PIC(INTERNAL_ETH_PLUGINS[i].availableCheck))()) {
strcpy(dataContext.tokenContext.pluginName, INTERNAL_ETH_PLUGINS[i].alias); strlcpy(dataContext.tokenContext.pluginName,
INTERNAL_ETH_PLUGINS[i].alias,
PLUGIN_ID_LENGTH);
dataContext.tokenContext.pluginStatus = ETH_PLUGIN_RESULT_OK; dataContext.tokenContext.pluginStatus = ETH_PLUGIN_RESULT_OK;
contractAddress = NULL; contractAddress = NULL;
break; break;

View File

@@ -36,7 +36,7 @@ bool copy_transaction_parameters(create_transaction_parameters_t* sign_transacti
sizeof(stack_data.fullAmount)); sizeof(stack_data.fullAmount));
// If the amount is a fee, its value is nominated in ETH even if we're doing an ERC20 swap // If the amount is a fee, its value is nominated in ETH even if we're doing an ERC20 swap
strcpy(ticker, config->coinName); strlcpy(ticker, config->coinName, MAX_TICKER_LEN);
decimals = WEI_TO_ETHER; decimals = WEI_TO_ETHER;
amountToString(sign_transaction_params->fee_amount, amountToString(sign_transaction_params->fee_amount,
sign_transaction_params->fee_amount_length, sign_transaction_params->fee_amount_length,

View File

@@ -81,7 +81,7 @@ void stark_get_amount_string(uint8_t *contractAddress,
mul256(&amountPre, &quantum, &amount); mul256(&amountPre, &quantum, &amount);
tostring256(&amount, 10, tmp100, 100); tostring256(&amount, 10, tmp100, 100);
PRINTF("stark_get_amount_string - mul256 %s\n", tmp100); PRINTF("stark_get_amount_string - mul256 %s\n", tmp100);
strcpy(target100, ticker); strlcpy(target100, ticker, 100);
adjustDecimals(tmp100, strlen(tmp100), target100 + strlen(ticker), 100, decimals); adjustDecimals(tmp100, strlen(tmp100), target100 + strlen(ticker), 100, decimals);
PRINTF("get_amount_string %s\n", target100); PRINTF("get_amount_string %s\n", target100);
} }

View File

@@ -28,7 +28,7 @@ typedef struct tokenDefinition_t {
uint8_t contractName[ADDRESS_LENGTH]; uint8_t contractName[ADDRESS_LENGTH];
#endif #endif
uint8_t address[ADDRESS_LENGTH]; uint8_t address[ADDRESS_LENGTH];
uint8_t ticker[MAX_TICKER_LEN]; char ticker[MAX_TICKER_LEN];
uint8_t decimals; uint8_t decimals;
} tokenDefinition_t; } tokenDefinition_t;

View File

@@ -132,11 +132,13 @@ UX_FLOW(ux_settings_flow,
&ux_settings_flow_4_step); &ux_settings_flow_4_step);
void display_settings(const ux_flow_step_t* const start_step) { void display_settings(const ux_flow_step_t* const start_step) {
strcpy(strings.common.fullAddress, (N_storage.dataAllowed ? "Allowed" : "NOT Allowed")); strlcpy(strings.common.fullAddress, (N_storage.dataAllowed ? "Allowed" : "NOT Allowed"), 12);
strcpy(strings.common.fullAddress + 12, strlcpy(strings.common.fullAddress + 12,
(N_storage.contractDetails ? "Displayed" : "NOT Displayed")); (N_storage.contractDetails ? "Displayed" : "NOT Displayed"),
strcpy(strings.common.fullAddress + 26, 26 - 12);
(N_storage.displayNonce ? "Displayed" : "NOT Displayed")); strlcpy(strings.common.fullAddress + 26,
(N_storage.displayNonce ? "Displayed" : "NOT Displayed"),
sizeof(strings.common.fullAddress) - 26);
ux_flow_init(0, ux_settings_flow, start_step); ux_flow_init(0, ux_settings_flow, start_step);
} }

View File

@@ -4,7 +4,6 @@
unsigned int io_seproxyhal_touch_signMessage_ok(__attribute__((unused)) const bagl_element_t *e) { unsigned int io_seproxyhal_touch_signMessage_ok(__attribute__((unused)) const bagl_element_t *e) {
uint8_t privateKeyData[INT256_LENGTH]; uint8_t privateKeyData[INT256_LENGTH];
uint8_t signature[100]; uint8_t signature[100];
uint8_t signatureLength;
cx_ecfp_private_key_t privateKey; cx_ecfp_private_key_t privateKey;
uint32_t tx = 0; uint32_t tx = 0;
io_seproxyhal_io_heartbeat(); io_seproxyhal_io_heartbeat();
@@ -18,14 +17,14 @@ unsigned int io_seproxyhal_touch_signMessage_ok(__attribute__((unused)) const ba
explicit_bzero(privateKeyData, sizeof(privateKeyData)); explicit_bzero(privateKeyData, sizeof(privateKeyData));
unsigned int info = 0; unsigned int info = 0;
io_seproxyhal_io_heartbeat(); io_seproxyhal_io_heartbeat();
signatureLength = cx_ecdsa_sign(&privateKey, cx_ecdsa_sign(&privateKey,
CX_RND_RFC6979 | CX_LAST, CX_RND_RFC6979 | CX_LAST,
CX_SHA256, CX_SHA256,
tmpCtx.messageSigningContext.hash, tmpCtx.messageSigningContext.hash,
sizeof(tmpCtx.messageSigningContext.hash), sizeof(tmpCtx.messageSigningContext.hash),
signature, signature,
sizeof(signature), sizeof(signature),
&info); &info);
explicit_bzero(&privateKey, sizeof(privateKey)); explicit_bzero(&privateKey, sizeof(privateKey));
G_io_apdu_buffer[0] = 27; G_io_apdu_buffer[0] = 27;
if (info & CX_ECCINFO_PARITY_ODD) { if (info & CX_ECCINFO_PARITY_ODD) {

View File

@@ -8,7 +8,6 @@ unsigned int io_seproxyhal_touch_signMessage712_v0_ok(__attribute__((unused))
uint8_t privateKeyData[INT256_LENGTH]; uint8_t privateKeyData[INT256_LENGTH];
uint8_t hash[INT256_LENGTH]; uint8_t hash[INT256_LENGTH];
uint8_t signature[100]; uint8_t signature[100];
uint8_t signatureLength;
cx_ecfp_private_key_t privateKey; cx_ecfp_private_key_t privateKey;
uint32_t tx = 0; uint32_t tx = 0;
io_seproxyhal_io_heartbeat(); io_seproxyhal_io_heartbeat();
@@ -43,14 +42,14 @@ unsigned int io_seproxyhal_touch_signMessage712_v0_ok(__attribute__((unused))
explicit_bzero(privateKeyData, sizeof(privateKeyData)); explicit_bzero(privateKeyData, sizeof(privateKeyData));
unsigned int info = 0; unsigned int info = 0;
io_seproxyhal_io_heartbeat(); io_seproxyhal_io_heartbeat();
signatureLength = cx_ecdsa_sign(&privateKey, cx_ecdsa_sign(&privateKey,
CX_RND_RFC6979 | CX_LAST, CX_RND_RFC6979 | CX_LAST,
CX_SHA256, CX_SHA256,
hash, hash,
sizeof(hash), sizeof(hash),
signature, signature,
sizeof(signature), sizeof(signature),
&info); &info);
explicit_bzero(&privateKey, sizeof(privateKey)); explicit_bzero(&privateKey, sizeof(privateKey));
G_io_apdu_buffer[0] = 27; G_io_apdu_buffer[0] = 27;
if (info & CX_ECCINFO_PARITY_ODD) { if (info & CX_ECCINFO_PARITY_ODD) {

View File

@@ -171,7 +171,7 @@ void to_uppercase(char *str, unsigned char size) {
} }
} }
void compareOrCopy(char *preapproved_string, char *parsed_string, bool silent_mode) { void compareOrCopy(char *preapproved_string, size_t size, char *parsed_string, bool silent_mode) {
if (silent_mode) { if (silent_mode) {
/* ETH address are not fundamentally case sensitive but might /* ETH address are not fundamentally case sensitive but might
have some for checksum purpose, so let's get rid of these diffs */ have some for checksum purpose, so let's get rid of these diffs */
@@ -181,7 +181,7 @@ void compareOrCopy(char *preapproved_string, char *parsed_string, bool silent_mo
THROW(ERR_SILENT_MODE_CHECK_FAILED); THROW(ERR_SILENT_MODE_CHECK_FAILED);
} }
} else { } else {
strcpy(preapproved_string, parsed_string); strlcpy(preapproved_string, parsed_string, size);
} }
} }
@@ -358,7 +358,7 @@ void finalizeParsing(bool direct) {
tmpContent.txContent.destinationLength = 20; tmpContent.txContent.destinationLength = 20;
if (pluginProvideToken.token1 != NULL) { if (pluginProvideToken.token1 != NULL) {
decimals = pluginProvideToken.token1->decimals; decimals = pluginProvideToken.token1->decimals;
ticker = (char *) pluginProvideToken.token1->ticker; ticker = pluginProvideToken.token1->ticker;
} }
break; break;
default: default:
@@ -388,7 +388,10 @@ void finalizeParsing(bool direct) {
displayBuffer + 2, displayBuffer + 2,
&global_sha3, &global_sha3,
chainConfig); chainConfig);
compareOrCopy(strings.common.fullAddress, displayBuffer, called_from_swap); compareOrCopy(strings.common.fullAddress,
sizeof(strings.common.fullAddress),
displayBuffer,
called_from_swap);
} else { } else {
strcpy(strings.common.fullAddress, "Contract"); strcpy(strings.common.fullAddress, "Contract");
} }
@@ -398,10 +401,13 @@ void finalizeParsing(bool direct) {
amountToString(tmpContent.txContent.value.value, amountToString(tmpContent.txContent.value.value,
tmpContent.txContent.value.length, tmpContent.txContent.value.length,
decimals, decimals,
(char *) ticker, ticker,
displayBuffer, displayBuffer,
sizeof(displayBuffer)); sizeof(displayBuffer));
compareOrCopy(strings.common.fullAmount, displayBuffer, called_from_swap); compareOrCopy(strings.common.fullAmount,
sizeof(strings.common.fullAddress),
displayBuffer,
called_from_swap);
} }
// Prepare nonce to display // Prepare nonce to display
if (genericUI) { if (genericUI) {
@@ -415,7 +421,10 @@ void finalizeParsing(bool direct) {
// Compute maximum fee // Compute maximum fee
if (genericUI) { if (genericUI) {
computeFees(displayBuffer, sizeof(displayBuffer)); computeFees(displayBuffer, sizeof(displayBuffer));
compareOrCopy(strings.common.maxFee, displayBuffer, called_from_swap); compareOrCopy(strings.common.maxFee,
sizeof(strings.common.maxFee),
displayBuffer,
called_from_swap);
} }
// Prepare chainID field // Prepare chainID field

View File

@@ -5,7 +5,6 @@
unsigned int io_seproxyhal_touch_tx_ok(__attribute__((unused)) const bagl_element_t *e) { unsigned int io_seproxyhal_touch_tx_ok(__attribute__((unused)) const bagl_element_t *e) {
uint8_t privateKeyData[INT256_LENGTH]; uint8_t privateKeyData[INT256_LENGTH];
uint8_t signature[100]; uint8_t signature[100];
uint8_t signatureLength;
cx_ecfp_private_key_t privateKey; cx_ecfp_private_key_t privateKey;
uint32_t tx = 0; uint32_t tx = 0;
uint32_t v = u32_from_BE(tmpContent.txContent.v, tmpContent.txContent.vLength, true); uint32_t v = u32_from_BE(tmpContent.txContent.v, tmpContent.txContent.vLength, true);
@@ -19,14 +18,14 @@ unsigned int io_seproxyhal_touch_tx_ok(__attribute__((unused)) const bagl_elemen
explicit_bzero(privateKeyData, sizeof(privateKeyData)); explicit_bzero(privateKeyData, sizeof(privateKeyData));
unsigned int info = 0; unsigned int info = 0;
io_seproxyhal_io_heartbeat(); io_seproxyhal_io_heartbeat();
signatureLength = cx_ecdsa_sign(&privateKey, cx_ecdsa_sign(&privateKey,
CX_RND_RFC6979 | CX_LAST, CX_RND_RFC6979 | CX_LAST,
CX_SHA256, CX_SHA256,
tmpCtx.transactionContext.hash, tmpCtx.transactionContext.hash,
sizeof(tmpCtx.transactionContext.hash), sizeof(tmpCtx.transactionContext.hash),
signature, signature,
sizeof(signature), sizeof(signature),
&info); &info);
explicit_bzero(&privateKey, sizeof(privateKey)); explicit_bzero(&privateKey, sizeof(privateKey));
// Parity is present in the sequence tag in the legacy API // Parity is present in the sequence tag in the legacy API
if (tmpContent.txContent.vLength == 0) { if (tmpContent.txContent.vLength == 0) {

View File

@@ -30,7 +30,7 @@ static const uint8_t COMPOUND_EXPECTED_DATA_SIZE[] = {
typedef struct compound_parameters_t { typedef struct compound_parameters_t {
uint8_t selectorIndex; uint8_t selectorIndex;
uint8_t amount[32]; uint8_t amount[32];
uint8_t ticker_1[MAX_TICKER_LEN]; char ticker_1[MAX_TICKER_LEN];
uint8_t decimals; uint8_t decimals;
} compound_parameters_t; } compound_parameters_t;
@@ -153,15 +153,15 @@ void compound_plugin_call(int message, void *parameters) {
compound_parameters_t *context = (compound_parameters_t *) msg->pluginContext; compound_parameters_t *context = (compound_parameters_t *) msg->pluginContext;
PRINTF("compound plugin provide token: %d\n", (msg->token1 != NULL)); PRINTF("compound plugin provide token: %d\n", (msg->token1 != NULL));
if (msg->token1 != NULL) { if (msg->token1 != NULL) {
strcpy((char *) context->ticker_1, (char *) msg->token1->ticker); strlcpy(context->ticker_1, msg->token1->ticker, MAX_TICKER_LEN);
switch (context->selectorIndex) { switch (context->selectorIndex) {
case COMPOUND_REDEEM_UNDERLYING: case COMPOUND_REDEEM_UNDERLYING:
case COMPOUND_MINT: case COMPOUND_MINT:
case CETH_MINT: case CETH_MINT:
msg->result = get_underlying_asset_decimals((char *) &context->ticker_1, msg->result =
&context->decimals) get_underlying_asset_decimals(context->ticker_1, &context->decimals)
? ETH_PLUGIN_RESULT_OK ? ETH_PLUGIN_RESULT_OK
: ETH_PLUGIN_RESULT_FALLBACK; : ETH_PLUGIN_RESULT_FALLBACK;
break; break;
// Only case where we use the compound contract decimals // Only case where we use the compound contract decimals
@@ -182,22 +182,22 @@ void compound_plugin_call(int message, void *parameters) {
case ETH_PLUGIN_QUERY_CONTRACT_ID: { case ETH_PLUGIN_QUERY_CONTRACT_ID: {
ethQueryContractID_t *msg = (ethQueryContractID_t *) parameters; ethQueryContractID_t *msg = (ethQueryContractID_t *) parameters;
compound_parameters_t *context = (compound_parameters_t *) msg->pluginContext; compound_parameters_t *context = (compound_parameters_t *) msg->pluginContext;
strcpy(msg->name, "Type"); strlcpy(msg->name, "Type", msg->nameLength);
switch (context->selectorIndex) { switch (context->selectorIndex) {
case COMPOUND_REDEEM_UNDERLYING: case COMPOUND_REDEEM_UNDERLYING:
case COMPOUND_REDEEM: case COMPOUND_REDEEM:
strcpy(msg->version, "Redeem"); strlcpy(msg->version, "Redeem", msg->versionLength);
break; break;
case COMPOUND_MINT: case COMPOUND_MINT:
case CETH_MINT: case CETH_MINT:
strcpy(msg->version, "Lend"); strlcpy(msg->version, "Lend", msg->versionLength);
break; break;
default: default:
break; break;
} }
strcat(msg->version, " Assets"); strlcat(msg->version, " Assets", msg->versionLength);
msg->result = ETH_PLUGIN_RESULT_OK; msg->result = ETH_PLUGIN_RESULT_OK;
} break; } break;
@@ -206,8 +206,8 @@ void compound_plugin_call(int message, void *parameters) {
compound_parameters_t *context = (compound_parameters_t *) msg->pluginContext; compound_parameters_t *context = (compound_parameters_t *) msg->pluginContext;
switch (msg->screenIndex) { switch (msg->screenIndex) {
case 0: { case 0: {
strcpy(msg->title, "Amount"); strlcpy(msg->title, "Amount", msg->titleLength);
char *ticker_ptr = (char *) context->ticker_1; char *ticker_ptr = context->ticker_1;
/* skip "c" in front of cToken unless we use "redeem", as /* skip "c" in front of cToken unless we use "redeem", as
redeem is the only operation dealing with a cToken amount */ redeem is the only operation dealing with a cToken amount */
if (context->selectorIndex != COMPOUND_REDEEM) { if (context->selectorIndex != COMPOUND_REDEEM) {
@@ -223,11 +223,11 @@ void compound_plugin_call(int message, void *parameters) {
} break; } break;
case 1: case 1:
strcpy(msg->title, "Contract"); strlcpy(msg->title, "Contract", msg->titleLength);
strcpy(msg->msg, "Compound "); strlcpy(msg->msg, "Compound ", msg->msgLength);
strcat(msg->msg, strlcat(msg->msg,
(char *) context->ticker_1 + context->ticker_1 + 1,
1); // remove the 'c' char at beginning of compound ticker msg->msgLength); // remove the 'c' char at beginning of compound ticker
msg->result = ETH_PLUGIN_RESULT_OK; msg->result = ETH_PLUGIN_RESULT_OK;
break; break;
default: default:

View File

@@ -16,10 +16,10 @@ typedef struct erc20_parameters_t {
uint8_t selectorIndex; uint8_t selectorIndex;
uint8_t destinationAddress[21]; uint8_t destinationAddress[21];
uint8_t amount[INT256_LENGTH]; uint8_t amount[INT256_LENGTH];
uint8_t ticker[MAX_TICKER_LEN]; char ticker[MAX_TICKER_LEN];
uint8_t decimals; uint8_t decimals;
uint8_t target; uint8_t target;
uint8_t contract_name[MAX_CONTRACT_NAME_LEN]; char contract_name[MAX_CONTRACT_NAME_LEN];
} erc20_parameters_t; } erc20_parameters_t;
typedef struct contract_t { typedef struct contract_t {
@@ -58,9 +58,7 @@ bool check_contract(erc20_parameters_t *context) {
for (size_t i = 0; i < NUM_CONTRACTS; i++) { for (size_t i = 0; i < NUM_CONTRACTS; i++) {
contract_t *contract = (contract_t *) PIC(&CONTRACTS[i]); contract_t *contract = (contract_t *) PIC(&CONTRACTS[i]);
if (memcmp(contract->address, context->destinationAddress, ADDRESS_LENGTH) == 0) { if (memcmp(contract->address, context->destinationAddress, ADDRESS_LENGTH) == 0) {
strncpy((char *) context->contract_name, strncpy(context->contract_name, contract->name, sizeof(context->contract_name));
contract->name,
sizeof(context->contract_name));
return true; return true;
} }
} }
@@ -161,7 +159,7 @@ void erc20_plugin_call(int message, void *parameters) {
(msg->token2 != NULL)); (msg->token2 != NULL));
if (msg->token1 != NULL) { if (msg->token1 != NULL) {
context->target = TARGET_ADDRESS; context->target = TARGET_ADDRESS;
strcpy((char *) context->ticker, (char *) msg->token1->ticker); strlcpy(context->ticker, msg->token1->ticker, MAX_TICKER_LEN);
context->decimals = msg->token1->decimals; context->decimals = msg->token1->decimals;
if (context->selectorIndex == ERC20_APPROVE) { if (context->selectorIndex == ERC20_APPROVE) {
if (check_contract(context)) { if (check_contract(context)) {
@@ -176,8 +174,8 @@ void erc20_plugin_call(int message, void *parameters) {
case ETH_PLUGIN_QUERY_CONTRACT_ID: { case ETH_PLUGIN_QUERY_CONTRACT_ID: {
ethQueryContractID_t *msg = (ethQueryContractID_t *) parameters; ethQueryContractID_t *msg = (ethQueryContractID_t *) parameters;
strcpy(msg->name, "Type"); strlcpy(msg->name, "Type", msg->nameLength);
strcpy(msg->version, "Approve"); strlcpy(msg->version, "Approve", msg->versionLength);
msg->result = ETH_PLUGIN_RESULT_OK; msg->result = ETH_PLUGIN_RESULT_OK;
} break; } break;
@@ -186,15 +184,15 @@ void erc20_plugin_call(int message, void *parameters) {
erc20_parameters_t *context = (erc20_parameters_t *) msg->pluginContext; erc20_parameters_t *context = (erc20_parameters_t *) msg->pluginContext;
switch (msg->screenIndex) { switch (msg->screenIndex) {
case 0: case 0:
strcpy(msg->title, "Amount"); strlcpy(msg->title, "Amount", msg->titleLength);
if (ismaxint(context->amount, sizeof(context->amount))) { if (ismaxint(context->amount, sizeof(context->amount))) {
strcpy(msg->msg, "Unlimited "); strlcpy(msg->msg, "Unlimited ", msg->msgLength);
strcat(msg->msg, (char *) context->ticker); strlcat(msg->msg, context->ticker, msg->msgLength);
} else { } else {
amountToString(context->amount, amountToString(context->amount,
sizeof(context->amount), sizeof(context->amount),
context->decimals, context->decimals,
(char *) context->ticker, context->ticker,
msg->msg, msg->msg,
100); 100);
} }
@@ -202,10 +200,10 @@ void erc20_plugin_call(int message, void *parameters) {
break; break;
case 1: case 1:
if (context->target >= TARGET_CONTRACT) { if (context->target >= TARGET_CONTRACT) {
strcpy(msg->title, "Contract"); strlcpy(msg->title, "Contract", msg->titleLength);
strcpy(msg->msg, (char *) context->contract_name); strlcpy(msg->msg, context->contract_name, msg->msgLength);
} else { } else {
strcpy(msg->title, "Address"); strlcpy(msg->title, "Address", msg->titleLength);
msg->msg[0] = '0'; msg->msg[0] = '0';
msg->msg[1] = 'x'; msg->msg[1] = 'x';
getEthAddressStringFromBinary(context->destinationAddress, getEthAddressStringFromBinary(context->destinationAddress,

View File

@@ -108,8 +108,8 @@ void erc721_plugin_call(int message, void *parameters) {
case ETH_PLUGIN_QUERY_CONTRACT_ID: { case ETH_PLUGIN_QUERY_CONTRACT_ID: {
ethQueryContractID_t *msg = (ethQueryContractID_t *) parameters; ethQueryContractID_t *msg = (ethQueryContractID_t *) parameters;
strcpy(msg->name, "Allowance"); strlcpy(msg->name, "Allowance", msg->nameLength);
strcpy(msg->version, ""); strlcpy(msg->version, "", msg->versionLength);
msg->result = ETH_PLUGIN_RESULT_OK; msg->result = ETH_PLUGIN_RESULT_OK;
} break; } break;
@@ -118,19 +118,19 @@ void erc721_plugin_call(int message, void *parameters) {
erc721_parameters_t *context = (erc721_parameters_t *) msg->pluginContext; erc721_parameters_t *context = (erc721_parameters_t *) msg->pluginContext;
switch (msg->screenIndex) { switch (msg->screenIndex) {
case 0: case 0:
strcpy(msg->title, "Contract Name"); strlcpy(msg->title, "Contract Name", msg->titleLength);
starkware_print_eth_address(tmpContent.txContent.destination, msg->msg); starkware_print_eth_address(tmpContent.txContent.destination, msg->msg);
msg->result = ETH_PLUGIN_RESULT_OK; msg->result = ETH_PLUGIN_RESULT_OK;
break; break;
case 1: case 1:
strcpy(msg->title, "NFT Contract"); strlcpy(msg->title, "NFT Contract", msg->titleLength);
starkware_print_eth_address(context->address, msg->msg); starkware_print_eth_address(context->address, msg->msg);
msg->result = ETH_PLUGIN_RESULT_OK; msg->result = ETH_PLUGIN_RESULT_OK;
break; break;
case 2: case 2:
strcpy(msg->title, "TokenID"); strlcpy(msg->title, "TokenID", msg->titleLength);
starkware_print_stark_key(context->tokenId, msg->msg); starkware_print_stark_key(context->tokenId, msg->msg);
msg->result = ETH_PLUGIN_RESULT_OK; msg->result = ETH_PLUGIN_RESULT_OK;
break; break;

View File

@@ -135,7 +135,7 @@ void eth2_plugin_call(int message, void *parameters) {
msg->pluginSharedRW->sha3); msg->pluginSharedRW->sha3);
// Copy back the string to the global variable. // Copy back the string to the global variable.
strcpy(context->deposit_address, tmp); strlcpy(context->deposit_address, tmp, ETH2_DEPOSIT_PUBKEY_LENGTH);
msg->result = ETH_PLUGIN_RESULT_OK; msg->result = ETH_PLUGIN_RESULT_OK;
break; break;
} }
@@ -198,8 +198,8 @@ void eth2_plugin_call(int message, void *parameters) {
case ETH_PLUGIN_QUERY_CONTRACT_ID: { case ETH_PLUGIN_QUERY_CONTRACT_ID: {
ethQueryContractID_t *msg = (ethQueryContractID_t *) parameters; ethQueryContractID_t *msg = (ethQueryContractID_t *) parameters;
strcpy(msg->name, "ETH2"); strlcpy(msg->name, "ETH2", msg->nameLength);
strcpy(msg->version, "Deposit"); strlcpy(msg->version, "Deposit", msg->versionLength);
msg->result = ETH_PLUGIN_RESULT_OK; msg->result = ETH_PLUGIN_RESULT_OK;
} break; } break;
@@ -210,7 +210,7 @@ void eth2_plugin_call(int message, void *parameters) {
case 0: { // Amount screen case 0: { // Amount screen
uint8_t decimals = WEI_TO_ETHER; uint8_t decimals = WEI_TO_ETHER;
char *ticker = chainConfig->coinName; char *ticker = chainConfig->coinName;
strcpy(msg->title, "Amount"); strlcpy(msg->title, "Amount", msg->titleLength);
amountToString(tmpContent.txContent.value.value, amountToString(tmpContent.txContent.value.value,
tmpContent.txContent.value.length, tmpContent.txContent.value.length,
decimals, decimals,
@@ -220,8 +220,8 @@ void eth2_plugin_call(int message, void *parameters) {
msg->result = ETH_PLUGIN_RESULT_OK; msg->result = ETH_PLUGIN_RESULT_OK;
} break; } break;
case 1: { // Deposit pubkey screen case 1: { // Deposit pubkey screen
strcpy(msg->title, "Validator"); strlcpy(msg->title, "Validator", msg->titleLength);
strcpy(msg->msg, context->deposit_address); strlcpy(msg->msg, context->deposit_address, msg->msgLength);
msg->result = ETH_PLUGIN_RESULT_OK; msg->result = ETH_PLUGIN_RESULT_OK;
} }
default: default:

View File

@@ -280,18 +280,22 @@ void starkware_print_stark_key(uint8_t *starkKey, char *destination) {
} }
// TODO : rewrite as independant code // TODO : rewrite as independant code
void starkware_print_eth_address(uint8_t *address, char *destination) { void starkware_print_eth_address(uint8_t *address, char *destination, size_t destinationLength) {
if (destinationLength < 43) {
strlcpy(destination, "ERROR", destinationLength);
return;
}
destination[0] = '0'; destination[0] = '0';
destination[1] = 'x'; destination[1] = 'x';
getEthAddressStringFromBinary(address, getEthAddressStringFromBinary(address, destination + 2, &global_sha3, chainConfig);
destination + 2,
&global_sha3,
chainConfig);
destination[42] = '\0'; destination[42] = '\0';
} }
// TODO : rewrite as independant code // TODO : rewrite as independant code
void starkware_print_amount(uint8_t *amountData, char *destination, bool forEscape) { void starkware_print_amount(uint8_t *amountData,
char *destination,
size_t destinationLength,
bool forEscape) {
uint256_t amount, amountPre, quantum; uint256_t amount, amountPre, quantum;
uint8_t decimals; uint8_t decimals;
char *ticker = chainConfig->coinName; char *ticker = chainConfig->coinName;
@@ -310,7 +314,7 @@ void starkware_print_amount(uint8_t *amountData, char *destination, bool forEsca
tokenDefinition_t *token = tokenDefinition_t *token =
&tmpCtx.transactionContext.tokens[dataContext.tokenContext.quantumIndex]; &tmpCtx.transactionContext.tokens[dataContext.tokenContext.quantumIndex];
decimals = token->decimals; decimals = token->decimals;
ticker = (char *) token->ticker; ticker = token->ticker;
readu256BE(amountData, &amountPre); readu256BE(amountData, &amountPre);
} }
if (amountData != NULL) { if (amountData != NULL) {
@@ -318,35 +322,35 @@ void starkware_print_amount(uint8_t *amountData, char *destination, bool forEsca
mul256(&amountPre, &quantum, &amount); mul256(&amountPre, &quantum, &amount);
} }
tostring256(&amount, 10, (char *) (G_io_apdu_buffer + 100), 100); tostring256(&amount, 10, (char *) (G_io_apdu_buffer + 100), 100);
strcpy(destination, ticker); strlcpy(destination, ticker, destinationLength);
adjustDecimals((char *) (G_io_apdu_buffer + 100), adjustDecimals((char *) (G_io_apdu_buffer + 100),
strlen((char *) (G_io_apdu_buffer + 100)), strlen((char *) (G_io_apdu_buffer + 100)),
destination + strlen(ticker), destination + strlen(ticker),
50 - strlen(ticker), destinationLength - strlen(ticker),
decimals); decimals);
} }
// TODO : rewrite as independant code // TODO : rewrite as independant code
void starkware_print_ticker(char *destination) { void starkware_print_ticker(char *destination, size_t destinationLength) {
char *ticker = chainConfig->coinName; char *ticker = chainConfig->coinName;
if (dataContext.tokenContext.quantumIndex != MAX_TOKEN) { if (dataContext.tokenContext.quantumIndex != MAX_TOKEN) {
tokenDefinition_t *token = tokenDefinition_t *token =
&tmpCtx.transactionContext.tokens[dataContext.tokenContext.quantumIndex]; &tmpCtx.transactionContext.tokens[dataContext.tokenContext.quantumIndex];
ticker = (char *) token->ticker; ticker = token->ticker;
} }
strcpy(destination, ticker); strlcpy(destination, ticker, destinationLength);
} }
// TODO : rewrite as independant code // TODO : rewrite as independant code
void starkware_print_asset_contract(char *destination) { void starkware_print_asset_contract(char *destination, size_t destinationLength) {
// token has been validated to be present previously // token has been validated to be present previously
if (dataContext.tokenContext.quantumIndex != MAX_TOKEN) { if (dataContext.tokenContext.quantumIndex != MAX_TOKEN) {
tokenDefinition_t *token = tokenDefinition_t *token =
&tmpCtx.transactionContext.tokens[dataContext.tokenContext.quantumIndex]; &tmpCtx.transactionContext.tokens[dataContext.tokenContext.quantumIndex];
starkware_print_eth_address(token->address, destination); starkware_print_eth_address(token->address, destination, destinationLength);
} else { } else {
strcpy(destination, "UNKNOWN"); strlcpy(destination, "UNKNOWN", destinationLength);
} }
} }
@@ -368,10 +372,7 @@ void starkware_get_source_address(char *destination) {
io_seproxyhal_io_heartbeat(); io_seproxyhal_io_heartbeat();
destination[0] = '0'; destination[0] = '0';
destination[1] = 'x'; destination[1] = 'x';
getEthAddressStringFromKey(&publicKey, getEthAddressStringFromKey(&publicKey, destination + 2, &global_sha3, chainConfig);
destination + 2,
&global_sha3,
chainConfig);
destination[42] = '\0'; destination[42] = '\0';
} }
@@ -646,54 +647,55 @@ void starkware_plugin_call(int message, void *parameters) {
PRINTF("starkware query contract id\n"); PRINTF("starkware query contract id\n");
switch (context->selectorIndex) { switch (context->selectorIndex) {
case STARKWARE_REGISTER: case STARKWARE_REGISTER:
strcpy(msg->name, "Register"); strlcpy(msg->name, "Register", msg->nameLength);
break; break;
case STARKWARE_DEPOSIT_TOKEN: case STARKWARE_DEPOSIT_TOKEN:
case STARKWARE_DEPOSIT_ETH: case STARKWARE_DEPOSIT_ETH:
case STARKWARE_DEPOSIT_NFT: case STARKWARE_DEPOSIT_NFT:
case STARKWARE_PROXY_DEPOSIT_TOKEN: case STARKWARE_PROXY_DEPOSIT_TOKEN:
case STARKWARE_PROXY_DEPOSIT_ETH: case STARKWARE_PROXY_DEPOSIT_ETH:
strcpy(msg->name, "Deposit"); strlcpy(msg->name, "Deposit", msg->nameLength);
break; break;
case STARKWARE_DEPOSIT_CANCEL: case STARKWARE_DEPOSIT_CANCEL:
strcpy(msg->name, "Cancel Deposit"); strlcpy(msg->name, "Cancel Deposit", msg->nameLength);
break; break;
case STARKWARE_DEPOSIT_RECLAIM: case STARKWARE_DEPOSIT_RECLAIM:
case STARKWARE_DEPOSIT_NFT_RECLAIM: case STARKWARE_DEPOSIT_NFT_RECLAIM:
strcpy(msg->name, "Reclaim Deposit"); strlcpy(msg->name, "Reclaim Deposit", msg->nameLength);
break; break;
case STARKWARE_WITHDRAW: case STARKWARE_WITHDRAW:
case STARKWARE_WITHDRAW_NFT: case STARKWARE_WITHDRAW_NFT:
case STARKWARE_WITHDRAW_AND_MINT: case STARKWARE_WITHDRAW_AND_MINT:
strcpy(msg->name, "Withdrawal"); strlcpy(msg->name, "Withdrawal", msg->nameLength);
break; break;
case STARKWARE_FULL_WITHDRAW: case STARKWARE_FULL_WITHDRAW:
strcpy(msg->name, "Full Withdrawal"); strlcpy(msg->name, "Full Withdrawal", msg->nameLength);
break; break;
case STARKWARE_FREEZE: case STARKWARE_FREEZE:
strcpy(msg->name, "Freeze"); strlcpy(msg->name, "Freeze", msg->nameLength);
break; break;
case STARKWARE_ESCAPE: case STARKWARE_ESCAPE:
strcpy(msg->name, "Escape"); strlcpy(msg->name, "Escape", msg->nameLength);
break; break;
case STARKWARE_VERIFY_ESCAPE: case STARKWARE_VERIFY_ESCAPE:
strcpy(msg->name, "Verify Escape"); strlcpy(msg->name, "Verify Escape", msg->nameLength);
break; break;
case STARKWARE_WITHDRAW_TO: case STARKWARE_WITHDRAW_TO:
case STARKWARE_WITHDRAW_NFT_TO: case STARKWARE_WITHDRAW_NFT_TO:
strcpy(msg->name, "Withdrawal To"); strlcpy(msg->name, "Withdrawal To", msg->nameLength);
break; break;
case STARKWARE_REGISTER_AND_DEPOSIT_TOKEN: case STARKWARE_REGISTER_AND_DEPOSIT_TOKEN:
case STARKWARE_REGISTER_AND_DEPOSIT_ETH: case STARKWARE_REGISTER_AND_DEPOSIT_ETH:
strcpy(msg->name, "Register&Deposit"); strlcpy(msg->name, "Register&Deposit", msg->nameLength);
break; break;
default: default:
break; break;
} }
strcpy(msg->version, strlcpy(
is_deversify_contract(tmpContent.txContent.destination) ? "DeversiFi" msg->version,
: "Starkware"); is_deversify_contract(tmpContent.txContent.destination) ? "DeversiFi" : "Starkware",
msg->versionLength);
msg->result = ETH_PLUGIN_RESULT_OK; msg->result = ETH_PLUGIN_RESULT_OK;
} break; } break;
@@ -702,11 +704,13 @@ void starkware_plugin_call(int message, void *parameters) {
starkware_parameters_t *context = (starkware_parameters_t *) msg->pluginContext; starkware_parameters_t *context = (starkware_parameters_t *) msg->pluginContext;
switch (msg->screenIndex) { switch (msg->screenIndex) {
case 0: case 0:
strcpy(msg->title, "Contract Name"); strlcpy(msg->title, "Contract Name", msg->titleLength);
if (is_deversify_contract(tmpContent.txContent.destination)) { if (is_deversify_contract(tmpContent.txContent.destination)) {
strcpy(msg->msg, "DeversiFi"); strlcpy(msg->msg, "DeversiFi", msg->msgLength);
} else { } else {
starkware_print_eth_address(tmpContent.txContent.destination, msg->msg); starkware_print_eth_address(tmpContent.txContent.destination,
msg->msg,
msg->msgLength);
} }
msg->result = ETH_PLUGIN_RESULT_OK; msg->result = ETH_PLUGIN_RESULT_OK;
break; break;
@@ -715,12 +719,12 @@ void starkware_plugin_call(int message, void *parameters) {
case STARKWARE_REGISTER: case STARKWARE_REGISTER:
case STARKWARE_REGISTER_AND_DEPOSIT_TOKEN: case STARKWARE_REGISTER_AND_DEPOSIT_TOKEN:
case STARKWARE_REGISTER_AND_DEPOSIT_ETH: case STARKWARE_REGISTER_AND_DEPOSIT_ETH:
strcpy(msg->title, "From ETH Address"); strlcpy(msg->title, "From ETH Address", msg->titleLength);
starkware_print_eth_address(context->amount, msg->msg); starkware_print_eth_address(context->amount, msg->msg, msg->msgLength);
break; break;
case STARKWARE_ESCAPE: case STARKWARE_ESCAPE:
strcpy(msg->title, "Amount"); strlcpy(msg->title, "Amount", msg->titleLength);
starkware_print_amount(context->amount, msg->msg, true); starkware_print_amount(context->amount, msg->msg, msg->msgLength, true);
break; break;
case STARKWARE_DEPOSIT_TOKEN: case STARKWARE_DEPOSIT_TOKEN:
case STARKWARE_DEPOSIT_ETH: case STARKWARE_DEPOSIT_ETH:
@@ -738,7 +742,7 @@ void starkware_plugin_call(int message, void *parameters) {
case STARKWARE_WITHDRAW_AND_MINT: case STARKWARE_WITHDRAW_AND_MINT:
case STARKWARE_WITHDRAW_NFT: case STARKWARE_WITHDRAW_NFT:
case STARKWARE_WITHDRAW_NFT_TO: case STARKWARE_WITHDRAW_NFT_TO:
strcpy(msg->title, "Master Account"); strlcpy(msg->title, "Master Account", msg->titleLength);
starkware_print_stark_key(context->starkKey, msg->msg); starkware_print_stark_key(context->starkKey, msg->msg);
break; break;
default: default:
@@ -755,7 +759,7 @@ void starkware_plugin_call(int message, void *parameters) {
case STARKWARE_ESCAPE: case STARKWARE_ESCAPE:
case STARKWARE_REGISTER_AND_DEPOSIT_TOKEN: case STARKWARE_REGISTER_AND_DEPOSIT_TOKEN:
case STARKWARE_REGISTER_AND_DEPOSIT_ETH: case STARKWARE_REGISTER_AND_DEPOSIT_ETH:
strcpy(msg->title, "Master Account"); strlcpy(msg->title, "Master Account", msg->titleLength);
starkware_print_stark_key(context->starkKey, msg->msg); starkware_print_stark_key(context->starkKey, msg->msg);
break; break;
@@ -769,22 +773,22 @@ void starkware_plugin_call(int message, void *parameters) {
case STARKWARE_FREEZE: case STARKWARE_FREEZE:
case STARKWARE_DEPOSIT_NFT: case STARKWARE_DEPOSIT_NFT:
case STARKWARE_DEPOSIT_NFT_RECLAIM: case STARKWARE_DEPOSIT_NFT_RECLAIM:
strcpy(msg->title, "Token Account"); strlcpy(msg->title, "Token Account", msg->titleLength);
starkware_print_vault_id(U4BE(context->vaultId, 0), msg->msg); starkware_print_vault_id(U4BE(context->vaultId, 0), msg->msg);
break; break;
case STARKWARE_WITHDRAW: case STARKWARE_WITHDRAW:
case STARKWARE_WITHDRAW_NFT: case STARKWARE_WITHDRAW_NFT:
strcpy(msg->title, "To ETH Address"); strlcpy(msg->title, "To ETH Address", msg->titleLength);
starkware_get_source_address(msg->msg); starkware_get_source_address(msg->msg);
break; break;
case STARKWARE_WITHDRAW_TO: case STARKWARE_WITHDRAW_TO:
case STARKWARE_WITHDRAW_NFT_TO: case STARKWARE_WITHDRAW_NFT_TO:
strcpy(msg->title, "To ETH Address"); strlcpy(msg->title, "To ETH Address", msg->titleLength);
starkware_print_eth_address(context->amount, msg->msg); starkware_print_eth_address(context->amount, msg->msg, msg->msgLength);
break; break;
case STARKWARE_WITHDRAW_AND_MINT: case STARKWARE_WITHDRAW_AND_MINT:
strcpy(msg->title, "Asset Contract"); strlcpy(msg->title, "Asset Contract", msg->titleLength);
starkware_print_asset_contract(msg->msg); starkware_print_asset_contract(msg->msg, msg->msgLength);
break; break;
default: default:
@@ -799,39 +803,40 @@ void starkware_plugin_call(int message, void *parameters) {
case 3: case 3:
switch (context->selectorIndex) { switch (context->selectorIndex) {
case STARKWARE_ESCAPE: case STARKWARE_ESCAPE:
strcpy(msg->title, "Token Account"); strlcpy(msg->title, "Token Account", msg->titleLength);
starkware_print_vault_id(U4BE(context->vaultId, 0), msg->msg); starkware_print_vault_id(U4BE(context->vaultId, 0), msg->msg);
break; break;
case STARKWARE_DEPOSIT_TOKEN: case STARKWARE_DEPOSIT_TOKEN:
case STARKWARE_DEPOSIT_ETH: case STARKWARE_DEPOSIT_ETH:
case STARKWARE_PROXY_DEPOSIT_TOKEN: case STARKWARE_PROXY_DEPOSIT_TOKEN:
case STARKWARE_PROXY_DEPOSIT_ETH: case STARKWARE_PROXY_DEPOSIT_ETH:
strcpy(msg->title, "Amount"); strlcpy(msg->title, "Amount", msg->titleLength);
starkware_print_amount( starkware_print_amount(
(((context->selectorIndex == STARKWARE_DEPOSIT_ETH) || (((context->selectorIndex == STARKWARE_DEPOSIT_ETH) ||
(context->selectorIndex == STARKWARE_PROXY_DEPOSIT_ETH)) (context->selectorIndex == STARKWARE_PROXY_DEPOSIT_ETH))
? NULL ? NULL
: context->amount), : context->amount),
msg->msg, msg->msg,
msg->msgLength,
false); false);
break; break;
case STARKWARE_WITHDRAW: case STARKWARE_WITHDRAW:
case STARKWARE_WITHDRAW_TO: case STARKWARE_WITHDRAW_TO:
strcpy(msg->title, "Token Symbol"); strlcpy(msg->title, "Token Symbol", msg->titleLength);
starkware_print_ticker(msg->msg); starkware_print_ticker(msg->msg, msg->msgLength);
break; break;
case STARKWARE_WITHDRAW_NFT: case STARKWARE_WITHDRAW_NFT:
case STARKWARE_WITHDRAW_NFT_TO: case STARKWARE_WITHDRAW_NFT_TO:
case STARKWARE_DEPOSIT_NFT: case STARKWARE_DEPOSIT_NFT:
case STARKWARE_DEPOSIT_NFT_RECLAIM: case STARKWARE_DEPOSIT_NFT_RECLAIM:
strcpy(msg->title, "NFT Contract"); strlcpy(msg->title, "NFT Contract", msg->titleLength);
starkware_print_asset_contract(msg->msg); starkware_print_asset_contract(msg->msg, msg->msgLength);
break; break;
case STARKWARE_REGISTER_AND_DEPOSIT_TOKEN: case STARKWARE_REGISTER_AND_DEPOSIT_TOKEN:
case STARKWARE_REGISTER_AND_DEPOSIT_ETH: case STARKWARE_REGISTER_AND_DEPOSIT_ETH:
strcpy(msg->title, "Token Account"); strlcpy(msg->title, "Token Account", msg->titleLength);
starkware_print_vault_id(U4BE(context->vaultId, 0), msg->msg); starkware_print_vault_id(U4BE(context->vaultId, 0), msg->msg);
break; break;
@@ -850,18 +855,19 @@ void starkware_plugin_call(int message, void *parameters) {
case STARKWARE_WITHDRAW_NFT_TO: case STARKWARE_WITHDRAW_NFT_TO:
case STARKWARE_DEPOSIT_NFT: case STARKWARE_DEPOSIT_NFT:
case STARKWARE_DEPOSIT_NFT_RECLAIM: case STARKWARE_DEPOSIT_NFT_RECLAIM:
strcpy(msg->title, "TokenID"); strlcpy(msg->title, "TokenID", msg->titleLength);
starkware_print_stark_key(dataContext.tokenContext.quantum, msg->msg); starkware_print_stark_key(dataContext.tokenContext.quantum, msg->msg);
break; break;
case STARKWARE_REGISTER_AND_DEPOSIT_TOKEN: case STARKWARE_REGISTER_AND_DEPOSIT_TOKEN:
case STARKWARE_REGISTER_AND_DEPOSIT_ETH: case STARKWARE_REGISTER_AND_DEPOSIT_ETH:
strcpy(msg->title, "Amount"); strlcpy(msg->title, "Amount", msg->titleLength);
starkware_print_amount( starkware_print_amount(
((context->selectorIndex == STARKWARE_REGISTER_AND_DEPOSIT_ETH) ((context->selectorIndex == STARKWARE_REGISTER_AND_DEPOSIT_ETH)
? NULL ? NULL
: context->amount), : context->amount),
msg->msg, msg->msg,
msg->msgLength,
false); false);
break; break;