* Re: [Qemu-devel] [libfdt][PATCH v2] implement strnlen for systems that need it
@ 2017-10-22 5:33 ` David Gibson
0 siblings, 0 replies; 21+ messages in thread
From: David Gibson @ 2017-10-22 5:33 UTC (permalink / raw)
To: Richard Henderson
Cc: John Arbuckle, devicetree-compiler, qemu-ppc, qemu-devel
[-- Attachment #1: Type: text/plain, Size: 778 bytes --]
On Fri, Oct 20, 2017 at 04:44:58PM -0700, Richard Henderson wrote:
> On 10/20/2017 10:55 AM, John Arbuckle wrote:
> > +static inline size_t strnlen(const char *string, size_t max_count)
> > +{
> > + size_t count;
> > + for (count = 0; count < max_count; count++) {
> > + if (string[count] == '\0') {
> > + break;
> > + }
> > + }
> > + return count;
>
> Not to nitpick, but
>
> const char *p = memchr(string, 0, max_count);
> return p ? max_count : p - string;
Richard's right, that's definitely a better implementation.
--
David Gibson | I'll have my music baroque, and my code
david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_
| _way_ _around_!
http://www.ozlabs.org/~dgibson
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 21+ messages in thread* Re: [Qemu-devel] [libfdt][PATCH v2] implement strnlen for systems that need it
2017-10-22 5:33 ` David Gibson
@ 2017-10-22 14:41 ` Programmingkid
-1 siblings, 0 replies; 21+ messages in thread
From: Programmingkid @ 2017-10-22 14:41 UTC (permalink / raw)
To: David Gibson
Cc: Richard Henderson, devicetree-compiler-u79uwXL29TY76Z2rM5mHXA,
list-l3A5Bk7waGM@public.gmane.org:PowerPC list:PowerPC,
qemu-devel-qX2TKyscuCcdnm+yROfE0A@public.gmane.org qemu-devel,
Peter Maydell
> On Oct 22, 2017, at 1:33 AM, David Gibson <david-xT8FGy+AXnRB3Ne2BGzF6laj5H9X9Tb+@public.gmane.org> wrote:
>
> On Fri, Oct 20, 2017 at 04:44:58PM -0700, Richard Henderson wrote:
>> On 10/20/2017 10:55 AM, John Arbuckle wrote:
>>> +static inline size_t strnlen(const char *string, size_t max_count)
>>> +{
>>> + size_t count;
>>> + for (count = 0; count < max_count; count++) {
>>> + if (string[count] == '\0') {
>>> + break;
>>> + }
>>> + }
>>> + return count;
>>
>> Not to nitpick, but
>>
>> const char *p = memchr(string, 0, max_count);
>> return p ? max_count : p - string;
>
> Richard's right, that's definitely a better implementation.
His implementation is smaller, but this one is even smaller. Plus it uses the familiar strlen() function:
size_t strnlen(const char *string, size_t max_count)
{
return strlen(string) < max_count ? strlen(string) : max_count;
}
^ permalink raw reply [flat|nested] 21+ messages in thread* Re: [Qemu-devel] [libfdt][PATCH v2] implement strnlen for systems that need it
@ 2017-10-22 14:41 ` Programmingkid
0 siblings, 0 replies; 21+ messages in thread
From: Programmingkid @ 2017-10-22 14:41 UTC (permalink / raw)
To: David Gibson
Cc: Richard Henderson, devicetree-compiler,
list@suse.de:PowerPC list:PowerPC,
qemu-devel@nongnu.org qemu-devel, Peter Maydell
> On Oct 22, 2017, at 1:33 AM, David Gibson <david@gibson.dropbear.id.au> wrote:
>
> On Fri, Oct 20, 2017 at 04:44:58PM -0700, Richard Henderson wrote:
>> On 10/20/2017 10:55 AM, John Arbuckle wrote:
>>> +static inline size_t strnlen(const char *string, size_t max_count)
>>> +{
>>> + size_t count;
>>> + for (count = 0; count < max_count; count++) {
>>> + if (string[count] == '\0') {
>>> + break;
>>> + }
>>> + }
>>> + return count;
>>
>> Not to nitpick, but
>>
>> const char *p = memchr(string, 0, max_count);
>> return p ? max_count : p - string;
>
> Richard's right, that's definitely a better implementation.
His implementation is smaller, but this one is even smaller. Plus it uses the familiar strlen() function:
size_t strnlen(const char *string, size_t max_count)
{
return strlen(string) < max_count ? strlen(string) : max_count;
}
^ permalink raw reply [flat|nested] 21+ messages in thread* Re: [Qemu-devel] [libfdt][PATCH v2] implement strnlen for systems that need it
2017-10-22 14:41 ` Programmingkid
(?)
@ 2017-10-22 15:05 ` John Reiser
-1 siblings, 0 replies; 21+ messages in thread
From: John Reiser @ 2017-10-22 15:05 UTC (permalink / raw)
To: qemu-devel
> ... this one is even smaller. Plus it uses the familiar strlen() function:
>
> size_t strnlen(const char *string, size_t max_count)
> {
> return strlen(string) < max_count ? strlen(string) : max_count;
> }
>
Please do not use that implementation.
The major goal of strnlen is to avoid looking beyond &string[max_count].
strlen(string) looks all the way to the end, which may be very much longer than max_count;
and which may cause SIGSEGV by running into a memory page that does not exist
before the terminating '\0' is found.
[Besides, some compilers do not recognize that "strlen(string)"
need not be evaluated twice.]
--
^ permalink raw reply [flat|nested] 21+ messages in thread[parent not found: <82BA0070-FFBB-4868-AE48-D7A3671621C5-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>]
* Re: [Qemu-devel] [libfdt][PATCH v2] implement strnlen for systems that need it
2017-10-22 14:41 ` Programmingkid
@ 2017-10-22 19:06 ` Ian Lepore
-1 siblings, 0 replies; 21+ messages in thread
From: Ian Lepore @ 2017-10-22 19:06 UTC (permalink / raw)
To: Programmingkid, David Gibson
Cc: Richard Henderson, devicetree-compiler-u79uwXL29TY76Z2rM5mHXA,
list-l3A5Bk7waGM@public.gmane.org:PowerPC list:PowerPC,
qemu-devel-qX2TKyscuCcdnm+yROfE0A@public.gmane.org qemu-devel,
Peter Maydell
On Sun, 2017-10-22 at 10:41 -0400, Programmingkid wrote:
> >
> > On Oct 22, 2017, at 1:33 AM, David Gibson wrote:
> >
> > On Fri, Oct 20, 2017 at 04:44:58PM -0700, Richard Henderson wrote:
> > >
> > > On 10/20/2017 10:55 AM, John Arbuckle wrote:
> > > >
> > > > +static inline size_t strnlen(const char *string, size_t max_count)
> > > > +{
> > > > + size_t count;
> > > > + for (count = 0; count < max_count; count++) {
> > > > + if (string[count] == '\0') {
> > > > + break;
> > > > + }
> > > > + }
> > > > + return count;
> > > Not to nitpick, but
> > >
> > > const char *p = memchr(string, 0, max_count);
> > > return p ? max_count : p - string;
> > Richard's right, that's definitely a better implementation.
> His implementation is smaller, but this one is even smaller. Plus it uses the familiar strlen() function:
>
> size_t strnlen(const char *string, size_t max_count)
> {
> return strlen(string) < max_count ? strlen(string) : max_count;
> }
That is not a proper implementation of strnlen(), which is not supposed
to access any source-string bytes beyond max_count.
-- Ian
^ permalink raw reply [flat|nested] 21+ messages in thread* Re: [Qemu-devel] [libfdt][PATCH v2] implement strnlen for systems that need it
@ 2017-10-22 19:06 ` Ian Lepore
0 siblings, 0 replies; 21+ messages in thread
From: Ian Lepore @ 2017-10-22 19:06 UTC (permalink / raw)
To: Programmingkid, David Gibson
Cc: Richard Henderson, devicetree-compiler,
list@suse.de:PowerPC list:PowerPC,
qemu-devel@nongnu.org qemu-devel, Peter Maydell
On Sun, 2017-10-22 at 10:41 -0400, Programmingkid wrote:
> >
> > On Oct 22, 2017, at 1:33 AM, David Gibson wrote:
> >
> > On Fri, Oct 20, 2017 at 04:44:58PM -0700, Richard Henderson wrote:
> > >
> > > On 10/20/2017 10:55 AM, John Arbuckle wrote:
> > > >
> > > > +static inline size_t strnlen(const char *string, size_t max_count)
> > > > +{
> > > > + size_t count;
> > > > + for (count = 0; count < max_count; count++) {
> > > > + if (string[count] == '\0') {
> > > > + break;
> > > > + }
> > > > + }
> > > > + return count;
> > > Not to nitpick, but
> > >
> > > const char *p = memchr(string, 0, max_count);
> > > return p ? max_count : p - string;
> > Richard's right, that's definitely a better implementation.
> His implementation is smaller, but this one is even smaller. Plus it uses the familiar strlen() function:
>
> size_t strnlen(const char *string, size_t max_count)
> {
> return strlen(string) < max_count ? strlen(string) : max_count;
> }
That is not a proper implementation of strnlen(), which is not supposed
to access any source-string bytes beyond max_count.
-- Ian
^ permalink raw reply [flat|nested] 21+ messages in thread[parent not found: <1508699172.7314.12.camel-h+KGxgPPiopAfugRpC6u6w@public.gmane.org>]
* Re: [Qemu-devel] [libfdt][PATCH v2] implement strnlen for systems that need it
2017-10-22 19:06 ` Ian Lepore
@ 2017-10-22 19:52 ` Programmingkid
-1 siblings, 0 replies; 21+ messages in thread
From: Programmingkid @ 2017-10-22 19:52 UTC (permalink / raw)
To: Ian Lepore
Cc: David Gibson, Richard Henderson,
devicetree-compiler-u79uwXL29TY76Z2rM5mHXA,
list-l3A5Bk7waGM@public.gmane.org:PowerPC list:PowerPC,
qemu-devel-qX2TKyscuCcdnm+yROfE0A@public.gmane.org qemu-devel,
Peter Maydell
> On Oct 22, 2017, at 3:06 PM, Ian Lepore <ian-h+KGxgPPiopAfugRpC6u6w@public.gmane.org> wrote:
>
> On Sun, 2017-10-22 at 10:41 -0400, Programmingkid wrote:
>>>
>>> On Oct 22, 2017, at 1:33 AM, David Gibson wrote:
>>>
>>> On Fri, Oct 20, 2017 at 04:44:58PM -0700, Richard Henderson wrote:
>>>>
>>>> On 10/20/2017 10:55 AM, John Arbuckle wrote:
>>>>>
>>>>> +static inline size_t strnlen(const char *string, size_t max_count)
>>>>> +{
>>>>> + size_t count;
>>>>> + for (count = 0; count < max_count; count++) {
>>>>> + if (string[count] == '\0') {
>>>>> + break;
>>>>> + }
>>>>> + }
>>>>> + return count;
>>>> Not to nitpick, but
>>>>
>>>> const char *p = memchr(string, 0, max_count);
>>>> return p ? max_count : p - string;
>>> Richard's right, that's definitely a better implementation.
>> His implementation is smaller, but this one is even smaller. Plus it uses the familiar strlen() function:
>>
>> size_t strnlen(const char *string, size_t max_count)
>> {
>> return strlen(string) < max_count ? strlen(string) : max_count;
>> }
>
> That is not a proper implementation of strnlen(), which is not supposed
> to access any source-string bytes beyond max_count.
>
> -- Ian
http://pubs.opengroup.org/onlinepubs/9699919799/functions/strlen.html
This specification document should help anyone who wants more info.
The first implementation using the loop would never access anything beyond max_count. My second implementation does go beyond max_count. The implementation using memchr() will probably live up the requirement so I guess it wins.
Thank you Ian for this information.
^ permalink raw reply [flat|nested] 21+ messages in thread* Re: [Qemu-devel] [libfdt][PATCH v2] implement strnlen for systems that need it
@ 2017-10-22 19:52 ` Programmingkid
0 siblings, 0 replies; 21+ messages in thread
From: Programmingkid @ 2017-10-22 19:52 UTC (permalink / raw)
To: Ian Lepore
Cc: David Gibson, Richard Henderson, devicetree-compiler,
list@suse.de:PowerPC list:PowerPC,
qemu-devel@nongnu.org qemu-devel, Peter Maydell
> On Oct 22, 2017, at 3:06 PM, Ian Lepore <ian@freebsd.org> wrote:
>
> On Sun, 2017-10-22 at 10:41 -0400, Programmingkid wrote:
>>>
>>> On Oct 22, 2017, at 1:33 AM, David Gibson wrote:
>>>
>>> On Fri, Oct 20, 2017 at 04:44:58PM -0700, Richard Henderson wrote:
>>>>
>>>> On 10/20/2017 10:55 AM, John Arbuckle wrote:
>>>>>
>>>>> +static inline size_t strnlen(const char *string, size_t max_count)
>>>>> +{
>>>>> + size_t count;
>>>>> + for (count = 0; count < max_count; count++) {
>>>>> + if (string[count] == '\0') {
>>>>> + break;
>>>>> + }
>>>>> + }
>>>>> + return count;
>>>> Not to nitpick, but
>>>>
>>>> const char *p = memchr(string, 0, max_count);
>>>> return p ? max_count : p - string;
>>> Richard's right, that's definitely a better implementation.
>> His implementation is smaller, but this one is even smaller. Plus it uses the familiar strlen() function:
>>
>> size_t strnlen(const char *string, size_t max_count)
>> {
>> return strlen(string) < max_count ? strlen(string) : max_count;
>> }
>
> That is not a proper implementation of strnlen(), which is not supposed
> to access any source-string bytes beyond max_count.
>
> -- Ian
http://pubs.opengroup.org/onlinepubs/9699919799/functions/strlen.html
This specification document should help anyone who wants more info.
The first implementation using the loop would never access anything beyond max_count. My second implementation does go beyond max_count. The implementation using memchr() will probably live up the requirement so I guess it wins.
Thank you Ian for this information.
^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [Qemu-devel] [libfdt][PATCH v2] implement strnlen for systems that need it
2017-10-22 5:33 ` David Gibson
@ 2017-10-24 4:16 ` Programmingkid
-1 siblings, 0 replies; 21+ messages in thread
From: Programmingkid @ 2017-10-24 4:16 UTC (permalink / raw)
To: David Gibson
Cc: Richard Henderson, devicetree-compiler-u79uwXL29TY76Z2rM5mHXA,
qemu-ppc-qX2TKyscuCcdnm+yROfE0A,
qemu-devel-qX2TKyscuCcdnm+yROfE0A
> On Oct 22, 2017, at 1:33 AM, David Gibson <david-xT8FGy+AXnRB3Ne2BGzF6laj5H9X9Tb+@public.gmane.org> wrote:
>
> On Fri, Oct 20, 2017 at 04:44:58PM -0700, Richard Henderson wrote:
>> On 10/20/2017 10:55 AM, John Arbuckle wrote:
>>> +static inline size_t strnlen(const char *string, size_t max_count)
>>> +{
>>> + size_t count;
>>> + for (count = 0; count < max_count; count++) {
>>> + if (string[count] == '\0') {
>>> + break;
>>> + }
>>> + }
>>> + return count;
>>
>> Not to nitpick, but
>>
>> const char *p = memchr(string, 0, max_count);
>> return p ? max_count : p - string;
>
> Richard's right, that's definitely a better implementation.
I was just wondering, what if we rewrote the code to use strlen() instead of strnlen(). Would that be an acceptable solution?
^ permalink raw reply [flat|nested] 21+ messages in thread* Re: [Qemu-devel] [libfdt][PATCH v2] implement strnlen for systems that need it
@ 2017-10-24 4:16 ` Programmingkid
0 siblings, 0 replies; 21+ messages in thread
From: Programmingkid @ 2017-10-24 4:16 UTC (permalink / raw)
To: David Gibson; +Cc: Richard Henderson, devicetree-compiler, qemu-ppc, qemu-devel
> On Oct 22, 2017, at 1:33 AM, David Gibson <david@gibson.dropbear.id.au> wrote:
>
> On Fri, Oct 20, 2017 at 04:44:58PM -0700, Richard Henderson wrote:
>> On 10/20/2017 10:55 AM, John Arbuckle wrote:
>>> +static inline size_t strnlen(const char *string, size_t max_count)
>>> +{
>>> + size_t count;
>>> + for (count = 0; count < max_count; count++) {
>>> + if (string[count] == '\0') {
>>> + break;
>>> + }
>>> + }
>>> + return count;
>>
>> Not to nitpick, but
>>
>> const char *p = memchr(string, 0, max_count);
>> return p ? max_count : p - string;
>
> Richard's right, that's definitely a better implementation.
I was just wondering, what if we rewrote the code to use strlen() instead of strnlen(). Would that be an acceptable solution?
^ permalink raw reply [flat|nested] 21+ messages in thread[parent not found: <4CE427C5-BD1A-4911-9B10-CC1C0F80FD12-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>]
* Re: [Qemu-devel] [libfdt][PATCH v2] implement strnlen for systems that need it
2017-10-24 4:16 ` Programmingkid
@ 2017-10-24 16:31 ` David Gibson
-1 siblings, 0 replies; 21+ messages in thread
From: David Gibson @ 2017-10-24 16:31 UTC (permalink / raw)
To: Programmingkid
Cc: Richard Henderson, devicetree-compiler-u79uwXL29TY76Z2rM5mHXA,
qemu-ppc-qX2TKyscuCcdnm+yROfE0A,
qemu-devel-qX2TKyscuCcdnm+yROfE0A
[-- Attachment #1: Type: text/plain, Size: 1391 bytes --]
On Tue, Oct 24, 2017 at 12:16:47AM -0400, Programmingkid wrote:
>
> > On Oct 22, 2017, at 1:33 AM, David Gibson <david-xT8FGy+AXnRB3Ne2BGzF6laj5H9X9Tb+@public.gmane.org> wrote:
> >
> > On Fri, Oct 20, 2017 at 04:44:58PM -0700, Richard Henderson wrote:
> >> On 10/20/2017 10:55 AM, John Arbuckle wrote:
> >>> +static inline size_t strnlen(const char *string, size_t max_count)
> >>> +{
> >>> + size_t count;
> >>> + for (count = 0; count < max_count; count++) {
> >>> + if (string[count] == '\0') {
> >>> + break;
> >>> + }
> >>> + }
> >>> + return count;
> >>
> >> Not to nitpick, but
> >>
> >> const char *p = memchr(string, 0, max_count);
> >> return p ? max_count : p - string;
> >
> > Richard's right, that's definitely a better implementation.
>
> I was just wondering, what if we rewrote the code to use strlen()
> instead of strnlen(). Would that be an acceptable solution?
Only if you can do so safely - i.e. without accessing memory beyond
what we're supposed to. I don't think you'll be able to do that
without effectively re-implementing strnlen(), there's a reason I used
it in the first place, after all.
--
David Gibson | I'll have my music baroque, and my code
david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_
| _way_ _around_!
http://www.ozlabs.org/~dgibson
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 21+ messages in thread* Re: [Qemu-devel] [libfdt][PATCH v2] implement strnlen for systems that need it
@ 2017-10-24 16:31 ` David Gibson
0 siblings, 0 replies; 21+ messages in thread
From: David Gibson @ 2017-10-24 16:31 UTC (permalink / raw)
To: Programmingkid
Cc: Richard Henderson, devicetree-compiler, qemu-ppc, qemu-devel
[-- Attachment #1: Type: text/plain, Size: 1363 bytes --]
On Tue, Oct 24, 2017 at 12:16:47AM -0400, Programmingkid wrote:
>
> > On Oct 22, 2017, at 1:33 AM, David Gibson <david@gibson.dropbear.id.au> wrote:
> >
> > On Fri, Oct 20, 2017 at 04:44:58PM -0700, Richard Henderson wrote:
> >> On 10/20/2017 10:55 AM, John Arbuckle wrote:
> >>> +static inline size_t strnlen(const char *string, size_t max_count)
> >>> +{
> >>> + size_t count;
> >>> + for (count = 0; count < max_count; count++) {
> >>> + if (string[count] == '\0') {
> >>> + break;
> >>> + }
> >>> + }
> >>> + return count;
> >>
> >> Not to nitpick, but
> >>
> >> const char *p = memchr(string, 0, max_count);
> >> return p ? max_count : p - string;
> >
> > Richard's right, that's definitely a better implementation.
>
> I was just wondering, what if we rewrote the code to use strlen()
> instead of strnlen(). Would that be an acceptable solution?
Only if you can do so safely - i.e. without accessing memory beyond
what we're supposed to. I don't think you'll be able to do that
without effectively re-implementing strnlen(), there's a reason I used
it in the first place, after all.
--
David Gibson | I'll have my music baroque, and my code
david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_
| _way_ _around_!
http://www.ozlabs.org/~dgibson
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 21+ messages in thread