linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] printk: Use strlen/sizeof instead of hardcoded constants
@ 2019-07-31 12:39 Geert Uytterhoeven
  2019-07-31 13:15 ` Steven Rostedt
  0 siblings, 1 reply; 2+ messages in thread
From: Geert Uytterhoeven @ 2019-07-31 12:39 UTC (permalink / raw)
  To: Petr Mladek, Sergey Senozhatsky
  Cc: Steven Rostedt, linux-kernel, Geert Uytterhoeven

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@glider.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"))) {
 		devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT;
-		return 9;
+		return strlen("ratelimit");
 	}
 	return -EINVAL;
 }
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] printk: Use strlen/sizeof instead of hardcoded constants
  2019-07-31 12:39 [PATCH] printk: Use strlen/sizeof instead of hardcoded constants Geert Uytterhoeven
@ 2019-07-31 13:15 ` Steven Rostedt
  0 siblings, 0 replies; 2+ messages in thread
From: Steven Rostedt @ 2019-07-31 13:15 UTC (permalink / raw)
  To: Geert Uytterhoeven; +Cc: Petr Mladek, Sergey Senozhatsky, linux-kernel

On Wed, 31 Jul 2019 14:39:22 +0200
Geert Uytterhoeven <geert+renesas@glider.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@glider.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;
>  }


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-07-31 13:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-31 12:39 [PATCH] printk: Use strlen/sizeof instead of hardcoded constants Geert Uytterhoeven
2019-07-31 13:15 ` Steven Rostedt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).