[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <20190731091555.1d764ba1@gandalf.local.home>
Date: Wed, 31 Jul 2019 09:15:55 -0400
From: Steven Rostedt <rostedt@...dmis.org>
To: Geert Uytterhoeven <geert+renesas@...der.be>
Cc: Petr Mladek <pmladek@...e.com>,
Sergey Senozhatsky <sergey.senozhatsky@...il.com>,
linux-kernel@...r.kernel.org
Subject: Re: [PATCH] printk: Use strlen/sizeof instead of hardcoded
constants
On Wed, 31 Jul 2019 14:39:22 +0200
Geert Uytterhoeven <geert+renesas@...der.be> wrote:
> Replace hardcoded string length or size constants by proper strlen() or
> sizeof() constructs. As the strings are constant, gcc will reduce the
> lengths or sizes to constants again.
>
> Signed-off-by: Geert Uytterhoeven <geert+renesas@...der.be>
> ---
> Assembler output before/after compared: no change in generated code.
> ---
> include/linux/printk.h | 3 +--
> kernel/printk/printk.c | 12 ++++++------
> 2 files changed, 7 insertions(+), 8 deletions(-)
>
> diff --git a/include/linux/printk.h b/include/linux/printk.h
> index cefd374c47b1f88e..9a85d2eb6ff63460 100644
> --- a/include/linux/printk.h
> +++ b/include/linux/printk.h
> @@ -77,8 +77,7 @@ static inline void console_verbose(void)
> console_loglevel = CONSOLE_LOGLEVEL_MOTORMOUTH;
> }
>
> -/* strlen("ratelimit") + 1 */
> -#define DEVKMSG_STR_MAX_SIZE 10
> +#define DEVKMSG_STR_MAX_SIZE sizeof("ratelimit")
> extern char devkmsg_log_str[];
> struct ctl_table;
>
> diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
> index 1888f6a3b694cb88..e8f332f0ae3595e8 100644
> --- a/kernel/printk/printk.c
> +++ b/kernel/printk/printk.c
> @@ -121,15 +121,15 @@ static int __control_devkmsg(char *str)
> if (!str)
> return -EINVAL;
>
> - if (!strncmp(str, "on", 2)) {
> + if (!strncmp(str, "on", strlen("on"))) {
> devkmsg_log = DEVKMSG_LOG_MASK_ON;
> - return 2;
> - } else if (!strncmp(str, "off", 3)) {
> + return strlen("on");
> + } else if (!strncmp(str, "off", strlen("off"))) {
> devkmsg_log = DEVKMSG_LOG_MASK_OFF;
> - return 3;
> - } else if (!strncmp(str, "ratelimit", 9)) {
> + return strlen("off");
> + } else if (!strncmp(str, "ratelimit", strlen("ratelimit"))) {
Perhaps replacing all these with str_has_prefix() may be a better idea.
-- Steve
> devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT;
> - return 9;
> + return strlen("ratelimit");
> }
> return -EINVAL;
> }
Powered by blists - more mailing lists