[PATCH v3 1/1] input: avoid NULL dereference

Before using the result of env_get("stdin") we must check if it is NULL.
Avoid #if. This resolves the -Wunused-but-set-variable issue and we don't need a dummy assignment in the else branch. Anyway this warning is disabled in the Makefile.
For sake of readability use an early return after the configuration check.
Checking CONFIG_SPL_BUILD is incorrect as env_get() is only defined if CONFIG_$(SPL_TPL)ENV_SUPPORT=y.
Fixes: 985ca3945fa3 ("spl: input: Allow input in SPL and TPL") Signed-off-by: Heinrich Schuchardt heinrich.schuchardt@canonical.com --- v2: avoid #if
v3: use an early return after configuration check remove CONFIG_SPL_BUILD check --- drivers/input/input.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-)
diff --git a/drivers/input/input.c b/drivers/input/input.c index a4341e8c7c..743ba89f77 100644 --- a/drivers/input/input.c +++ b/drivers/input/input.c @@ -669,17 +669,22 @@ int input_stdio_register(struct stdio_dev *dev) 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 (!CONFIG_IS_ENABLED(ENV_SUPPORT)) + return 0; + + 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 19:09, Heinrich Schuchardt heinrich.schuchardt@canonical.com wrote:
Before using the result of env_get("stdin") we must check if it is NULL.
Avoid #if. This resolves the -Wunused-but-set-variable issue and we don't need a dummy assignment in the else branch. Anyway this warning is disabled in the Makefile.
For sake of readability use an early return after the configuration check.
Checking CONFIG_SPL_BUILD is incorrect as env_get() is only defined if CONFIG_$(SPL_TPL)ENV_SUPPORT=y.
Fixes: 985ca3945fa3 ("spl: input: Allow input in SPL and TPL") Signed-off-by: Heinrich Schuchardt heinrich.schuchardt@canonical.com
v2: avoid #if
v3: use an early return after configuration check remove CONFIG_SPL_BUILD check
drivers/input/input.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-)
Reviewed-by: Simon Glass sjg@chromium.org

On Tue, Oct 03, 2023 at 03:09:01AM +0200, Heinrich Schuchardt wrote:
Before using the result of env_get("stdin") we must check if it is NULL.
Avoid #if. This resolves the -Wunused-but-set-variable issue and we don't need a dummy assignment in the else branch. Anyway this warning is disabled in the Makefile.
For sake of readability use an early return after the configuration check.
Checking CONFIG_SPL_BUILD is incorrect as env_get() is only defined if CONFIG_$(SPL_TPL)ENV_SUPPORT=y.
Fixes: 985ca3945fa3 ("spl: input: Allow input in SPL and TPL") Signed-off-by: Heinrich Schuchardt heinrich.schuchardt@canonical.com
Reviewed-by: Tom Rini trini@konsulko.com

On Tue, Oct 03, 2023 at 03:09:01AM +0200, Heinrich Schuchardt wrote:
Before using the result of env_get("stdin") we must check if it is NULL.
Avoid #if. This resolves the -Wunused-but-set-variable issue and we don't need a dummy assignment in the else branch. Anyway this warning is disabled in the Makefile.
For sake of readability use an early return after the configuration check.
Checking CONFIG_SPL_BUILD is incorrect as env_get() is only defined if CONFIG_$(SPL_TPL)ENV_SUPPORT=y.
Fixes: 985ca3945fa3 ("spl: input: Allow input in SPL and TPL") Signed-off-by: Heinrich Schuchardt heinrich.schuchardt@canonical.com Reviewed-by: Simon Glass sjg@chromium.org Reviewed-by: Tom Rini trini@konsulko.com
Applied to u-boot/master, thanks!
participants (3)
-
Heinrich Schuchardt
-
Simon Glass
-
Tom Rini