Skip to contentSkip to conversation

common/command.c: Avoid NULL pointer use in cmd_auto_complete

PATCH Superseded Adam Lackorzynski 2 participants 3 messages May 14 – May 15

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 common/command.c: Avoid NULL pointer use in cmd_auto_complete

Avoid putting a potential NULL pointer to strcmp, which would lead to a load at address 0.

---
 common/command.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
common/command.c +1 -1
index 0f9dd06d72b..55bd9e44b1f 100644
@@ -373,7 +373,7 @@ int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp)
 	const char *ps_prompt = CONFIG_SYS_PROMPT;
 #endif
 
-	if (strcmp(prompt, ps_prompt) != 0)
+	if (ps_prompt && strcmp(prompt, ps_prompt) != 0)
 		return 0;	/* not in normal console */
 
 	cnt = strlen(buf);
Signature · 1 line
2.53.0
#1 lore raw

Hi Adam,

Quoted 12 lines · Adam Lackorzynski

common/command.c: Avoid NULL pointer use in cmd_auto_complete

Avoid putting a potential NULL pointer to strcmp, which would lead to a load at address 0.

common/command.c | 2 +-

 1 file changed, 1 insertion(+), 1 deletion(-)

common/command.c int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp) · line 377

Quoted 6 lines
common/command.c +1 -1
@@ -373,7 +373,7 @@ int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp)
      const char *ps_prompt = CONFIG_SYS_PROMPT;
 #endif
 
-     if (strcmp(prompt, ps_prompt) != 0)
+     if (ps_prompt && strcmp(prompt, ps_prompt) != 0)
              return 0;       /* not in normal console */

This avoids the deref but isn't quite right. uboot_cli_readline() in common/cli_hush.c falls back to CONFIG_SYS_PROMPT (or CONFIG_SYS_PROMPT_HUSH_PS2 for promptmode 2) when PS1 is unset, so prompt here is never NULL. With your change, a NULL PS1 makes the comparison succeed unconditionally and promptmode 2 gets treated as the normal console.

Please can you fall back to the default when env_get() returns NULL, mirroring the readline path:

    const char *ps_prompt;

    if (IS_ENABLED(CONFIG_CMDLINE_PS_SUPPORT)) {
        ps_prompt = env_get('PS1');

        if (!ps_prompt)
                ps_prompt = CONFIG_SYS_PROMPT;
    } else {
        ps_prompt = CONFIG_SYS_PROMPT;
    }

What do you think?

Regards,
Simon

#2 lore raw

Hi Simon,

Quoted 43 lines · Simon Glass

Hi Adam,

Quoted 12 lines · Adam Lackorzynski

common/command.c: Avoid NULL pointer use in cmd_auto_complete

Avoid putting a potential NULL pointer to strcmp, which would lead to a load at address 0.

common/command.c | 2 +-

 1 file changed, 1 insertion(+), 1 deletion(-)

common/command.c int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp) · line 377

Quoted 6 lines
common/command.c +1 -1
@@ -373,7 +373,7 @@ int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp)
      const char *ps_prompt = CONFIG_SYS_PROMPT;
 #endif
 
-     if (strcmp(prompt, ps_prompt) != 0)
+     if (ps_prompt && strcmp(prompt, ps_prompt) != 0)
              return 0;       /* not in normal console */

This avoids the deref but isn't quite right. uboot_cli_readline() in common/cli_hush.c falls back to CONFIG_SYS_PROMPT (or CONFIG_SYS_PROMPT_HUSH_PS2 for promptmode 2) when PS1 is unset, so prompt here is never NULL. With your change, a NULL PS1 makes the comparison succeed unconditionally and promptmode 2 gets treated as the normal console.

Please can you fall back to the default when env_get() returns NULL, mirroring the readline path:

    const char *ps_prompt;

    if (IS_ENABLED(CONFIG_CMDLINE_PS_SUPPORT)) {
        ps_prompt = env_get('PS1');

        if (!ps_prompt)
                ps_prompt = CONFIG_SYS_PROMPT;
    } else {
        ps_prompt = CONFIG_SYS_PROMPT;
    }
Quoted 1 line

What do you think?

Thanks, looks good to me, will send a v2.

BR, Adam