Skip to contentSkip to conversation

printk: Replace strncmp with str_has_prefix

PATCH Superseded Chuhong Yuan 2 participants 2 messages Aug 5, 2019 – Aug 6, 2019 Printk

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 4/8 printk: Replace strncmp with str_has_prefix

strncmp(str, const, len) is error-prone because len is easy to have typo.
The example is the hard-coded len has counting error or sizeof(const) forgets - 1.
So we prefer using newly introduced str_has_prefix() to substitute such strncmp to make code better.

---
Changes in v3:
- Revise the description.
- Remove else uses in printk.c.

 kernel/printk/braille.c | 10 ++++++----
 kernel/printk/printk.c  | 19 +++++++++++++------
 2 files changed, 19 insertions(+), 10 deletions(-)
kernel/printk/braille.c +6 -4
index 1d21ebacfdb8..e451b8b1d3d5 100644
@@ -11,11 +11,13 @@
 
 int _braille_console_setup(char **str, char **brl_options)
 {
-	if (!strncmp(*str, "brl,", 4)) {
+	size_t len;
+
+	if ((len = str_has_prefix(*str, "brl,"))) {
 		*brl_options = "";
-		*str += 4;
-	} else if (!strncmp(*str, "brl=", 4)) {
-		*brl_options = *str + 4;
+		*str += len;
+	} else if ((len = str_has_prefix(*str, "brl="))) {
+		*brl_options = *str + len;
 		*str = strchr(*brl_options, ',');
 		if (!*str) {
 			pr_err("need port name after brl=\n");
kernel/printk/printk.c +13 -6
index 1888f6a3b694..6b8d9cfebc0b 100644
@@ -118,19 +118,26 @@ static unsigned int __read_mostly devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT;
 
 static int __control_devkmsg(char *str)
 {
+	size_t len;
+
 	if (!str)
 		return -EINVAL;
 
-	if (!strncmp(str, "on", 2)) {
+	if ((len = str_has_prefix(str, "on"))) {
 		devkmsg_log = DEVKMSG_LOG_MASK_ON;
-		return 2;
-	} else if (!strncmp(str, "off", 3)) {
+		return len;
+	}
+
+	if ((len = str_has_prefix(str, "off"))) {
 		devkmsg_log = DEVKMSG_LOG_MASK_OFF;
-		return 3;
-	} else if (!strncmp(str, "ratelimit", 9)) {
+		return len;
+	}
+
+	if ((len = str_has_prefix(str, "ratelimit"))) {
 		devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT;
-		return 9;
+		return len;
 	}
+
 	return -EINVAL;
 }
 
Signature · 1 line
2.20.1
#1 lore raw

Hi Chuhong,

Quoted 8 lines · Chuhong Yuan

strncmp(str, const, len) is error-prone because len is easy to have typo.
The example is the hard-coded len has counting error or sizeof(const) forgets - 1.
So we prefer using newly introduced str_has_prefix() to substitute such strncmp to make code better.

Thanks for your patch!

kernel/printk/braille.c line 16

Quoted 10 lines
kernel/printk/braille.c +3 -1
@@ -11,11 +11,13 @@
 
 int _braille_console_setup(char **str, char **brl_options)
 {
-       if (!strncmp(*str, "brl,", 4)) {
+       size_t len;
+
+       if ((len = str_has_prefix(*str, "brl,"))) {

Please write this as

    len = str_has_prefix(*str, "brl,");
    if (len) {

(everywhere)

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds