Skip to contentSkip to conversation

locking/locktorture: Replace strncmp with str_has_prefix

PATCH Applied Chuhong Yuan 2 participants 2 messages Aug 2, 2019 Torture-test modules

v2 of 4 Applied Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 3/10 locking/locktorture: 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.

---
Changes in v2:
- Newly detected in upstream.

 kernel/locking/locktorture.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/locking/locktorture.c +4 -4
index c513031cd7e3..8dd900247205 100644
@@ -889,16 +889,16 @@ static int __init lock_torture_init(void)
 		cxt.nrealwriters_stress = 2 * num_online_cpus();
 
 #ifdef CONFIG_DEBUG_MUTEXES
-	if (strncmp(torture_type, "mutex", 5) == 0)
+	if (str_has_prefix(torture_type, "mutex"))
 		cxt.debug_lock = true;
 #endif
 #ifdef CONFIG_DEBUG_RT_MUTEXES
-	if (strncmp(torture_type, "rtmutex", 7) == 0)
+	if (str_has_prefix(torture_type, "rtmutex"))
 		cxt.debug_lock = true;
 #endif
 #ifdef CONFIG_DEBUG_SPINLOCK
-	if ((strncmp(torture_type, "spin", 4) == 0) ||
-	    (strncmp(torture_type, "rw_lock", 7) == 0))
+	if ((str_has_prefix(torture_type, "spin")) ||
+	    (str_has_prefix(torture_type, "rw_lock")))
 		cxt.debug_lock = true;
 #endif
 
Signature · 1 line
2.20.1
#1 lore raw
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.

Queued, thank you! I updated the commit log as follows. Please let me know if I messed anything up.

Thanx, Paul

------------------------------------------------------------------------

commit e2be525371b2f6790f6c1ab712c3d273fb1f2bea

Author: Chuhong Yuan <[email protected]>
Date:   Fri Aug 2 09:46:56 2019 +0800

locktorture: Replace strncmp() with str_has_prefix()

The strncmp() function is error-prone because it is easy to get the length wrong, especially if the string is subject to change, especially given the need to account for the terminating nul byte. This commit therefore substitutes the newly introduced str_has_prefix(), which does not require a separately specified length.

kernel/locking/locktorture.c +4 -4
index c513031cd7e3..8dd900247205 100644
@@ -889,16 +889,16 @@ static int __init lock_torture_init(void)
 		cxt.nrealwriters_stress = 2 * num_online_cpus();
 
 #ifdef CONFIG_DEBUG_MUTEXES
-	if (strncmp(torture_type, "mutex", 5) == 0)
+	if (str_has_prefix(torture_type, "mutex"))
 		cxt.debug_lock = true;
 #endif
 #ifdef CONFIG_DEBUG_RT_MUTEXES
-	if (strncmp(torture_type, "rtmutex", 7) == 0)
+	if (str_has_prefix(torture_type, "rtmutex"))
 		cxt.debug_lock = true;
 #endif
 #ifdef CONFIG_DEBUG_SPINLOCK
-	if ((strncmp(torture_type, "spin", 4) == 0) ||
-	    (strncmp(torture_type, "rw_lock", 7) == 0))
+	if ((str_has_prefix(torture_type, "spin")) ||
+	    (str_has_prefix(torture_type, "rw_lock")))
 		cxt.debug_lock = true;
 #endif