mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 2/3] fastboot: retire strcmp_l1 in favor of str_has_prefix
Date: Fri,  9 Aug 2024 16:19:58 +0200	[thread overview]
Message-ID: <20240809141959.313914-2-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20240809141959.313914-1-a.fatoum@pengutronix.de>

strcmp_l1 is basically str_has_prefix with inverted arguments and a
NULL check.

We don't need the NULL check as cmdbuf and cmd->cmd should always be
non-NULL: The former is the address of an array populated by fastboot
network or USB code and the latter is a pointer to a string literal.

So let's codify the assumption that cmdbuf is not NULL into the
prototype and replace strcmp_l1 with str_has_prefix. This has the added
benefit that str_has_prefix returns the length of the prefix, which
saves us a repeated call to strlen(cmd->cmd);

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
v1 -> v2:
  - new patch
---
 common/fastboot.c  | 14 +++++---------
 include/fastboot.h |  3 ++-
 2 files changed, 7 insertions(+), 10 deletions(-)

diff --git a/common/fastboot.c b/common/fastboot.c
index e85cc6d8aaf8..dc66d7123b02 100644
--- a/common/fastboot.c
+++ b/common/fastboot.c
@@ -287,13 +287,6 @@ static void cb_reboot(struct fastboot *fb, const char *cmd)
 	restart_machine();
 }
 
-static int strcmp_l1(const char *s1, const char *s2)
-{
-	if (!s1 || !s2)
-		return -1;
-	return strncmp(s1, s2, strlen(s1));
-}
-
 static void cb_getvar(struct fastboot *fb, const char *cmd)
 {
 	struct fb_variable *var;
@@ -815,10 +808,13 @@ static void fb_run_command(struct fastboot *fb, const char *cmdbuf,
 	console_countdown_abort("fastboot");
 
 	for (i = 0; i < num_commands; i++) {
+		size_t cmdlen;
+
 		cmd = &cmds[i];
 
-		if (!strcmp_l1(cmd->cmd, cmdbuf)) {
-			cmd->cb(fb, cmdbuf + strlen(cmd->cmd));
+		cmdlen = str_has_prefix(cmdbuf, cmd->cmd);
+		if (cmdlen) {
+			cmd->cb(fb, cmdbuf + cmdlen);
 
 			return;
 		}
diff --git a/include/fastboot.h b/include/fastboot.h
index cd415847e348..4b2fdf3190b2 100644
--- a/include/fastboot.h
+++ b/include/fastboot.h
@@ -87,7 +87,8 @@ int fastboot_tx_print(struct fastboot *fb, enum fastboot_msg_type type,
 		      const char *fmt, ...);
 void fastboot_start_download_generic(struct fastboot *fb);
 void fastboot_download_finished(struct fastboot *fb);
-void fastboot_exec_cmd(struct fastboot *fb, const char *cmdbuf);
+void fastboot_exec_cmd(struct fastboot *fb, const char *cmdbuf)
+        __attribute__((nonnull));
 void fastboot_abort(struct fastboot *fb);
 
 #endif
-- 
2.39.2




  reply	other threads:[~2024-08-09 14:20 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-09 14:19 [PATCH 1/3] fastboot: print all variables only on getvar:all and not its prefixes Ahmad Fatoum
2024-08-09 14:19 ` Ahmad Fatoum [this message]
2024-08-09 14:19 ` [PATCH 3/3] fastboot: avoid console_countdown_abort for getvar request Ahmad Fatoum
2024-08-14  8:53 ` [PATCH 1/3] fastboot: print all variables only on getvar:all and not its prefixes Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240809141959.313914-2-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox