Skip to content

Commit

Permalink
PVS-Studio pipeline (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
akopachov authored Nov 2, 2022
1 parent e8bcd90 commit a11332c
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 61 deletions.
6 changes: 1 addition & 5 deletions scenes/add_new_token/totp_scene_add_new_token.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,7 @@ bool totp_scene_add_new_token_handle_event(PluginEvent* const event, PluginState
tokenInfo->algo = scene_state->algo;
tokenInfo->digits = scene_state->digits_count;

if(plugin_state->tokens_list == NULL) {
plugin_state->tokens_list = list_init_head(tokenInfo);
} else {
list_add(plugin_state->tokens_list, tokenInfo);
}
TOTP_LIST_INIT_OR_ADD(plugin_state->tokens_list, tokenInfo);
plugin_state->tokens_count++;

totp_config_file_save_new_token(tokenInfo);
Expand Down
83 changes: 40 additions & 43 deletions services/cli/commands/add/add.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,43 @@ static void furi_string_secure_free(FuriString* str) {
furi_string_free(str);
}

static bool totp_cli_read_secret(Cli* cli, FuriString* out_str, bool mask_user_input) {
uint8_t c;
while(cli_read(cli, &c, 1) == 1) {
if(c == CliSymbolAsciiEsc) {
// Some keys generating escape-sequences
// We need to ignore them as we case about alpha-numerics only
uint8_t c2;
cli_read_timeout(cli, &c2, 1, 0);
cli_read_timeout(cli, &c2, 1, 0);
} else if(c == CliSymbolAsciiETX) {
TOTP_CLI_DELETE_CURRENT_LINE();
TOTP_CLI_PRINTF("Cancelled by user\r\n");
return false;
} else if((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
if(mask_user_input) {
putc('*', stdout);
} else {
putc(c, stdout);
}
fflush(stdout);
furi_string_push_back(out_str, c);
} else if(c == CliSymbolAsciiBackspace || c == CliSymbolAsciiDel) {
size_t out_str_size = furi_string_size(out_str);
if(out_str_size > 0) {
TOTP_CLI_DELETE_LAST_CHAR();
furi_string_left(out_str, out_str_size - 1);
}
} else if(c == CliSymbolAsciiCR) {
cli_nl();
break;
}
}

TOTP_CLI_DELETE_LAST_LINE();
return true;
}

void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
FuriString* temp_str = furi_string_alloc();
TokenInfo* token_info = token_info_alloc();
Expand Down Expand Up @@ -148,44 +185,8 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
// Reading token secret
furi_string_reset(temp_str);
TOTP_CLI_PRINTF("Enter token secret and confirm with [ENTER]\r\n");

uint8_t c;
while(cli_read(cli, &c, 1) == 1) {
if(c == CliSymbolAsciiEsc) {
// Some keys generating escape-sequences
// We need to ignore them as we case about alpha-numerics only
uint8_t c2;
cli_read_timeout(cli, &c2, 1, 0);
cli_read_timeout(cli, &c2, 1, 0);
} else if(c == CliSymbolAsciiETX) {
TOTP_CLI_DELETE_CURRENT_LINE();
TOTP_CLI_PRINTF("Cancelled by user\r\n");
furi_string_secure_free(temp_str);
token_info_free(token_info);
return;
} else if((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
if(mask_user_input) {
putc('*', stdout);
} else {
putc(c, stdout);
}
fflush(stdout);
furi_string_push_back(temp_str, c);
} else if(c == CliSymbolAsciiBackspace || c == CliSymbolAsciiDel) {
size_t temp_str_size = furi_string_size(temp_str);
if(temp_str_size > 0) {
TOTP_CLI_DELETE_LAST_CHAR();
furi_string_left(temp_str, temp_str_size - 1);
}
} else if(c == CliSymbolAsciiCR) {
cli_nl();
break;
}
}

TOTP_CLI_DELETE_LAST_LINE();

if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
if(!totp_cli_read_secret(cli, temp_str, mask_user_input) ||
!totp_cli_ensure_authenticated(plugin_state, cli)) {
furi_string_secure_free(temp_str);
token_info_free(token_info);
return;
Expand All @@ -210,11 +211,7 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
load_generate_token_scene = true;
}

if(plugin_state->tokens_list == NULL) {
plugin_state->tokens_list = list_init_head(token_info);
} else {
list_add(plugin_state->tokens_list, token_info);
}
TOTP_LIST_INIT_OR_ADD(plugin_state->tokens_list, token_info);
plugin_state->tokens_count++;
totp_config_file_save_new_token(token_info);

Expand Down
6 changes: 1 addition & 5 deletions services/config/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -409,11 +409,7 @@ TokenLoadingResult totp_config_file_load_tokens(PluginState* const plugin_state)

FURI_LOG_D(LOGGING_TAG, "Found token \"%s\"", tokenInfo->name);

if(plugin_state->tokens_list == NULL) {
plugin_state->tokens_list = list_init_head(tokenInfo);
} else {
list_add(plugin_state->tokens_list, tokenInfo);
}
TOTP_LIST_INIT_OR_ADD(plugin_state->tokens_list, tokenInfo);

index++;
}
Expand Down
5 changes: 3 additions & 2 deletions services/list/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ ListNode* list_add(ListNode* head, void* data) {
return head;
}

ListNode* list_find(ListNode* head, void* data) {
ListNode* list_find(ListNode* head, const void* data) {
ListNode* it;

for(it = head; it != NULL; it = it->next)
Expand Down Expand Up @@ -66,7 +66,8 @@ ListNode* list_remove(ListNode* head, ListNode* ep) {
}

void list_free(ListNode* head) {
ListNode *it = head, *tmp;
ListNode* it = head;
ListNode* tmp;

while(it != NULL) {
tmp = it;
Expand Down
11 changes: 10 additions & 1 deletion services/list/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,20 @@ ListNode* list_add(
void* data); /* adds element with specified data to the end of the list and returns new head node. */
ListNode* list_find(
ListNode* head,
void* data); /* returns pointer of element with specified data in list. */
const void* data); /* returns pointer of element with specified data in list. */
ListNode* list_element_at(
ListNode* head,
uint16_t index); /* returns pointer of element with specified index in list. */
ListNode* list_remove(
ListNode* head,
ListNode* ep); /* removes element from the list and returns new head node. */
void list_free(ListNode* head); /* deletes all elements of the list. */

#define TOTP_LIST_INIT_OR_ADD(head, item) \
do { \
if(head == NULL) { \
head = list_init_head(item); \
} else { \
list_add(head, item); \
} \
} while(false)
6 changes: 3 additions & 3 deletions types/token_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ bool token_info_set_secret(
TokenInfo* token_info,
const char* base32_token_secret,
size_t token_secret_length,
uint8_t* iv) {
const uint8_t* iv) {
uint8_t* plain_secret = malloc(token_secret_length);
int plain_secret_length =
base32_decode((uint8_t*)base32_token_secret, plain_secret, token_secret_length);
base32_decode((const uint8_t*)base32_token_secret, plain_secret, token_secret_length);
bool result;
if(plain_secret_length >= 0) {
token_info->token =
Expand All @@ -43,7 +43,7 @@ bool token_info_set_secret(
return result;
}

uint8_t token_info_get_digits_count(TokenInfo* token_info) {
uint8_t token_info_get_digits_count(const TokenInfo* token_info) {
switch(token_info->digits) {
case TOTP_6_DIGITS:
return 6;
Expand Down
4 changes: 2 additions & 2 deletions types/token_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ bool token_info_set_secret(
TokenInfo* token_info,
const char* base32_token_secret,
size_t token_secret_length,
uint8_t* iv);
uint8_t token_info_get_digits_count(TokenInfo* token_info);
const uint8_t* iv);
uint8_t token_info_get_digits_count(const TokenInfo* token_info);

0 comments on commit a11332c

Please sign in to comment.