Skip to contentSkip to conversation

power: Replace strncmp with str_has_prefix

PATCH In review Chuhong Yuan 2 participants 3 messages Jul 29, 2019 – Aug 1, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 7/12 power: Replace strncmp with str_has_prefix

strncmp(str, const, len) is error-prone.
We had better use newly introduced
str_has_prefix() instead of it.

---
 kernel/power/hibernate.c | 8 ++++----
 kernel/power/main.c      | 2 +-
 2 files changed, 5 insertions(+), 5 deletions(-)
kernel/power/hibernate.c +4 -4
index cd7434e6000d..49d4bfdb2b67 100644
@@ -1188,15 +1188,15 @@ static int __init resume_offset_setup(char *str)
 
 static int __init hibernate_setup(char *str)
 {
-	if (!strncmp(str, "noresume", 8)) {
+	if (str_has_prefix(str, "noresume")) {
 		noresume = 1;
-	} else if (!strncmp(str, "nocompress", 10)) {
+	} else if (str_has_prefix(str, "nocompress")) {
 		nocompress = 1;
-	} else if (!strncmp(str, "no", 2)) {
+	} else if (str_has_prefix(str, "no")) {
 		noresume = 1;
 		nohibernate = 1;
 	} else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)
-		   && !strncmp(str, "protect_image", 13)) {
+		   && str_has_prefix(str, "protect_image")) {
 		enable_restore_image_protection();
 	}
 	return 1;
kernel/power/main.c +1 -1
index bdbd605c4215..5e5f64bb3a43 100644
@@ -495,7 +495,7 @@ static suspend_state_t decode_state(const char *buf, size_t n)
 	len = p ? p - buf : n;
 
 	/* Check hibernation first. */
-	if (len == 4 && !strncmp(buf, "disk", len))
+	if (len == 4 && str_has_prefix(buf, "disk"))
 		return PM_SUSPEND_MAX;
 
 #ifdef CONFIG_SUSPEND
Signature · 1 line
2.20.1
#1 lore raw
Quoted 15 lines · Chuhong Yuan

strncmp(str, const, len) is error-prone.
We had better use newly introduced
str_has_prefix() instead of it.

---
 kernel/power/hibernate.c | 8 ++++----
 kernel/power/main.c      | 2 +-
 2 files changed, 5 insertions(+), 5 deletions(-)
kernel/power/hibernate.c +0 -0
index cd7434e6000d..49d4bfdb2b67 100644

kernel/power/hibernate.c static int __init resume_offset_setup(char *str) · line 1194

Quoted 9 lines
kernel/power/hibernate.c +2 -2
@@ -1188,15 +1188,15 @@ static int __init resume_offset_setup(char *str)
 
 static int __init hibernate_setup(char *str)
 {
-	if (!strncmp(str, "noresume", 8)) {
+	if (str_has_prefix(str, "noresume")) {
 		noresume = 1;
-	} else if (!strncmp(str, "nocompress", 10)) {
+	} else if (str_has_prefix(str, "nocompress")) {
 		nocompress = 1;

Ok, old code is bad, too... but this makes the error visible. We do not want "noresumenextmonday" to set noresume = 1, right?

Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#2 lore raw
Quoted 18 lines · Pavel Machek <[email protected]> 于2019年7月30日周二 上午3:15写道:
Quoted 16 lines · Chuhong Yuan

strncmp(str, const, len) is error-prone.
We had better use newly introduced
str_has_prefix() instead of it.

---
 kernel/power/hibernate.c | 8 ++++----
 kernel/power/main.c      | 2 +-
 2 files changed, 5 insertions(+), 5 deletions(-)
kernel/power/hibernate.c +0 -0
index cd7434e6000d..49d4bfdb2b67 100644
@@ -1188,15 +1188,15 @@ static int __init resume_offset_setup(char *str)
 
Quoted 12 lines

kernel/power/hibernate.c static int __init resume_offset_setup(char *str) · line 1194

Quoted 8 lines
kernel/power/hibernate.c +2 -2
 static int __init hibernate_setup(char *str)
 {
-     if (!strncmp(str, "noresume", 8)) {
+     if (str_has_prefix(str, "noresume")) {
              noresume = 1;
-     } else if (!strncmp(str, "nocompress", 10)) {
+     } else if (str_has_prefix(str, "nocompress")) {
              nocompress = 1;

Ok, old code is bad, too... but this makes the error visible. We do not want "noresumenextmonday" to set noresume = 1, right?

I am not clear about the semantic of the code. Therefore, what is the right way to fix this error?

Quoted 5 lines

Pavel

--
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html