[PATCH v3 3/7] image-android.c: Add null check to bootargs and cmdline

George Chan via B4 Relay devnull+gchan9527.gmail.com at kernel.org
Sat Jun 28 14:25:21 CEST 2025


From: George Chan <gchan9527 at gmail.com>

With some manual testing, when bootargs and/or cmdline from android boot
image is absent there will be some unhandled behavior happened.

So add null check to all these const char values.

Signed-off-by: George Chan <gchan9527 at gmail.com>
---
 boot/image-android.c | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)

diff --git a/boot/image-android.c b/boot/image-android.c
index 26e2fc21929..422f254c78d 100644
--- a/boot/image-android.c
+++ b/boot/image-android.c
@@ -299,15 +299,20 @@ int android_image_modify_bootargs_env(const char *cmd, const char *cmd_extra) {
 	char *newbootargs;
 	int len = 0;
 
-	if (bootargs)
-		len += strlen(bootargs);
-
-	if (cmd && *cmd)
+	if (cmd != NULL && cmd && *cmd)
 		len += strlen(cmd) + (len ? 1 : 0); /* +1 for extra space */
 
-	if (cmd_extra && *cmd_extra)
+	if (cmd_extra != NULL && cmd_extra && *cmd_extra)
 		len += strlen(cmd_extra) + (len ? 1 : 0); /* +1 for extra space */
 
+	if (len <=2) {
+		puts("Error: neither bootargs, ccmdline or cmdline_extra found!\n");
+		return 0;
+	}
+
+	if (bootargs != NULL && bootargs && *bootargs)
+		len += strlen(bootargs);
+
 	newbootargs = malloc(len + 2); /* +2 for 2x '\0' */
 
 	if (!newbootargs) {
@@ -317,22 +322,24 @@ int android_image_modify_bootargs_env(const char *cmd, const char *cmd_extra) {
 
 	*newbootargs = '\0'; /* set to Null in case no components below are present */
 
-	if (bootargs && !IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE_PREPEND_ENV_BOOTARGS))
-		strcpy(newbootargs, bootargs);
+	if (bootargs != NULL && bootargs && *bootargs
+	    && !IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE_PREPEND_ENV_BOOTARGS))
+		strcat(newbootargs, bootargs);
 
-	if (cmd && *cmd) {
+	if (cmd != NULL && cmd && *cmd) {
 		if (*newbootargs) /* If there is something in newbootargs, a space is needed */
 				strcat(newbootargs, " ");
 		strcat(newbootargs, cmd);
 	}
 
-	if (cmd_extra && *cmd_extra) {
+	if (cmd_extra != NULL && cmd_extra && *cmd_extra) {
 		if (*newbootargs) /* If there is something in newbootargs, a space is needed */
 				strcat(newbootargs, " ");
 		strcat(newbootargs, cmd_extra);
 	}
 
-	if (bootargs && IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE_PREPEND_ENV_BOOTARGS)) {
+	if (bootargs != NULL && bootargs && *bootargs
+	    && IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE_PREPEND_ENV_BOOTARGS)) {
 		if (*newbootargs) /* If there is something in newbootargs, a space is needed */
 				strcat(newbootargs, " ");
 		strcat(newbootargs, bootargs);

-- 
2.43.0




More information about the U-Boot mailing list