[PATCH 2/5] dmesg: error out on unknown loglevels

Marco Felsch m.felsch at pengutronix.de
Mon Jun 12 23:57:18 PDT 2023


On 23-06-13, Sascha Hauer wrote:
> On Tue, Jun 13, 2023 at 08:34:39AM +0200, Marco Felsch wrote:
> > Hi Sascha,
> > 
> > On 23-06-12, Sascha Hauer wrote:
> > > The user deserves an error when an unknown loglevel is given, so
> > > print an error instead of silently ignoring unknown loglevels.
> > > 
> > > Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
> > > ---
> > >  commands/dmesg.c | 6 +++++-
> > >  1 file changed, 5 insertions(+), 1 deletion(-)
> > > 
> > > diff --git a/commands/dmesg.c b/commands/dmesg.c
> > > index 339910bb76..2a36710b98 100644
> > > --- a/commands/dmesg.c
> > > +++ b/commands/dmesg.c
> > > @@ -32,6 +32,8 @@ static int str_to_loglevel(const char *str)
> > >  	if (!strcmp(str, "emerg"))
> > >  		return MSG_EMERG;
> > >  
> > > +	printf("dmesg: unknown loglevel %s\n", str);
> > > +
> > >  	return -EINVAL;
> > >  }
> > >  
> > > @@ -51,6 +53,8 @@ static unsigned dmesg_get_levels(const char *__args)
> > >  		level = str_to_loglevel(str);
> > >  		if (level >= 0)
> > >  			flags |= BIT(level);
> > > +		else
> > > +			return 0;
> > 
> > This is more explicit but wouldn't be required since we initialized
> > 'flags = 0'.
> 
> It is required as flags might be != 0 already and we want to return 0
> in error case. Nevertheless this needs fixing as this return looses
> memory allocated with xstrdup above.

Argh.. right I overlooked the while(1).

> > >  	}
> > >  
> > >  	free(args);
> > > @@ -81,7 +85,7 @@ static int do_dmesg(int argc, char *argv[])
> > >  		case 'l':
> > >  			levels = dmesg_get_levels(optarg);
> > >  			if (!levels)
> > > -				return COMMAND_ERROR_USAGE;
> > > +				return COMMAND_ERROR;
> > 
> > Out of curiosity what's the difference between COMMAND_ERROR and
> > COMMAND_ERROR_USAGE?
> 
> When a command returns with COMMAND_ERROR_USAGE then the usage
> for the command is printed. COMMAND_ERROR just returns an error
> without printing the commands usage.
> 
> When a command prints a meaningful error message then I often
> find it confusing when the usage is printed, because the error
> is often buried in the long help text.

Thanks for the clarification, make sense.

> ---------------------------8<------------------------------
> 
> From adadd6c0fcfd6b11076e0110497dd7ecdb44f99c Mon Sep 17 00:00:00 2001
> From: Sascha Hauer <s.hauer at pengutronix.de>
> Date: Mon, 12 Jun 2023 14:12:11 +0200
> Subject: [PATCH] dmesg: error out on unknown loglevels
> 
> The user deserves an error when an unknown loglevel is given, so
> print an error instead of silently ignoring unknown loglevels.
> 
> Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>

Reviewed-by: Marco Felsch <m.felsch at pengutronix.de>

> ---
>  commands/dmesg.c | 12 +++++++++---
>  1 file changed, 9 insertions(+), 3 deletions(-)
> 
> diff --git a/commands/dmesg.c b/commands/dmesg.c
> index 339910bb76..0d46353fb5 100644
> --- a/commands/dmesg.c
> +++ b/commands/dmesg.c
> @@ -32,6 +32,8 @@ static int str_to_loglevel(const char *str)
>  	if (!strcmp(str, "emerg"))
>  		return MSG_EMERG;
>  
> +	printf("dmesg: unknown loglevel %s\n", str);
> +
>  	return -EINVAL;
>  }
>  
> @@ -49,8 +51,12 @@ static unsigned dmesg_get_levels(const char *__args)
>  			break;
>  
>  		level = str_to_loglevel(str);
> -		if (level >= 0)
> -			flags |= BIT(level);
> +		if (level < 0) {
> +			flags = 0;
> +			break;
> +		}
> +
> +		flags |= BIT(level);
>  	}
>  
>  	free(args);
> @@ -81,7 +87,7 @@ static int do_dmesg(int argc, char *argv[])
>  		case 'l':
>  			levels = dmesg_get_levels(optarg);
>  			if (!levels)
> -				return COMMAND_ERROR_USAGE;
> +				return COMMAND_ERROR;
>  			break;
>  		case 'r':
>  			flags |= BAREBOX_LOG_PRINT_RAW | BAREBOX_LOG_PRINT_TIME;
> -- 
> 2.39.2
> 
> -- 
> Pengutronix e.K.                           |                             |
> Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
> 31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
> Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
> 



More information about the barebox mailing list