[PATCH] input: avoid NULL dereference

Before using the result of env_get("stdin") we must check if it is NULL.
Use attribute __maybe unused instead of dummy assignment.
Signed-off-by: Heinrich Schuchardt heinrich.schuchardt@canonical.com --- drivers/input/input.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-)
diff --git a/drivers/input/input.c b/drivers/input/input.c index a4341e8c7c..2330b21f71 100644 --- a/drivers/input/input.c +++ b/drivers/input/input.c @@ -666,19 +666,22 @@ int input_init(struct input_config *config, int leds)
int input_stdio_register(struct stdio_dev *dev) { - int error; + __maybe_unused int error;
error = stdio_register(dev); #if !defined(CONFIG_SPL_BUILD) || CONFIG_IS_ENABLED(ENV_SUPPORT) - /* check if this is the standard input device */ - if (!error && strcmp(env_get("stdin"), dev->name) == 0) { - /* reassign the console */ - if (OVERWRITE_CONSOLE || - console_assign(stdin, dev->name)) - return -1; + if (!error) { + const char *cstdin; + + /* check if this is the standard input device */ + cstdin = env_get("stdin"); + if (cstdin && !strcmp(cstdin, dev->name)) { + /* reassign the console */ + if (OVERWRITE_CONSOLE || + console_assign(stdin, dev->name)) + return -1; + } } -#else - error = error; #endif
return 0;

On Mon, 2 Oct 2023 at 11:26, Heinrich Schuchardt heinrich.schuchardt@canonical.com wrote:
Before using the result of env_get("stdin") we must check if it is NULL.
Use attribute __maybe unused instead of dummy assignment.
Signed-off-by: Heinrich Schuchardt heinrich.schuchardt@canonical.com
drivers/input/input.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-)
Reviewed-by: Simon Glass sjg@chromium.org
But is it not possible to use if() instead of #if ?
diff --git a/drivers/input/input.c b/drivers/input/input.c index a4341e8c7c..2330b21f71 100644 --- a/drivers/input/input.c +++ b/drivers/input/input.c @@ -666,19 +666,22 @@ int input_init(struct input_config *config, int leds)
int input_stdio_register(struct stdio_dev *dev) {
int error;
__maybe_unused int error; error = stdio_register(dev);
#if !defined(CONFIG_SPL_BUILD) || CONFIG_IS_ENABLED(ENV_SUPPORT)
/* check if this is the standard input device */
if (!error && strcmp(env_get("stdin"), dev->name) == 0) {
/* reassign the console */
if (OVERWRITE_CONSOLE ||
console_assign(stdin, dev->name))
return -1;
if (!error) {
const char *cstdin;
/* check if this is the standard input device */
cstdin = env_get("stdin");
if (cstdin && !strcmp(cstdin, dev->name)) {
/* reassign the console */
if (OVERWRITE_CONSOLE ||
console_assign(stdin, dev->name))
return -1;
} }
-#else
error = error;
#endif
return 0;
-- 2.40.1
Regards, Simon
participants (2)
-
Heinrich Schuchardt
-
Simon Glass