diff mbox series

[v2,5/7] cmd: bcb: Add support for processing const string literals in bcb_set()

Message ID 2da216314c820fdf2fa00cb3614c349a27076b32.1611688251.git.roman.kovalivskyi@globallogic.com
State Accepted
Delegated to: Lukasz Majewski
Headers show
Series Refactor generic fastboot_set_reboot_flag implementation | expand

Commit Message

Roman Kovalivskyi Jan. 26, 2021, 8:54 p.m. UTC
From: Eugeniu Rosca <erosca@de.adit-jv.com>

On request/suggestion from Simon Glass back in May 22 2019 [1], the
'strsep' mechanism implemented in bcb_set() was set to work directly
with user-provided argv strings, to avoid duplicating memory and for
the sake of simpler implementation.

However, since we recently exposed bcb_write_reboot_reason() API to be
called by U-Boot fastboot, the idea is to be able to pass const string
literals to this new BCB API, carrying the reboot reason.

Since 'strsep' (just like its older/superseded sibling 'strtok')
modifies the input string passed as parameter, BCB command in its
current state would attempt to perform in-place modifications in a
readonly string, which might lead to unexpected results.

Fix the above with the cost of one dynamic memory allocation ('strdup').
This will also ensure no compiler warnings when passing string literals
to bcb_write_reboot_reason().

[1] http://u-boot.10912.n7.nabble.com/PATCH-v2-0-2-Add-bcb-command-to-read-modify-write-Android-BCB-td369934i20.html#a370456

Cc: Simon Glass <sjg@chromium.org>
Signed-off-by: Eugeniu Rosca <erosca@de.adit-jv.com>
Signed-off-by: Roman Kovalivskyi <roman.kovalivskyi@globallogic.com>
---
 cmd/bcb.c     | 17 ++++++++++++-----
 include/bcb.h |  4 ++--
 2 files changed, 14 insertions(+), 7 deletions(-)
diff mbox series

Patch

diff --git a/cmd/bcb.c b/cmd/bcb.c
index 5da3526142ad..6b6f1e9a2f10 100644
--- a/cmd/bcb.c
+++ b/cmd/bcb.c
@@ -11,6 +11,7 @@ 
 #include <common.h>
 #include <log.h>
 #include <part.h>
+#include <malloc.h>
 
 enum bcb_cmd {
 	BCB_CMD_LOAD,
@@ -179,10 +180,10 @@  static int do_bcb_load(struct cmd_tbl *cmdtp, int flag, int argc,
 	return __bcb_load(devnum, argv[2]);
 }
 
-static int __bcb_set(char *fieldp, char *valp)
+static int __bcb_set(char *fieldp, const char *valp)
 {
 	int size, len;
-	char *field, *str, *found;
+	char *field, *str, *found, *tmp;
 
 	if (bcb_field_get(fieldp, &field, &size))
 		return CMD_RET_FAILURE;
@@ -193,14 +194,20 @@  static int __bcb_set(char *fieldp, char *valp)
 		       valp, len, size, fieldp);
 		return CMD_RET_FAILURE;
 	}
-	str = valp;
+	str = strdup(valp);
+	if (!str) {
+		printf("Error: Out of memory while strdup\n");
+		return CMD_RET_FAILURE;
+	}
 
+	tmp = str;
 	field[0] = '\0';
-	while ((found = strsep(&str, ":"))) {
+	while ((found = strsep(&tmp, ":"))) {
 		if (field[0] != '\0')
 			strcat(field, "\n");
 		strcat(field, found);
 	}
+	free(str);
 
 	return CMD_RET_SUCCESS;
 }
@@ -308,7 +315,7 @@  static int do_bcb_store(struct cmd_tbl *cmdtp, int flag, int argc,
 	return __bcb_store();
 }
 
-int bcb_write_reboot_reason(int devnum, char *partp, char *reasonp)
+int bcb_write_reboot_reason(int devnum, char *partp, const char *reasonp)
 {
 	int ret;
 
diff --git a/include/bcb.h b/include/bcb.h
index 05db5935e0e7..c91edb91265a 100644
--- a/include/bcb.h
+++ b/include/bcb.h
@@ -11,9 +11,9 @@ 
 #include <common.h>
 
 #if CONFIG_IS_ENABLED(CMD_BCB)
-int bcb_write_reboot_reason(int devnum, char *partp, char *reasonp);
+int bcb_write_reboot_reason(int devnum, char *partp, const char *reasonp);
 #else
-static inline int bcb_write_reboot_reason(int devnum, char *partp, char *reasonp)
+static inline int bcb_write_reboot_reason(int devnum, char *partp, const char *reasonp)
 {
 	return -EOPNOTSUPP;
 }