2012-08-06 10:21:20

by Pavel Machek

[permalink] [raw]
Subject: Re: [linux-pm] [PATCH] ACPI: replace strlen("string") with sizeof("string") -1

On Thu 2012-07-26 21:39:38, Len Brown wrote:
> ...both give the number of chars in the string
> without the '\0', as strncmp() wants,
> but sizeof() is compile-time.

What about introducing something like streq() to do this
automatically? This is ugly....

#define streq(a, b) ... if (_buildin_constant(b)) ...

?

> - if (!strncmp(val, "enable", strlen("enable"))) {
> + if (!strncmp(val, "enable", sizeof("enable") - 1)) {

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


2012-08-06 14:37:00

by Alan Stern

[permalink] [raw]
Subject: Re: [linux-pm] [PATCH] ACPI: replace strlen("string") with sizeof("string") -1

On Mon, 6 Aug 2012, Pavel Machek wrote:

> On Thu 2012-07-26 21:39:38, Len Brown wrote:
> > ...both give the number of chars in the string
> > without the '\0', as strncmp() wants,
> > but sizeof() is compile-time.
>
> What about introducing something like streq() to do this
> automatically? This is ugly....
>
> #define streq(a, b) ... if (_buildin_constant(b)) ...
>
> ?
>
> > - if (!strncmp(val, "enable", strlen("enable"))) {
> > + if (!strncmp(val, "enable", sizeof("enable") - 1)) {

While you're at it, there's no point using strncmp when you know the
length of one of the strings beforehand. Just use memcmp, and don't
subtract 1 from the sizeof value.

Alan Stern

2012-08-06 16:04:19

by Pavel Vasilyev

[permalink] [raw]
Subject: Re: [linux-pm] [PATCH] ACPI: replace strlen("string") with sizeof("string") -1

06.08.2012 18:36, Alan Stern пишет:
> On Mon, 6 Aug 2012, Pavel Machek wrote:
>
>> On Thu 2012-07-26 21:39:38, Len Brown wrote:
>>> ...both give the number of chars in the string
>>> without the '\0', as strncmp() wants,
>>> but sizeof() is compile-time.
>>
>> What about introducing something like streq() to do this
>> automatically? This is ugly....
>>
>> #define streq(a, b) ... if (_buildin_constant(b)) ...
>>
>> ?
>>
>>> - if (!strncmp(val, "enable", strlen("enable"))) {
>>> + if (!strncmp(val, "enable", sizeof("enable") - 1)) {
>
> While you're at it, there's no point using strncmp when you know the
> length of one of the strings beforehand. Just use memcmp, and don't
> subtract 1 from the sizeof value.

http://www.gossamer-threads.com/lists/engine?do=post_attachment;postatt_id=41157;list=linux

:)




--

Pavel.

2012-08-06 16:28:35

by Alan Stern

[permalink] [raw]
Subject: Re: [linux-pm] [PATCH] ACPI: replace strlen("string") with sizeof("string") -1

On Mon, 6 Aug 2012, Pavel Vasilyev wrote:

> 06.08.2012 18:36, Alan Stern пишет:
> > On Mon, 6 Aug 2012, Pavel Machek wrote:
> >
> >> On Thu 2012-07-26 21:39:38, Len Brown wrote:
> >>> ...both give the number of chars in the string
> >>> without the '\0', as strncmp() wants,
> >>> but sizeof() is compile-time.
> >>
> >> What about introducing something like streq() to do this
> >> automatically? This is ugly....
> >>
> >> #define streq(a, b) ... if (_buildin_constant(b)) ...
> >>
> >> ?
> >>
> >>> - if (!strncmp(val, "enable", strlen("enable"))) {
> >>> + if (!strncmp(val, "enable", sizeof("enable") - 1)) {
> >
> > While you're at it, there's no point using strncmp when you know the
> > length of one of the strings beforehand. Just use memcmp, and don't
> > subtract 1 from the sizeof value.
>
> http://www.gossamer-threads.com/lists/engine?do=post_attachment;postatt_id=41157;list=linux

Interestingly, many (all?) of the changes in that patch are wrong
because they don't try to match the terminating '\0'. As a result,
they will match against extensions of the target string as well as the
target string itself.

Alan Stern

2012-08-06 18:48:22

by Pavel Vasilyev

[permalink] [raw]
Subject: Re: [linux-pm] [PATCH] ACPI: replace strlen("string") with sizeof("string") -1

06.08.2012 20:28, Alan Stern пишет:
> On Mon, 6 Aug 2012, Pavel Vasilyev wrote:
>
>> 06.08.2012 18:36, Alan Stern пишет:
>>> On Mon, 6 Aug 2012, Pavel Machek wrote:
>>>
>>>> On Thu 2012-07-26 21:39:38, Len Brown wrote:
>>>>> ...both give the number of chars in the string
>>>>> without the '\0', as strncmp() wants,
>>>>> but sizeof() is compile-time.
>>>>
>>>> What about introducing something like streq() to do this
>>>> automatically? This is ugly....
>>>>
>>>> #define streq(a, b) ... if (_buildin_constant(b)) ...
>>>>
>>>> ?
>>>>
>>>>> - if (!strncmp(val, "enable", strlen("enable"))) {
>>>>> + if (!strncmp(val, "enable", sizeof("enable") - 1)) {
>>>
>>> While you're at it, there's no point using strncmp when you know the
>>> length of one of the strings beforehand. Just use memcmp, and don't
>>> subtract 1 from the sizeof value.
>>
>> http://www.gossamer-threads.com/lists/engine?do=post_attachment;postatt_id=41157;list=linux
>
> Interestingly, many (all?) of the changes in that patch are wrong
> because they don't try to match the terminating '\0'. As a result,
> they will match against extensions of the target string as well as the
> target string itself.
>

strNcmp compare N bytes - http://lxr.linux.no/#linux+v3.5/lib/string.c#L270
memcmp compare N bytes - http://lxr.linux.no/#linux+v3.5/lib/string.c#L651

--

Pavel.

2012-08-06 19:59:40

by Alan Stern

[permalink] [raw]
Subject: Re: [linux-pm] [PATCH] ACPI: replace strlen("string") with sizeof("string") -1

On Mon, 6 Aug 2012, Pavel Vasilyev wrote:

> >> http://www.gossamer-threads.com/lists/engine?do=post_attachment;postatt_id=41157;list=linux
> >
> > Interestingly, many (all?) of the changes in that patch are wrong
> > because they don't try to match the terminating '\0'. As a result,
> > they will match against extensions of the target string as well as the
> > target string itself.
> >
>
> strNcmp compare N bytes - http://lxr.linux.no/#linux+v3.5/lib/string.c#L270
> memcmp compare N bytes - http://lxr.linux.no/#linux+v3.5/lib/string.c#L651

Yes. So if s contains "abcde" then

memcmp(s, "abc", 3) and strncmp(s, "abc", 3) will both return 0, and
memcmp(s, "abc", 4) and strncmp(s, "abc", 4) will both return 1.

Alan Stern

2012-08-06 23:06:57

by Daniel Taylor

[permalink] [raw]
Subject: RE: [linux-pm] [PATCH] ACPI: replace strlen("string") with sizeof("string") -1

Silly question: when did sizeof("string") get changed to be anything
other than the size of the pointer ("string" is, after all, an array
of characters)?

> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]] On Behalf Of Alan Stern
> Sent: Monday, August 06, 2012 1:00 PM
> To: Pavel Vasilyev
> Cc: Pavel Machek; Len Brown; [email protected];
> [email protected];
> [email protected]; Len Brown
> Subject: Re: [linux-pm] [PATCH] ACPI: replace
> strlen("string") with sizeof("string") -1
>
> On Mon, 6 Aug 2012, Pavel Vasilyev wrote:
>
> > >>
> http://www.gossamer-threads.com/lists/engine?do=post_attachmen
> t;postatt_id=41157;list=linux
> > >
> > > Interestingly, many (all?) of the changes in that patch are wrong
> > > because they don't try to match the terminating '\0'. As
> a result,
> > > they will match against extensions of the target string
> as well as the
> > > target string itself.
> > >
> >
> > strNcmp compare N bytes -
> http://lxr.linux.no/#linux+v3.5/lib/string.c#L270
> > memcmp compare N bytes -
> http://lxr.linux.no/#linux+v3.5/lib/string.c#L651
>
> Yes. So if s contains "abcde" then
>
> memcmp(s, "abc", 3) and strncmp(s, "abc", 3) will both
> return 0, and
> memcmp(s, "abc", 4) and strncmp(s, "abc", 4) will both return 1.
>
> Alan Stern
>
> --
> To unsubscribe from this list: send the line "unsubscribe
> linux-kernel" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at http://www.tux.org/lkml/
> -

2012-08-07 01:07:37

by Pavel Vasilyev

[permalink] [raw]
Subject: Re: [linux-pm] [PATCH] ACPI: replace strlen("string") with sizeof("string") -1

06.08.2012 23:59, Alan Stern пишет:
> On Mon, 6 Aug 2012, Pavel Vasilyev wrote:
>
>>>> http://www.gossamer-threads.com/lists/engine?do=post_attachment;postatt_id=41157;list=linux
>>>
>>> Interestingly, many (all?) of the changes in that patch are wrong
>>> because they don't try to match the terminating '\0'. As a result,
>>> they will match against extensions of the target string as well as the
>>> target string itself.
>>>
>>
>> strNcmp compare N bytes - http://lxr.linux.no/#linux+v3.5/lib/string.c#L270
>> memcmp compare N bytes - http://lxr.linux.no/#linux+v3.5/lib/string.c#L651
>
> Yes. So if s contains "abcde" then
>
> memcmp(s, "abc", 3) and strncmp(s, "abc", 3) will both return 0, and
> memcmp(s, "abc", 4) and strncmp(s, "abc", 4) will both return 1.

No matter what is contained in *s, "abcde" or "abcxxx",
are important first N bytes. The second example, you see,
a little bit stupid, and devoid of logic. :)


--

Pavel.

2012-08-07 13:44:56

by Bernd Petrovitsch

[permalink] [raw]
Subject: RE: [linux-pm] [PATCH] ACPI: replace strlen("string") with sizeof("string") -1

On Mon, 2012-08-06 at 22:57 +0000, Daniel Taylor wrote:
> Silly question: when did sizeof("string") get changed to be anything
> other than the size of the pointer ("string" is, after all, an array
> of characters)?

It is since K&R times that way.
If you do not know the difference between a pointer and an array (and
these are vastly different), go learn something new about C.

Bernd
--
Bernd Petrovitsch Email : [email protected]
LUGA : http://www.luga.at

2012-08-07 17:24:51

by Alan Stern

[permalink] [raw]
Subject: Re: [linux-pm] [PATCH] ACPI: replace strlen("string") with sizeof("string") -1

On Tue, 7 Aug 2012, Pavel Vasilyev wrote:

> 06.08.2012 23:59, Alan Stern пишет:
> > On Mon, 6 Aug 2012, Pavel Vasilyev wrote:
> >
> >>>> http://www.gossamer-threads.com/lists/engine?do=post_attachment;postatt_id=41157;list=linux
> >>>
> >>> Interestingly, many (all?) of the changes in that patch are wrong
> >>> because they don't try to match the terminating '\0'. As a result,
> >>> they will match against extensions of the target string as well as the
> >>> target string itself.
> >>>
> >>
> >> strNcmp compare N bytes - http://lxr.linux.no/#linux+v3.5/lib/string.c#L270
> >> memcmp compare N bytes - http://lxr.linux.no/#linux+v3.5/lib/string.c#L651
> >
> > Yes. So if s contains "abcde" then
> >
> > memcmp(s, "abc", 3) and strncmp(s, "abc", 3) will both return 0, and
> > memcmp(s, "abc", 4) and strncmp(s, "abc", 4) will both return 1.
>
> No matter what is contained in *s, "abcde" or "abcxxx",
> are important first N bytes. The second example, you see,
> a little bit stupid, and devoid of logic. :)

Maybe yes, maybe no. It all depends on what you want.

For example, if you're looking for "on" or "off", what should you do
when the user writes "onoff"? You could accept it as meaning the same
as "on", but if you were being careful then you would want to reject it
as a meaningless value.

Alan Stern

2012-08-07 23:24:16

by Pavel Vasilyev

[permalink] [raw]
Subject: Re: [linux-pm] [PATCH] ACPI: replace strlen("string") with sizeof("string") -1

07.08.2012 21:24, Alan Stern пишет:
> On Tue, 7 Aug 2012, Pavel Vasilyev wrote:
>
>> 06.08.2012 23:59, Alan Stern пишет:
>>> On Mon, 6 Aug 2012, Pavel Vasilyev wrote:
>>>
>>>>>> http://www.gossamer-threads.com/lists/engine?do=post_attachment;postatt_id=41157;list=linux
>>>>>
>>>>> Interestingly, many (all?) of the changes in that patch are wrong
>>>>> because they don't try to match the terminating '\0'. As a result,
>>>>> they will match against extensions of the target string as well as the
>>>>> target string itself.
>>>>>
>>>>
>>>> strNcmp compare N bytes - http://lxr.linux.no/#linux+v3.5/lib/string.c#L270
>>>> memcmp compare N bytes - http://lxr.linux.no/#linux+v3.5/lib/string.c#L651
>>>
>>> Yes. So if s contains "abcde" then
>>>
>>> memcmp(s, "abc", 3) and strncmp(s, "abc", 3) will both return 0, and
>>> memcmp(s, "abc", 4) and strncmp(s, "abc", 4) will both return 1.
>>
>> No matter what is contained in *s, "abcde" or "abcxxx",
>> are important first N bytes. The second example, you see,
>> a little bit stupid, and devoid of logic. :)
>
> Maybe yes, maybe no. It all depends on what you want.
>
> For example, if you're looking for "on" or "off", what should you do
> when the user writes "onoff"? You could accept it as meaning the same
> as "on", but if you were being careful then you would want to reject it
> as a meaningless value.


The users should't be allowed to think!
There is "on" - the size of 2 bytes, or "off" - 3 bytes,
other variations - user error.

We do not create a kernel with artificial intelligence? ;)

--
Pavel.

2012-08-08 01:09:01

by Daniel Taylor

[permalink] [raw]
Subject: RE: [linux-pm] [PATCH] ACPI: replace strlen("string") with sizeof("string") -1

Said it was a silly question.

It's funny.

I've been using "0123456789abcdef"[index] for a long time, so I "know"
that "string" is a array of char, but it never occurred to me that
"string" would work in sizeof() the same way as

char string[] = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f', '\0' };

int stringlength = sizeof(string);

Learned something.

Thanks,

Dan


> -----Original Message-----
> From: Bernd Petrovitsch [mailto:[email protected]]
> Sent: Tuesday, August 07, 2012 6:20 AM
> To: Daniel Taylor
> Cc: 'Alan Stern'; Pavel Vasilyev; Pavel Machek; Len Brown;
> [email protected];
> [email protected];
> [email protected]; Len Brown
> Subject: RE: [linux-pm] [PATCH] ACPI: replace
> strlen("string") with sizeof("string") -1
>
> On Mon, 2012-08-06 at 22:57 +0000, Daniel Taylor wrote:
> > Silly question: when did sizeof("string") get changed to
> be anything
> > other than the size of the pointer ("string" is, after all, an array
> > of characters)?
>
> It is since K&R times that way.
> If you do not know the difference between a pointer and an array (and
> these are vastly different), go learn something new about C.
>
> Bernd
> --
> Bernd Petrovitsch Email : [email protected]
> LUGA : http://www.luga.at
>
> -

2012-08-08 01:27:23

by Alan Stern

[permalink] [raw]
Subject: Re: [linux-pm] [PATCH] ACPI: replace strlen("string") with sizeof("string") -1

On Wed, 8 Aug 2012, Pavel Vasilyev wrote:

> >>> Yes. So if s contains "abcde" then
> >>>
> >>> memcmp(s, "abc", 3) and strncmp(s, "abc", 3) will both return 0, and
> >>> memcmp(s, "abc", 4) and strncmp(s, "abc", 4) will both return 1.
> >>
> >> No matter what is contained in *s, "abcde" or "abcxxx",
> >> are important first N bytes. The second example, you see,
> >> a little bit stupid, and devoid of logic. :)
> >
> > Maybe yes, maybe no. It all depends on what you want.
> >
> > For example, if you're looking for "on" or "off", what should you do
> > when the user writes "onoff"? You could accept it as meaning the same
> > as "on", but if you were being careful then you would want to reject it
> > as a meaningless value.
>
>
> The users should't be allowed to think!
> There is "on" - the size of 2 bytes, or "off" - 3 bytes,
> other variations - user error.
>
> We do not create a kernel with artificial intelligence? ;)

Let me rephrase the previous statement, as it appears you did not
understand what I meant.

If the kernel is testing for "on" or "off", what should it do when the
user writes "onoff"? The kernel could accept this as meaning the same
as "on", but if the kernel was being careful then it should reject
"onoff" as a meaningless value. A 2-byte comparison for "on" would
accept "onoff" whereas a 3-byte comparison would not.

Alan Stern