From 8f167ba1403d0b04dec84c2e700bbf16cd9251c4 Mon Sep 17 00:00:00 2001 From: Karl Fessel Date: Mon, 16 Jan 2023 21:08:29 +0100 Subject: [PATCH] sys/shell_lock: do not call strlen, less jumpy --- sys/shell_lock/shell_lock.c | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/sys/shell_lock/shell_lock.c b/sys/shell_lock/shell_lock.c index db53cf21f9d8..3b926449a17b 100644 --- a/sys/shell_lock/shell_lock.c +++ b/sys/shell_lock/shell_lock.c @@ -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; }