common/command.c: Avoid NULL pointer use in cmd_auto_complete
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
@@ -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
Hi Adam,
Quoted 12 lines · Adam Lackorzynski
common/command.c: Avoid NULL pointer use incmd_auto_completeAvoid putting a potential NULL pointer to strcmp, which would lead to a load at address 0.
Signed-off-by Adam Lackorzynski <[email protected]>
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
Hi Simon,
Quoted 43 lines · Simon Glass
Hi Adam,
Quoted 12 lines · Adam Lackorzynski
common/command.c: Avoid NULL pointer use incmd_auto_completeAvoid putting a potential NULL pointer to strcmp, which would lead to a load at address 0.
Signed-off-by Adam Lackorzynski <[email protected]>
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()incommon/cli_hush.cfalls back toCONFIG_SYS_PROMPT(orCONFIG_SYS_PROMPT_HUSH_PS2for 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