* [libfdt][PATCH v2] implement strnlen for systems that need it @ 2017-10-20 17:55 John Arbuckle [not found] ` <20171020175548.2566-1-programmingkidx-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 0 siblings, 1 reply; 10+ messages in thread From: John Arbuckle @ 2017-10-20 17:55 UTC (permalink / raw) To: david-xT8FGy+AXnRB3Ne2BGzF6laj5H9X9Tb+, devicetree-compiler-u79uwXL29TY76Z2rM5mHXA, qemu-ppc-qX2TKyscuCcdnm+yROfE0A, qemu-devel-qX2TKyscuCcdnm+yROfE0A Cc: John Arbuckle Prior the Mac OS 10.7, the function strnlen() was not available. This patch implements strnlen() on Mac OS X versions that are below 10.7. Signed-off-by: John Arbuckle <programmingkidx-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> --- v2 changes: - Simplified the code to make it static inline'ed - Changed the type of count to size_t libfdt/libfdt_env.h | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/libfdt/libfdt_env.h b/libfdt/libfdt_env.h index 952056c..2569339 100644 --- a/libfdt/libfdt_env.h +++ b/libfdt/libfdt_env.h @@ -109,4 +109,33 @@ static inline fdt64_t cpu_to_fdt64(uint64_t x) #undef CPU_TO_FDT16 #undef EXTRACT_BYTE +#ifdef __APPLE__ +#include <AvailabilityMacros.h> + +#define MAC_OS_X_VERSION_10_7 1070 + +/* strnlen() is not available on Mac OS < 10.7 */ +# if (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7) + +/* + * strnlen: returns the length of a string or max_count - which ever is smallest + * Input 1 string: the string whose size is to be determined + * Input 2 max_count: the maximum value returned by this function + * Output: length of the string or max_count (the smallest of the two) + */ +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; +} + +#endif /* (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7) */ + +#endif /* __APPLE__ */ + #endif /* _LIBFDT_ENV_H */ -- 2.13.5 (Apple Git-94) ^ permalink raw reply related [flat|nested] 10+ messages in thread
[parent not found: <20171020175548.2566-1-programmingkidx-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>]
* Re: [Qemu-devel] [libfdt][PATCH v2] implement strnlen for systems that need it [not found] ` <20171020175548.2566-1-programmingkidx-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> @ 2017-10-20 23:44 ` Richard Henderson [not found] ` <f9bd98f1-d45c-6d3a-c35c-c67da67c881f-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org> 0 siblings, 1 reply; 10+ messages in thread From: Richard Henderson @ 2017-10-20 23:44 UTC (permalink / raw) To: John Arbuckle, david-xT8FGy+AXnRB3Ne2BGzF6laj5H9X9Tb+, devicetree-compiler-u79uwXL29TY76Z2rM5mHXA, qemu-ppc-qX2TKyscuCcdnm+yROfE0A, qemu-devel-qX2TKyscuCcdnm+yROfE0A 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; r~ ^ permalink raw reply [flat|nested] 10+ messages in thread
[parent not found: <f9bd98f1-d45c-6d3a-c35c-c67da67c881f-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>]
* Re: [Qemu-devel] [libfdt][PATCH v2] implement strnlen for systems that need it [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 2017-10-24 4:16 ` Programmingkid 2017-10-22 13:37 ` Peter Maydell 1 sibling, 2 replies; 10+ messages in thread From: David Gibson @ 2017-10-22 5:33 UTC (permalink / raw) To: Richard Henderson Cc: John Arbuckle, devicetree-compiler-u79uwXL29TY76Z2rM5mHXA, qemu-ppc-qX2TKyscuCcdnm+yROfE0A, qemu-devel-qX2TKyscuCcdnm+yROfE0A [-- 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] 10+ 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 [not found] ` <82BA0070-FFBB-4868-AE48-D7A3671621C5-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 2017-10-24 4:16 ` Programmingkid 1 sibling, 1 reply; 10+ 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] 10+ 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 [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> 0 siblings, 1 reply; 10+ 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] 10+ 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 [not found] ` <1508699172.7314.12.camel-h+KGxgPPiopAfugRpC6u6w@public.gmane.org> @ 2017-10-22 19:52 ` Programmingkid 0 siblings, 0 replies; 10+ 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] 10+ 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 @ 2017-10-24 4:16 ` Programmingkid [not found] ` <4CE427C5-BD1A-4911-9B10-CC1C0F80FD12-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 1 sibling, 1 reply; 10+ 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] 10+ 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 [not found] ` <4CE427C5-BD1A-4911-9B10-CC1C0F80FD12-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> @ 2017-10-24 16:31 ` David Gibson 0 siblings, 0 replies; 10+ 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] 10+ messages in thread
* Re: [Qemu-devel] [libfdt][PATCH v2] implement strnlen for systems that need it [not found] ` <f9bd98f1-d45c-6d3a-c35c-c67da67c881f-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org> 2017-10-22 5:33 ` David Gibson @ 2017-10-22 13:37 ` Peter Maydell [not found] ` <CAFEAcA8WnzZpdTWjJafLDjTCOpT_=azr6T+8UAnMNyDiD2ZxAQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org> 1 sibling, 1 reply; 10+ messages in thread From: Peter Maydell @ 2017-10-22 13:37 UTC (permalink / raw) To: Richard Henderson Cc: John Arbuckle, David Gibson, devicetree-compiler-u79uwXL29TY76Z2rM5mHXA, qemu-ppc-qX2TKyscuCcdnm+yROfE0A@public.gmane.org, QEMU Developers On 21 October 2017 at 00:44, Richard Henderson <richard.henderson-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org> 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; Am I misreading that, or do you have the ?: arms the wrong way around there? thanks -- PMM ^ permalink raw reply [flat|nested] 10+ messages in thread
[parent not found: <CAFEAcA8WnzZpdTWjJafLDjTCOpT_=azr6T+8UAnMNyDiD2ZxAQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>]
* Re: [Qemu-devel] [libfdt][PATCH v2] implement strnlen for systems that need it [not found] ` <CAFEAcA8WnzZpdTWjJafLDjTCOpT_=azr6T+8UAnMNyDiD2ZxAQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org> @ 2017-10-22 14:29 ` Programmingkid 0 siblings, 0 replies; 10+ messages in thread From: Programmingkid @ 2017-10-22 14:29 UTC (permalink / raw) To: Peter Maydell Cc: Richard Henderson, David Gibson, devicetree-compiler-u79uwXL29TY76Z2rM5mHXA, qemu-ppc-qX2TKyscuCcdnm+yROfE0A@public.gmane.org, QEMU Developers > On Oct 22, 2017, at 9:37 AM, Peter Maydell <peter.maydell-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org> wrote: > > On 21 October 2017 at 00:44, Richard Henderson > <richard.henderson-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org> 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; > > Am I misreading that, or do you have the ?: arms the wrong way > around there? > > thanks > -- PMM Yes. It should read: return p ? p - string : max_count; ^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2017-10-24 16:31 UTC | newest] Thread overview: 10+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 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 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
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).