Skip to content

Commit

Permalink
use strl(cat|cpy) instead of strn(cat|cpy)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrnerdhair committed Jul 7, 2021
1 parent df002a1 commit 7d6bfe0
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 24 deletions.
23 changes: 9 additions & 14 deletions lib/firmware/coins.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,38 +356,33 @@ void coin_amnt_to_str(const CoinType *coin, uint64_t amnt, char *buf, int len) {
/* Convert whole value to string */
if (coin_whole_part > 0) {
dec64_to_str(coin_whole_part, buf);
buf[strlen(buf)] = '.';
strlcat(buf, ".", len);
} else {
strncpy(buf, "0.", len);
strlcat(buf, "0.", len);
}

/* Convert Fraction value to string */
if (coin_fraction_part > 0) {
dec64_to_str(coin_fraction_part, buf_fract);

/* Add zeros after decimal */
i = 8 - strlen(buf_fract);
while (i) {
buf[strlen(buf) + i - 1] = '0';
i--;
for (i = 8 - strlen(buf_fract); i > 0; i--) {
strlcat(buf, "0", len);
}
/*concantenate whole and fraction part of string */
strncpy(buf + strlen(buf), buf_fract, strlen(buf_fract));
strlcat(buf, buf_fract, len);

/* Drop least significant zeros in fraction part to shorten display*/
i = strlen(buf);
while (buf[i - 1] == '0') {
for (i = strlen(buf); i > 0 && buf[i - 1] == '0'; i--) {
buf[i - 1] = 0;
i--;
}
} else {
buf[strlen(buf)] = '0';
strlcat(buf, "0", len);
}
/* Added coin type to amount */
if (coin->has_coin_shortcut) {
buf[strlen(buf)] = ' ';
strncpy(buf + strlen(buf), coin->coin_shortcut,
strlen(coin->coin_shortcut));
strlcat(buf, " ", len);
strlcat(buf, coin->coin_shortcut, len);
}
}

Expand Down
6 changes: 3 additions & 3 deletions lib/firmware/ethereum_tokens.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ void coinFromToken(CoinType *coin, const TokenType *token) {
memset(coin, 0, sizeof(*coin));

coin->has_coin_name = true;
strncpy(&coin->coin_name[0], token->ticker + 1, sizeof(coin->coin_name));
strlcpy(&coin->coin_name[0], token->ticker + 1, sizeof(coin->coin_name));

coin->has_coin_shortcut = true;
strncpy(&coin->coin_shortcut[0], token->ticker + 1,
strlcpy(&coin->coin_shortcut[0], token->ticker + 1,
sizeof(coin->coin_shortcut));

coin->has_forkid = true;
Expand All @@ -116,5 +116,5 @@ void coinFromToken(CoinType *coin, const TokenType *token) {
"contract_address is not large enough to hold an ETH address");

coin->has_curve_name = true;
strncpy(&coin->curve_name[0], "secp256k1", sizeof(coin->curve_name));
strlcpy(&coin->curve_name[0], "secp256k1", sizeof(coin->curve_name));
}
2 changes: 1 addition & 1 deletion lib/firmware/fsm_msg_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ void fsm_msgPing(Ping *msg) {
if (is_mfg_mode() && msg->has_message && isValidModelNumber(msg->message)) {
set_mfg_mode_off();
char message[32];
strncpy(message, msg->message, sizeof(message));
strlcpy(message, msg->message, sizeof(message));
message[31] = 0;
flash_setModel(&message);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/firmware/fsm_msg_thorchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ void fsm_msgThorchainMsgAck(const ThorchainMsgAck *msg) {
char amount_str[32];
char asset_str[21];
asset_str[0] = ' ';
strncpy(&(asset_str[1]), msg->deposit.asset, 20);
strlcpy(&(asset_str[1]), msg->deposit.asset, 20);
bn_format_uint64(msg->deposit.amount, NULL, asset_str, 8, 0, false, amount_str,
sizeof(amount_str));
if (!confirm_transaction_output(
Expand Down
8 changes: 4 additions & 4 deletions lib/firmware/storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ void storage_readPolicyV2(PolicyType *policy, const char *policy_name,
bool enabled) {
policy->has_policy_name = true;
memset(policy->policy_name, 0, sizeof(policy->policy_name));
strncpy(policy->policy_name, policy_name, sizeof(policy->policy_name));
strlcpy(policy->policy_name, policy_name, sizeof(policy->policy_name));
policy->has_enabled = true;
policy->enabled = enabled;
}
Expand Down Expand Up @@ -589,7 +589,7 @@ void storage_readStorageV1(SessionState *ss, Storage *storage, const char *ptr,
storage->has_sec_fingerprint = false;

#if DEBUG_LINK
strncpy(debuglink_pin, storage->sec.pin, sizeof(debuglink_pin));
strlcpy(debuglink_pin, storage->sec.pin, sizeof(debuglink_pin));
memcpy(debuglink_mnemonic, storage->sec.mnemonic, sizeof(debuglink_mnemonic));
storage_loadNode(&debuglink_node, &storage->sec.node);
#endif
Expand Down Expand Up @@ -1466,7 +1466,7 @@ void storage_setPin(const char *pin) {
session.pinCached = true;

#if DEBUG_LINK
strncpy(debuglink_pin, pin, sizeof(debuglink_pin));
strlcpy(debuglink_pin, pin, sizeof(debuglink_pin));
#endif
}

Expand Down Expand Up @@ -1525,7 +1525,7 @@ void storage_setWipeCode(const char *wipe_code) {
storage_setWipeCode_impl(&session, &shadow_config.storage, wipe_code);

#if DEBUG_LINK
strncpy(debuglink_wipe_code, wipe_code, sizeof(debuglink_wipe_code));
strlcpy(debuglink_wipe_code, wipe_code, sizeof(debuglink_wipe_code));
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion lib/firmware/thorchain.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ bool thorchain_parseConfirmMemo(const char *swapStr, size_t size) {

if (size > 256) return false;
memzero(memoBuf, sizeof(memoBuf));
strncpy(memoBuf, swapStr, size);
strlcpy(memoBuf, swapStr, size);
memoBuf[255] = '\0'; // ensure null termination
tok = strtok(memoBuf, ":");

Expand Down

0 comments on commit 7d6bfe0

Please sign in to comment.