Skip to content

Commit a11332c

Browse files
authored
PVS-Studio pipeline (#18)
1 parent e8bcd90 commit a11332c

File tree

7 files changed

+60
-61
lines changed

7 files changed

+60
-61
lines changed

scenes/add_new_token/totp_scene_add_new_token.c

+1-5
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,7 @@ bool totp_scene_add_new_token_handle_event(PluginEvent* const event, PluginState
251251
tokenInfo->algo = scene_state->algo;
252252
tokenInfo->digits = scene_state->digits_count;
253253

254-
if(plugin_state->tokens_list == NULL) {
255-
plugin_state->tokens_list = list_init_head(tokenInfo);
256-
} else {
257-
list_add(plugin_state->tokens_list, tokenInfo);
258-
}
254+
TOTP_LIST_INIT_OR_ADD(plugin_state->tokens_list, tokenInfo);
259255
plugin_state->tokens_count++;
260256

261257
totp_config_file_save_new_token(tokenInfo);

services/cli/commands/add/add.c

+40-43
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,43 @@ static void furi_string_secure_free(FuriString* str) {
8787
furi_string_free(str);
8888
}
8989

90+
static bool totp_cli_read_secret(Cli* cli, FuriString* out_str, bool mask_user_input) {
91+
uint8_t c;
92+
while(cli_read(cli, &c, 1) == 1) {
93+
if(c == CliSymbolAsciiEsc) {
94+
// Some keys generating escape-sequences
95+
// We need to ignore them as we case about alpha-numerics only
96+
uint8_t c2;
97+
cli_read_timeout(cli, &c2, 1, 0);
98+
cli_read_timeout(cli, &c2, 1, 0);
99+
} else if(c == CliSymbolAsciiETX) {
100+
TOTP_CLI_DELETE_CURRENT_LINE();
101+
TOTP_CLI_PRINTF("Cancelled by user\r\n");
102+
return false;
103+
} else if((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
104+
if(mask_user_input) {
105+
putc('*', stdout);
106+
} else {
107+
putc(c, stdout);
108+
}
109+
fflush(stdout);
110+
furi_string_push_back(out_str, c);
111+
} else if(c == CliSymbolAsciiBackspace || c == CliSymbolAsciiDel) {
112+
size_t out_str_size = furi_string_size(out_str);
113+
if(out_str_size > 0) {
114+
TOTP_CLI_DELETE_LAST_CHAR();
115+
furi_string_left(out_str, out_str_size - 1);
116+
}
117+
} else if(c == CliSymbolAsciiCR) {
118+
cli_nl();
119+
break;
120+
}
121+
}
122+
123+
TOTP_CLI_DELETE_LAST_LINE();
124+
return true;
125+
}
126+
90127
void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
91128
FuriString* temp_str = furi_string_alloc();
92129
TokenInfo* token_info = token_info_alloc();
@@ -148,44 +185,8 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
148185
// Reading token secret
149186
furi_string_reset(temp_str);
150187
TOTP_CLI_PRINTF("Enter token secret and confirm with [ENTER]\r\n");
151-
152-
uint8_t c;
153-
while(cli_read(cli, &c, 1) == 1) {
154-
if(c == CliSymbolAsciiEsc) {
155-
// Some keys generating escape-sequences
156-
// We need to ignore them as we case about alpha-numerics only
157-
uint8_t c2;
158-
cli_read_timeout(cli, &c2, 1, 0);
159-
cli_read_timeout(cli, &c2, 1, 0);
160-
} else if(c == CliSymbolAsciiETX) {
161-
TOTP_CLI_DELETE_CURRENT_LINE();
162-
TOTP_CLI_PRINTF("Cancelled by user\r\n");
163-
furi_string_secure_free(temp_str);
164-
token_info_free(token_info);
165-
return;
166-
} else if((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
167-
if(mask_user_input) {
168-
putc('*', stdout);
169-
} else {
170-
putc(c, stdout);
171-
}
172-
fflush(stdout);
173-
furi_string_push_back(temp_str, c);
174-
} else if(c == CliSymbolAsciiBackspace || c == CliSymbolAsciiDel) {
175-
size_t temp_str_size = furi_string_size(temp_str);
176-
if(temp_str_size > 0) {
177-
TOTP_CLI_DELETE_LAST_CHAR();
178-
furi_string_left(temp_str, temp_str_size - 1);
179-
}
180-
} else if(c == CliSymbolAsciiCR) {
181-
cli_nl();
182-
break;
183-
}
184-
}
185-
186-
TOTP_CLI_DELETE_LAST_LINE();
187-
188-
if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
188+
if(!totp_cli_read_secret(cli, temp_str, mask_user_input) ||
189+
!totp_cli_ensure_authenticated(plugin_state, cli)) {
189190
furi_string_secure_free(temp_str);
190191
token_info_free(token_info);
191192
return;
@@ -210,11 +211,7 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
210211
load_generate_token_scene = true;
211212
}
212213

213-
if(plugin_state->tokens_list == NULL) {
214-
plugin_state->tokens_list = list_init_head(token_info);
215-
} else {
216-
list_add(plugin_state->tokens_list, token_info);
217-
}
214+
TOTP_LIST_INIT_OR_ADD(plugin_state->tokens_list, token_info);
218215
plugin_state->tokens_count++;
219216
totp_config_file_save_new_token(token_info);
220217

services/config/config.c

+1-5
Original file line numberDiff line numberDiff line change
@@ -409,11 +409,7 @@ TokenLoadingResult totp_config_file_load_tokens(PluginState* const plugin_state)
409409

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

412-
if(plugin_state->tokens_list == NULL) {
413-
plugin_state->tokens_list = list_init_head(tokenInfo);
414-
} else {
415-
list_add(plugin_state->tokens_list, tokenInfo);
416-
}
412+
TOTP_LIST_INIT_OR_ADD(plugin_state->tokens_list, tokenInfo);
417413

418414
index++;
419415
}

services/list/list.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ ListNode* list_add(ListNode* head, void* data) {
2626
return head;
2727
}
2828

29-
ListNode* list_find(ListNode* head, void* data) {
29+
ListNode* list_find(ListNode* head, const void* data) {
3030
ListNode* it;
3131

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

6868
void list_free(ListNode* head) {
69-
ListNode *it = head, *tmp;
69+
ListNode* it = head;
70+
ListNode* tmp;
7071

7172
while(it != NULL) {
7273
tmp = it;

services/list/list.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,20 @@ ListNode* list_add(
1414
void* data); /* adds element with specified data to the end of the list and returns new head node. */
1515
ListNode* list_find(
1616
ListNode* head,
17-
void* data); /* returns pointer of element with specified data in list. */
17+
const void* data); /* returns pointer of element with specified data in list. */
1818
ListNode* list_element_at(
1919
ListNode* head,
2020
uint16_t index); /* returns pointer of element with specified index in list. */
2121
ListNode* list_remove(
2222
ListNode* head,
2323
ListNode* ep); /* removes element from the list and returns new head node. */
2424
void list_free(ListNode* head); /* deletes all elements of the list. */
25+
26+
#define TOTP_LIST_INIT_OR_ADD(head, item) \
27+
do { \
28+
if(head == NULL) { \
29+
head = list_init_head(item); \
30+
} else { \
31+
list_add(head, item); \
32+
} \
33+
} while(false)

types/token_info.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ bool token_info_set_secret(
2525
TokenInfo* token_info,
2626
const char* base32_token_secret,
2727
size_t token_secret_length,
28-
uint8_t* iv) {
28+
const uint8_t* iv) {
2929
uint8_t* plain_secret = malloc(token_secret_length);
3030
int plain_secret_length =
31-
base32_decode((uint8_t*)base32_token_secret, plain_secret, token_secret_length);
31+
base32_decode((const uint8_t*)base32_token_secret, plain_secret, token_secret_length);
3232
bool result;
3333
if(plain_secret_length >= 0) {
3434
token_info->token =
@@ -43,7 +43,7 @@ bool token_info_set_secret(
4343
return result;
4444
}
4545

46-
uint8_t token_info_get_digits_count(TokenInfo* token_info) {
46+
uint8_t token_info_get_digits_count(const TokenInfo* token_info) {
4747
switch(token_info->digits) {
4848
case TOTP_6_DIGITS:
4949
return 6;

types/token_info.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ bool token_info_set_secret(
2020
TokenInfo* token_info,
2121
const char* base32_token_secret,
2222
size_t token_secret_length,
23-
uint8_t* iv);
24-
uint8_t token_info_get_digits_count(TokenInfo* token_info);
23+
const uint8_t* iv);
24+
uint8_t token_info_get_digits_count(const TokenInfo* token_info);

0 commit comments

Comments
 (0)