Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sys/shell_lock: do not call strlen, less jumpy #19157

Merged
merged 1 commit into from
Jan 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 8 additions & 20 deletions sys/shell_lock/shell_lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,35 +81,23 @@ static bool __attribute__((optimize("O0"))) _safe_strcmp(const char* input, cons
{
bool the_same = true;

int input_len = strlen(input);
int pwd_len = strlen(pwd);

int input_index = 0;
int pwd_index = 0;

do {
if (input[input_index] != pwd[pwd_index]) {
the_same &= false;
}
else {
the_same &= true;
}
the_same &= input[input_index] == pwd[pwd_index];

/* keep indices at last index of respective string */
if (input_index < input_len) {
input_index++;
}
input_index += input[input_index] != '\0';
pwd_index += pwd[pwd_index] != '\0';

if (pwd_index < pwd_len) {
pwd_index++;
}
} while (input[input_index] != '\0' );

} while (input[input_index] != '\0');
/* ensure last char is the same */
the_same &= input[input_index] == pwd[pwd_index];

if (input_len != pwd_len) {
/* substring of the password doesn't count */
return false;
}
/* ensure last index is the same */
the_same &= input_index == pwd_index;

return the_same;
}
Expand Down