linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 05/12] genirq/debugfs: Replace strncmp with str_has_prefix
@ 2019-07-29 15:14 Chuhong Yuan
  2019-07-30  9:17 ` Thomas Gleixner
  0 siblings, 1 reply; 5+ messages in thread
From: Chuhong Yuan @ 2019-07-29 15:14 UTC (permalink / raw)
  Cc: Thomas Gleixner, linux-kernel, Chuhong Yuan

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

Signed-off-by: Chuhong Yuan <hslester96@gmail.com>
---
 kernel/irq/debugfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/irq/debugfs.c b/kernel/irq/debugfs.c
index c1eccd4f6520..85d1e9aeb8ea 100644
--- a/kernel/irq/debugfs.c
+++ b/kernel/irq/debugfs.c
@@ -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;
 
-- 
2.20.1


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

* Re: [PATCH 05/12] genirq/debugfs: Replace strncmp with str_has_prefix
  2019-07-29 15:14 [PATCH 05/12] genirq/debugfs: Replace strncmp with str_has_prefix Chuhong Yuan
@ 2019-07-30  9:17 ` Thomas Gleixner
  2019-07-30 10:58   ` Chuhong Yuan
  0 siblings, 1 reply; 5+ messages in thread
From: Thomas Gleixner @ 2019-07-30  9:17 UTC (permalink / raw)
  To: Chuhong Yuan; +Cc: LKML, Marc Zyngier

On Mon, 29 Jul 2019, Chuhong Yuan wrote:

> 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.

> -	if (!strncmp(buf, "trigger", size)) {
> +	if (str_has_prefix(buf, "trigger")) {

Especially when the resulting code is not equivalent.

Thanks,

	tglx

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

* Re: [PATCH 05/12] genirq/debugfs: Replace strncmp with str_has_prefix
  2019-07-30  9:17 ` Thomas Gleixner
@ 2019-07-30 10:58   ` Chuhong Yuan
  2019-07-30 11:13     ` Marc Zyngier
  0 siblings, 1 reply; 5+ messages in thread
From: Chuhong Yuan @ 2019-07-30 10:58 UTC (permalink / raw)
  To: Thomas Gleixner; +Cc: LKML, Marc Zyngier

Thomas Gleixner <tglx@linutronix.de> 于2019年7月30日周二 下午5:17写道:
>
> On Mon, 29 Jul 2019, Chuhong Yuan wrote:
>
> > 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.

> Just running a script and copying some boiler plate changelog saying
> 'strncmp() is error prone' does not cut it.
>
> > -     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

> Thanks,
>
>         tglx

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

* Re: [PATCH 05/12] genirq/debugfs: Replace strncmp with str_has_prefix
  2019-07-30 10:58   ` Chuhong Yuan
@ 2019-07-30 11:13     ` Marc Zyngier
  2019-07-30 11:20       ` Chuhong Yuan
  0 siblings, 1 reply; 5+ messages in thread
From: Marc Zyngier @ 2019-07-30 11:13 UTC (permalink / raw)
  To: Chuhong Yuan, Thomas Gleixner; +Cc: LKML

On 30/07/2019 11:58, Chuhong Yuan wrote:
> Thomas Gleixner <tglx@linutronix.de> 于2019年7月30日周二 下午5:17写道:
>>
>> On Mon, 29 Jul 2019, Chuhong Yuan wrote:
>>
>>> 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.
> 
>> Just running a script and copying some boiler plate changelog saying
>> 'strncmp() is error prone' does not cut it.
>>
>>> -     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.
-- 
Jazz is not dead, it just smells funny...

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

* Re: [PATCH 05/12] genirq/debugfs: Replace strncmp with str_has_prefix
  2019-07-30 11:13     ` Marc Zyngier
@ 2019-07-30 11:20       ` Chuhong Yuan
  0 siblings, 0 replies; 5+ messages in thread
From: Chuhong Yuan @ 2019-07-30 11:20 UTC (permalink / raw)
  To: Marc Zyngier; +Cc: Thomas Gleixner, LKML

Marc Zyngier <maz@kernel.org> 于2019年7月30日周二 下午7:13写道:
>
> On 30/07/2019 11:58, Chuhong Yuan wrote:
> > Thomas Gleixner <tglx@linutronix.de> 于2019年7月30日周二 下午5:17写道:
> >>
> >> On Mon, 29 Jul 2019, Chuhong Yuan wrote:
> >>
> >>> 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.
> >
> >> Just running a script and copying some boiler plate changelog saying
> >> 'strncmp() is error prone' does not cut it.
> >>
> >>> -     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".
>

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

Regards,
Chuhong

> Thanks,
>
>         M.
> --
> Jazz is not dead, it just smells funny...

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

end of thread, other threads:[~2019-07-30 11:21 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-29 15:14 [PATCH 05/12] genirq/debugfs: Replace strncmp with str_has_prefix Chuhong Yuan
2019-07-30  9:17 ` Thomas Gleixner
2019-07-30 10:58   ` Chuhong Yuan
2019-07-30 11:13     ` Marc Zyngier
2019-07-30 11:20       ` Chuhong Yuan

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).