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

Tom Rini trini at konsulko.com
Tue Oct 3 00:46:41 CEST 2023


On Tue, Oct 03, 2023 at 12:27:25AM +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.
> 
> Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt at canonical.com>
> ---
> v2:
> 	Avoid #if.
> ---
>  drivers/input/input.c | 22 ++++++++++++----------
>  1 file changed, 12 insertions(+), 10 deletions(-)
> 
> diff --git a/drivers/input/input.c b/drivers/input/input.c
> index a4341e8c7c..de62189782 100644
> --- a/drivers/input/input.c
> +++ b/drivers/input/input.c
> @@ -669,17 +669,19 @@ 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 ((IS_ENABLED(SPL_BUILD) || CONFIG_IS_ENABLED(ENV_SUPPORT)) &&
> +	    !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;
>  }

This is an example I think of where #if is more readable.

-- 
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20231002/a88aef6a/attachment.sig>


More information about the U-Boot mailing list