Skip to contentSkip to conversation

genirq/debugfs: Replace strncmp with str_has_prefix

PATCH In review Chuhong Yuan 3 participants 5 messages Jul 29, 2019 – Jul 30, 2019 IRQ

lore
Share & tools
lore
#0 lore raw

PATCH 5/12 genirq/debugfs: 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/irq/debugfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/irq/debugfs.c +1 -1
index c1eccd4f6520..85d1e9aeb8ea 100644
@@ -188,7 +188,7 @@ static ssize_t irq_debug_write(struct file *file, const char __user *user_buf,
 	if (copy_from_user(buf, user_buf, size))
 		return -EFAULT;
 
-	if (!strncmp(buf, "trigger", size)) {
+	if (str_has_prefix(buf, "trigger")) {
 		unsigned long flags;
 		int err;
 
Signature · 1 line
2.20.1
#1 lore raw
Quoted 3 lines · Chuhong Yuan

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

Can you please provide a proper explanation why the below strncmp() is error prone?

Just running a script and copying some boiler plate changelog saying 'strncmp() is error prone' does not cut it.

kernel/irq/debugfs.c static ssize_t irq_debug_write(struct file *file, const char __user *user_buf, · line 191

Quoted 2 lines
kernel/irq/debugfs.c +1 -1
-	if (!strncmp(buf, "trigger", size)) {
+	if (str_has_prefix(buf, "trigger")) {

Especially when the resulting code is not equivalent.

Thanks,

tglx

#2 lore raw
Quoted 10 lines · Thomas Gleixner <[email protected]> 于2019年7月30日周二 下午5:17写道:
Quoted 3 lines · Chuhong Yuan

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

Can you please provide a proper explanation why the below strncmp() is error prone?

If the size is less than 7, for example, 2, then even if buf is "tr", the result will still be true. This is an error.
strncmp(str, const, len) is error-prone mainly because the len is easy to be wrong.

Quoted 8 lines

Just running a script and copying some boiler plate changelog saying 'strncmp() is error prone' does not cut it.

kernel/irq/debugfs.c static ssize_t irq_debug_write(struct file *file, const char __user *user_buf, · line 191

Quoted 2 lines
kernel/irq/debugfs.c +1 -1
-     if (!strncmp(buf, "trigger", size)) {
+     if (str_has_prefix(buf, "trigger")) {

Especially when the resulting code is not equivalent.

I think here the semantic is the comparison should only return true when buf is "trigger".
The buf's size is 8 and the string's size is at most 7.
Since str_has_prefix()'s implementation is strncmp(str, prefix, strlen(prefix)), here strlen(prefix) = 7, I think it satisfies the requirement.

Regards,
Chuhong

Quoted 3 lines

Thanks,

tglx

#3 lore raw
Quoted 20 lines · Chuhong Yuan
Quoted 10 lines · Thomas Gleixner <[email protected]> 于2019年7月30日周二 下午5:17写道:
Quoted 3 lines · Chuhong Yuan

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

Can you please provide a proper explanation why the below strncmp() is error prone?

If the size is less than 7, for example, 2, then even if buf is "tr", the result will still be true. This is an error.
strncmp(str, const, len) is error-prone mainly because the len is easy to be wrong.

Quoted 3 lines

Just running a script and copying some boiler plate changelog saying 'strncmp() is error prone' does not cut it.

Quoted 8 lines
Quoted 5 lines

kernel/irq/debugfs.c static ssize_t irq_debug_write(struct file *file, const char __user *user_buf, · line 191

Quoted 2 lines
kernel/irq/debugfs.c +1 -1
-     if (!strncmp(buf, "trigger", size)) {
+     if (str_has_prefix(buf, "trigger")) {

Especially when the resulting code is not equivalent.

I think here the semantic is the comparison should only return true when buf is "trigger".

Not quite. It will satisfy the condition for 't', 'tr', 'trig', 'trigger', and of course 'triggerthissillyinterruptwhichImdebugging'.

I agree that the semantic is a bit bizarre and maybe not quite expected, but still... You seem to be changing the semantic without any justification other than "this is safer".

Thanks,

	M.
Signature · 1 line
Jazz is not dead, it just smells funny...
#4 lore raw
Quoted 31 lines · Marc Zyngier <[email protected]> 于2019年7月30日周二 下午7:13写道:
Quoted 20 lines · Chuhong Yuan
Quoted 10 lines · Thomas Gleixner <[email protected]> 于2019年7月30日周二 下午5:17写道:
Quoted 3 lines · Chuhong Yuan

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

Can you please provide a proper explanation why the below strncmp() is error prone?

If the size is less than 7, for example, 2, then even if buf is "tr", the result will still be true. This is an error.
strncmp(str, const, len) is error-prone mainly because the len is easy to be wrong.

Quoted 3 lines

Just running a script and copying some boiler plate changelog saying 'strncmp() is error prone' does not cut it.

Quoted 8 lines
Quoted 5 lines

kernel/irq/debugfs.c static ssize_t irq_debug_write(struct file *file, const char __user *user_buf, · line 191

Quoted 2 lines
kernel/irq/debugfs.c +1 -1
-     if (!strncmp(buf, "trigger", size)) {
+     if (str_has_prefix(buf, "trigger")) {

Especially when the resulting code is not equivalent.

I think here the semantic is the comparison should only return true when buf is "trigger".

Quoted 7 lines

Not quite. It will satisfy the condition for 't', 'tr', 'trig', 'trigger', and of course 'triggerthissillyinterruptwhichImdebugging'.

I agree that the semantic is a bit bizarre and maybe not quite expected, but still... You seem to be changing the semantic without any justification other than "this is safer".

I am sorry about that... It is my fault.
I will improve my script and avoid such mistakes.
Thanks for your correction.

Regards,
Chuhong

Quoted 5 lines

Thanks,

        M.

--
Jazz is not dead, it just smells funny...