* [Qemu-devel] [libfdt][PATCH v2] implement strnlen for systems that need it
@ 2017-10-20 17:55 ` John Arbuckle
0 siblings, 0 replies; 21+ messages in thread
From: John Arbuckle @ 2017-10-20 17:55 UTC (permalink / raw)
To: david, devicetree-compiler, qemu-ppc, qemu-devel; +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@gmail.com>
---
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] 21+ messages in thread
* [libfdt][PATCH v2] implement strnlen for systems that need it
@ 2017-10-20 17:55 ` John Arbuckle
0 siblings, 0 replies; 21+ 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] 21+ messages in thread
* Re: [Qemu-devel] [libfdt][PATCH v2] implement strnlen for systems that need it
@ 2017-10-20 23:44 ` Richard Henderson
0 siblings, 0 replies; 21+ messages in thread
From: Richard Henderson @ 2017-10-20 23:44 UTC (permalink / raw)
To: John Arbuckle, david, devicetree-compiler, qemu-ppc, qemu-devel
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] 21+ messages in thread
* Re: [Qemu-devel] [libfdt][PATCH v2] implement strnlen for systems that need it
@ 2017-10-20 23:44 ` Richard Henderson
0 siblings, 0 replies; 21+ 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] 21+ messages in thread
* 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
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-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] 21+ messages in thread
* Re: [Qemu-devel] [libfdt][PATCH v2] implement strnlen for systems that need it
@ 2017-10-22 13:37 ` Peter Maydell
0 siblings, 0 replies; 21+ messages in thread
From: Peter Maydell @ 2017-10-22 13:37 UTC (permalink / raw)
To: Richard Henderson
Cc: John Arbuckle, David Gibson, devicetree-compiler, qemu-ppc,
QEMU Developers
On 21 October 2017 at 00:44, Richard Henderson
<richard.henderson@linaro.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] 21+ messages in thread
* Re: [Qemu-devel] [libfdt][PATCH v2] implement strnlen for systems that need it
@ 2017-10-22 13:37 ` Peter Maydell
0 siblings, 0 replies; 21+ 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, 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] 21+ messages in thread
* Re: [Qemu-devel] [libfdt][PATCH v2] implement strnlen for systems that need it
@ 2017-10-22 14:29 ` Programmingkid
0 siblings, 0 replies; 21+ messages in thread
From: Programmingkid @ 2017-10-22 14:29 UTC (permalink / raw)
To: Peter Maydell
Cc: Richard Henderson, David Gibson, devicetree-compiler, qemu-ppc,
QEMU Developers
> On Oct 22, 2017, at 9:37 AM, Peter Maydell <peter.maydell@linaro.org> wrote:
>
> On 21 October 2017 at 00:44, Richard Henderson
> <richard.henderson@linaro.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] 21+ messages in thread
* Re: [Qemu-devel] [libfdt][PATCH v2] implement strnlen for systems that need it
@ 2017-10-22 14:29 ` Programmingkid
0 siblings, 0 replies; 21+ 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, 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] 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,
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
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-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
(?)
@ 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
* 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
* 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-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: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 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-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 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, 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
* 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-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 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
* 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-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
end of thread, other threads:[~2017-10-24 16:44 UTC | newest]
Thread overview: 21+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-10-20 17:55 [Qemu-devel] [libfdt][PATCH v2] implement strnlen for systems that need it John Arbuckle
2017-10-20 17:55 ` John Arbuckle
2017-10-20 23:44 ` [Qemu-devel] " Richard Henderson
2017-10-20 23:44 ` Richard Henderson
2017-10-22 5:33 ` David Gibson
2017-10-22 5:33 ` David Gibson
2017-10-22 14:41 ` Programmingkid
2017-10-22 14:41 ` Programmingkid
2017-10-22 15:05 ` John Reiser
2017-10-22 19:06 ` Ian Lepore
2017-10-22 19:06 ` Ian Lepore
2017-10-22 19:52 ` Programmingkid
2017-10-22 19:52 ` Programmingkid
2017-10-24 4:16 ` Programmingkid
2017-10-24 4:16 ` Programmingkid
2017-10-24 16:31 ` David Gibson
2017-10-24 16:31 ` David Gibson
2017-10-22 13:37 ` Peter Maydell
2017-10-22 13:37 ` Peter Maydell
2017-10-22 14:29 ` Programmingkid
2017-10-22 14:29 ` Programmingkid
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.