From: Programmingkid <programmingkidx-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
To: Ian Lepore <ian-h+KGxgPPiopAfugRpC6u6w@public.gmane.org>
Cc: David Gibson
<david-xT8FGy+AXnRB3Ne2BGzF6laj5H9X9Tb+@public.gmane.org>,
Richard Henderson
<richard.henderson-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>,
devicetree-compiler-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
"list-l3A5Bk7waGM@public.gmane.org:PowerPC list:PowerPC"
<qemu-ppc-qX2TKyscuCcdnm+yROfE0A@public.gmane.org>,
"qemu-devel-qX2TKyscuCcdnm+yROfE0A@public.gmane.org qemu-devel"
<qemu-devel-qX2TKyscuCcdnm+yROfE0A@public.gmane.org>,
Peter Maydell
<peter.maydell-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
Subject: Re: [Qemu-devel] [libfdt][PATCH v2] implement strnlen for systems that need it
Date: Sun, 22 Oct 2017 15:52:43 -0400 [thread overview]
Message-ID: <5DBD5EDC-4F6C-4A75-ADB0-C1A52532A323@gmail.com> (raw)
In-Reply-To: <1508699172.7314.12.camel-h+KGxgPPiopAfugRpC6u6w@public.gmane.org>
> 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.
next prev parent reply other threads:[~2017-10-22 19:52 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-10-20 17:55 [libfdt][PATCH v2] implement strnlen for systems that need it John Arbuckle
[not found] ` <20171020175548.2566-1-programmingkidx-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2017-10-20 23:44 ` [Qemu-devel] " Richard Henderson
[not found] ` <f9bd98f1-d45c-6d3a-c35c-c67da67c881f-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
2017-10-22 5:33 ` David Gibson
2017-10-22 14:41 ` Programmingkid
[not found] ` <82BA0070-FFBB-4868-AE48-D7A3671621C5-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2017-10-22 19:06 ` Ian Lepore
[not found] ` <1508699172.7314.12.camel-h+KGxgPPiopAfugRpC6u6w@public.gmane.org>
2017-10-22 19:52 ` Programmingkid [this message]
2017-10-24 4:16 ` Programmingkid
[not found] ` <4CE427C5-BD1A-4911-9B10-CC1C0F80FD12-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2017-10-24 16:31 ` David Gibson
2017-10-22 13:37 ` Peter Maydell
[not found] ` <CAFEAcA8WnzZpdTWjJafLDjTCOpT_=azr6T+8UAnMNyDiD2ZxAQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2017-10-22 14:29 ` Programmingkid
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5DBD5EDC-4F6C-4A75-ADB0-C1A52532A323@gmail.com \
--to=programmingkidx-re5jqeeqqe8avxtiumwx3w@public.gmane.org \
--cc=david-xT8FGy+AXnRB3Ne2BGzF6laj5H9X9Tb+@public.gmane.org \
--cc=devicetree-compiler-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=ian-h+KGxgPPiopAfugRpC6u6w@public.gmane.org \
--cc=peter.maydell-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org \
--cc=qemu-devel-qX2TKyscuCcdnm+yROfE0A@public.gmane.org \
--cc=qemu-ppc-qX2TKyscuCcdnm+yROfE0A@public.gmane.org \
--cc=richard.henderson-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).