Skip to contentSkip to conversation

[5532] Replace uses of strndup (a GNU extension) with Qemu pstrdup

Qemu-devel Blue Swirl 3 participants 14 messages Oct 25, 2008 – Nov 1, 2008

lore
Share & tools
lore

Blue Swirl

author
#0 lore raw

Revision: 5532

          http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=5532
Author:   blueswir1
Date:     2008-10-25 11:23:27 +0000 (Sat, 25 Oct 2008)

Log Message:
-----------
Replace uses of strndup (a GNU extension) with Qemu pstrdup

Modified Paths:
--------------
trunk/cutils.c
trunk/hw/bt-hci.c
trunk/qemu-common.h

Modified: trunk/cutils.c
===================================================================

cutils.c +12 -0
@@ -50,6 +50,18 @@
     return buf;
 }
 
+/* strdup with a limit */
+char *pstrdup(const char *str, size_t buf_size)
+{
+    size_t len;
+    char *buf;
+
+    len = MIN(buf_size, strlen(str));
+    buf = qemu_malloc(len);
+    pstrcpy(buf, len, str);
+    return buf;
+}
+
 int strstart(const char *str, const char *val, const char **ptr)
 {
     const char *p, *q;

Modified: trunk/hw/bt-hci.c
===================================================================

hw/bt-hci.c +1 -1
@@ -1814,7 +1814,7 @@
 
         if (hci->device.lmp_name)
             free((void *) hci->device.lmp_name);
-        hci->device.lmp_name = strndup(PARAM(change_local_name, name),
+        hci->device.lmp_name = pstrdup(PARAM(change_local_name, name),
                         sizeof(PARAM(change_local_name, name)));
         bt_hci_event_complete_status(hci, HCI_SUCCESS);
         break;

Modified: trunk/qemu-common.h
===================================================================

qemu-common.h +1 -0
@@ -82,6 +82,7 @@
 /* cutils.c */
 void pstrcpy(char *buf, int buf_size, const char *str);
 char *pstrcat(char *buf, int buf_size, const char *s);
+char *pstrdup(const char *str, size_t buf_size);
 int strstart(const char *str, const char *val, const char **ptr);
 int stristart(const char *str, const char *val, const char **ptr);
 time_t mktimegm(struct tm *tm);
#1 lore raw

2008/10/25 Blue Swirl <[email protected]>:

Quoted 24 lines

Revision: 5532

         http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=5532
Author:   blueswir1
Date:     2008-10-25 11:23:27 +0000 (Sat, 25 Oct 2008)

Log Message:
-----------
Replace uses of strndup (a GNU extension) with Qemu pstrdup

Modified Paths:
--------------
trunk/cutils.c
trunk/hw/bt-hci.c
trunk/qemu-common.h

Modified: trunk/cutils.c
===================================================================

cutils.c 2008-10-25 11:23:27 UTC (rev 5532) renamed from cutils.c 2008-10-25 11:21:28 UTC (rev 5531) +1 -0
@@ -50,6 +50,18 @@
    return buf;
 }
 
+/* strdup with a limit */

cutils.c 2008-10-25 11:23:27 UTC (rev 5532) line 63

Quoted 10 lines
cutils.c 2008-10-25 11:23:27 UTC (rev 5532) renamed from cutils.c 2008-10-25 11:21:28 UTC (rev 5531) +10 -0
+char *pstrdup(const char *str, size_t buf_size)
+{
+    size_t len;
+    char *buf;
+
+    len = MIN(buf_size, strlen(str));
+    buf = qemu_malloc(len);
+    pstrcpy(buf, len, str);
+    return buf;
+}

I think here also pstrcpy will only copy up to buf_size - 1 characters while strndup would copy buf_size chars.

Cheers

author
#2 lore raw
Quoted 25 lines · andrzej zaborowski

2008/10/25 Blue Swirl <[email protected]>:

Quoted 23 lines

Revision: 5532

         http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=5532
Author:   blueswir1
Date:     2008-10-25 11:23:27 +0000 (Sat, 25 Oct 2008)

Log Message:
-----------
Replace uses of strndup (a GNU extension) with Qemu pstrdup

Modified Paths:
--------------
trunk/cutils.c
trunk/hw/bt-hci.c
trunk/qemu-common.h

Modified: trunk/cutils.c
===================================================================

cutils.c 2008-10-25 11:23:27 UTC (rev 5532) renamed from cutils.c 2008-10-25 11:21:28 UTC (rev 5531) +0 -0
@@ -50,6 +50,18 @@
    return buf;
 }
 
Quoted 15 lines

cutils.c 2008-10-25 11:23:27 UTC (rev 5532) line 63

Quoted 11 lines
cutils.c 2008-10-25 11:23:27 UTC (rev 5532) renamed from cutils.c 2008-10-25 11:21:28 UTC (rev 5531) +11 -0
+/* strdup with a limit */
+char *pstrdup(const char *str, size_t buf_size)
+{
+    size_t len;
+    char *buf;
+
+    len = MIN(buf_size, strlen(str));
+    buf = qemu_malloc(len);
+    pstrcpy(buf, len, str);
+    return buf;
+}

I think here also pstrcpy will only copy up to buf_size - 1 characters
while strndup would copy buf_size chars.

That is actually safer if we always want the strings to be NUL terminated.

But the allocation length is wrong, it should be MIN(buf_size, strlen(str) + 1).

#3 lore raw

2008/10/25 Blue Swirl <[email protected]>:

Quoted 39 lines · andrzej zaborowski
Quoted 38 lines

2008/10/25 Blue Swirl <[email protected]>:

Quoted 34 lines

Revision: 5532

         http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=5532
Author:   blueswir1
Date:     2008-10-25 11:23:27 +0000 (Sat, 25 Oct 2008)

Log Message:
-----------
Replace uses of strndup (a GNU extension) with Qemu pstrdup

Modified Paths:
--------------
trunk/cutils.c
trunk/hw/bt-hci.c
trunk/qemu-common.h

Modified: trunk/cutils.c
===================================================================

cutils.c 2008-10-25 11:23:27 UTC (rev 5532) renamed from cutils.c 2008-10-25 11:21:28 UTC (rev 5531) +11 -0
@@ -50,6 +50,18 @@
    return buf;
 }
 
+/* strdup with a limit */
+char *pstrdup(const char *str, size_t buf_size)
+{
+    size_t len;
+    char *buf;
+
+    len = MIN(buf_size, strlen(str));
+    buf = qemu_malloc(len);
+    pstrcpy(buf, len, str);
+    return buf;
+}
Quoted 4 lines
Quoted 2 lines

I think here also pstrcpy will only copy up to buf_size - 1 characters
while strndup would copy buf_size chars.

That is actually safer if we always want the strings to be NUL terminated.

strndup also always NUL terminates the string so it's just as safe, the length is just different.

Quoted 2 lines

But the allocation length is wrong, it should be MIN(buf_size, strlen(str) + 1).

By my reading of the manual, it should rather be MIN(buf_size, strlen(str)) + 1.

Now that I think of it, note that this changed malloc to qemu_malloc so you need to change the respective free()s to qemu_free()s. Cheers

author
#4 lore raw
Quoted 51 lines · andrzej zaborowski

2008/10/25 Blue Swirl <[email protected]>:

Quoted 39 lines · andrzej zaborowski
Quoted 38 lines

2008/10/25 Blue Swirl <[email protected]>:

Quoted 34 lines

Revision: 5532

         http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=5532
Author:   blueswir1
Date:     2008-10-25 11:23:27 +0000 (Sat, 25 Oct 2008)

Log Message:
-----------
Replace uses of strndup (a GNU extension) with Qemu pstrdup

Modified Paths:
--------------
trunk/cutils.c
trunk/hw/bt-hci.c
trunk/qemu-common.h

Modified: trunk/cutils.c
===================================================================

cutils.c 2008-10-25 11:23:27 UTC (rev 5532) renamed from cutils.c 2008-10-25 11:21:28 UTC (rev 5531) +11 -0
@@ -50,6 +50,18 @@
    return buf;
 }
 
+/* strdup with a limit */
+char *pstrdup(const char *str, size_t buf_size)
+{
+    size_t len;
+    char *buf;
+
+    len = MIN(buf_size, strlen(str));
+    buf = qemu_malloc(len);
+    pstrcpy(buf, len, str);
+    return buf;
+}
Quoted 4 lines
Quoted 2 lines

I think here also pstrcpy will only copy up to buf_size - 1 characters
while strndup would copy buf_size chars.

That is actually safer if we always want the strings to be NUL terminated.

strndup also always NUL terminates the string so it's just as safe,
the length is just different.

Quoted 1 line
Quoted 4 lines
Quoted 1 line

But the allocation length is wrong, it should be MIN(buf_size, strlen(str) + 1).

By my reading of the manual, it should rather be MIN(buf_size, strlen(str)) + 1.

But then the length could be incorrect: buf_size + 1.

Quoted 2 lines

Now that I think of it, note that this changed malloc to qemu_malloc so you need to change the respective free()s to qemu_free()s.

True.

#5 lore raw

2008/10/25 Blue Swirl <[email protected]>:

Quoted 52 lines · andrzej zaborowski
Quoted 51 lines

2008/10/25 Blue Swirl <[email protected]>:

Quoted 39 lines · andrzej zaborowski
Quoted 38 lines

2008/10/25 Blue Swirl <[email protected]>:

Quoted 34 lines

Revision: 5532

         http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=5532
Author:   blueswir1
Date:     2008-10-25 11:23:27 +0000 (Sat, 25 Oct 2008)

Log Message:
-----------
Replace uses of strndup (a GNU extension) with Qemu pstrdup

Modified Paths:
--------------
trunk/cutils.c
trunk/hw/bt-hci.c
trunk/qemu-common.h

Modified: trunk/cutils.c
===================================================================

cutils.c 2008-10-25 11:23:27 UTC (rev 5532) renamed from cutils.c 2008-10-25 11:21:28 UTC (rev 5531) +11 -0
@@ -50,6 +50,18 @@
    return buf;
 }
 
+/* strdup with a limit */
+char *pstrdup(const char *str, size_t buf_size)
+{
+    size_t len;
+    char *buf;
+
+    len = MIN(buf_size, strlen(str));
+    buf = qemu_malloc(len);
+    pstrcpy(buf, len, str);
+    return buf;
+}
Quoted 4 lines
Quoted 2 lines

I think here also pstrcpy will only copy up to buf_size - 1 characters
while strndup would copy buf_size chars.

That is actually safer if we always want the strings to be NUL terminated.

strndup also always NUL terminates the string so it's just as safe,
the length is just different.

Quoted 1 line
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

But the allocation length is wrong, it should be MIN(buf_size, strlen(str) + 1).

By my reading of the manual, it should rather be MIN(buf_size, strlen(str)) + 1.

But then the length could be incorrect: buf_size + 1.

That's what it should be in case lmp_name is 248 chars long. I think the confusion is because you called the parameter buf_size while strndup calls it n (the number of characters.. not buffer size).

Note that you can use memcpy instead of pstrcpy because the length is known so there's no need for the overhead.

Cheers

author
#6 lore raw
Quoted 56 lines · andrzej zaborowski

2008/10/25 Blue Swirl <[email protected]>:

Quoted 55 lines · andrzej zaborowski
Quoted 54 lines

2008/10/25 Blue Swirl <[email protected]>:

Quoted 39 lines · andrzej zaborowski
Quoted 38 lines

2008/10/25 Blue Swirl <[email protected]>:

Quoted 34 lines

Revision: 5532

         http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=5532
Author:   blueswir1
Date:     2008-10-25 11:23:27 +0000 (Sat, 25 Oct 2008)

Log Message:
-----------
Replace uses of strndup (a GNU extension) with Qemu pstrdup

Modified Paths:
--------------
trunk/cutils.c
trunk/hw/bt-hci.c
trunk/qemu-common.h

Modified: trunk/cutils.c
===================================================================

cutils.c 2008-10-25 11:23:27 UTC (rev 5532) renamed from cutils.c 2008-10-25 11:21:28 UTC (rev 5531) +11 -0
@@ -50,6 +50,18 @@
    return buf;
 }
 
+/* strdup with a limit */
+char *pstrdup(const char *str, size_t buf_size)
+{
+    size_t len;
+    char *buf;
+
+    len = MIN(buf_size, strlen(str));
+    buf = qemu_malloc(len);
+    pstrcpy(buf, len, str);
+    return buf;
+}
Quoted 4 lines
Quoted 2 lines

I think here also pstrcpy will only copy up to buf_size - 1 characters
while strndup would copy buf_size chars.

That is actually safer if we always want the strings to be NUL terminated.

strndup also always NUL terminates the string so it's just as safe,
the length is just different.

Quoted 2 lines

But the allocation length is wrong, it should be MIN(buf_size, strlen(str) + 1).

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

By my reading of the manual, it should rather be MIN(buf_size, strlen(str)) + 1.

But then the length could be incorrect: buf_size + 1.

That's what it should be in case lmp_name is 248 chars long. I think
the confusion is because you called the parameter buf_size while strndup calls it n (the number of characters.. not buffer size).

If strlen(lmp_name) >= 248, only first 247 should be copied and the final character should be NUL, because sizeof(read_local_name_rp.name) == buf_size == 248.

Quoted 2 lines

Note that you can use memcpy instead of pstrcpy because the length is known so there's no need for the overhead.

Right.

#7 lore raw

2008/10/25 Blue Swirl <[email protected]>:

Quoted 62 lines · andrzej zaborowski
Quoted 61 lines

2008/10/25 Blue Swirl <[email protected]>:

Quoted 58 lines · andrzej zaborowski
Quoted 51 lines

2008/10/25 Blue Swirl <[email protected]>:

Quoted 39 lines · andrzej zaborowski
Quoted 38 lines

2008/10/25 Blue Swirl <[email protected]>:

Quoted 34 lines

Revision: 5532

         http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=5532
Author:   blueswir1
Date:     2008-10-25 11:23:27 +0000 (Sat, 25 Oct 2008)

Log Message:
-----------
Replace uses of strndup (a GNU extension) with Qemu pstrdup

Modified Paths:
--------------
trunk/cutils.c
trunk/hw/bt-hci.c
trunk/qemu-common.h

Modified: trunk/cutils.c
===================================================================

cutils.c 2008-10-25 11:23:27 UTC (rev 5532) renamed from cutils.c 2008-10-25 11:21:28 UTC (rev 5531) +11 -0
@@ -50,6 +50,18 @@
    return buf;
 }
 
+/* strdup with a limit */
+char *pstrdup(const char *str, size_t buf_size)
+{
+    size_t len;
+    char *buf;
+
+    len = MIN(buf_size, strlen(str));
+    buf = qemu_malloc(len);
+    pstrcpy(buf, len, str);
+    return buf;
+}
Quoted 4 lines
Quoted 2 lines

I think here also pstrcpy will only copy up to buf_size - 1 characters
while strndup would copy buf_size chars.

That is actually safer if we always want the strings to be NUL terminated.

strndup also always NUL terminates the string so it's just as safe,
the length is just different.

Quoted 1 line
Quoted 4 lines
Quoted 1 line

But the allocation length is wrong, it should be MIN(buf_size, strlen(str) + 1).

By my reading of the manual, it should rather be MIN(buf_size, strlen(str)) + 1.

But then the length could be incorrect: buf_size + 1.

Quoted 7 lines
Quoted 3 lines

That's what it should be in case lmp_name is 248 chars long. I think
the confusion is because you called the parameter buf_size while strndup calls it n (the number of characters.. not buffer size).

If strlen(lmp_name) >= 248, only first 247 should be copied and the final character should be NUL, because sizeof(read_local_name_rp.name) == buf_size == 248.

Nope, actually now I notice that I assumed that strndup never executes strlen() on the parameter... I think it's a sane assumption (?).

read_local_name_rp.name format is specified by the bluetooth "Volume 2 Core system package, part E - Host Controller Interface Functional Specification" on pg. 394:

6.24 LOCAL NAME
<...>
If the name contained in the parameter is shorter than 248 octets, the end of the name is indicated by a NULL octet (0x00), and the following octets (to fill up 248 octets, which is the length of the parameter) do not have valid values.

#10 lore raw
In reply to andrzej zaborowski

From: "andrzej zaborowski" <[email protected]>
Subject: Re: [Qemu-devel] [5532] Replace uses of strndup (a GNU extension) with Qemu pstrdup
Date: Sat, 25 Oct 2008 14:03:40 +0200

Quoted 24 lines

2008/10/25 Blue Swirl <[email protected]>:

Quoted 23 lines

Revision: 5532

         http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=5532
Author:   blueswir1
Date:     2008-10-25 11:23:27 +0000 (Sat, 25 Oct 2008)

Log Message:
-----------
Replace uses of strndup (a GNU extension) with Qemu pstrdup

Modified Paths:
--------------
trunk/cutils.c
trunk/hw/bt-hci.c
trunk/qemu-common.h

Modified: trunk/cutils.c
===================================================================

cutils.c 2008-10-25 11:23:27 UTC (rev 5532) renamed from cutils.c 2008-10-25 11:21:28 UTC (rev 5531) +0 -0
@@ -50,6 +50,18 @@
    return buf;
 }
 
Quoted 14 lines

cutils.c 2008-10-25 11:23:27 UTC (rev 5532) line 63

Quoted 11 lines
cutils.c 2008-10-25 11:23:27 UTC (rev 5532) renamed from cutils.c 2008-10-25 11:21:28 UTC (rev 5531) +11 -0
+/* strdup with a limit */
+char *pstrdup(const char *str, size_t buf_size)
+{
+    size_t len;
+    char *buf;
+
+    len = MIN(buf_size, strlen(str));
+    buf = qemu_malloc(len);
+    pstrcpy(buf, len, str);
+    return buf;
+}

I think here also pstrcpy will only copy up to buf_size - 1 characters while strndup would copy buf_size chars.

Is this really safe? If str is very long, this can still run off into the weeds...

Warner

author
#8 lore raw
In reply to andrzej zaborowski
Quoted 67 lines · andrzej zaborowski

2008/10/25 Blue Swirl <[email protected]>:

Quoted 66 lines · andrzej zaborowski
Quoted 64 lines

2008/10/25 Blue Swirl <[email protected]>:

Quoted 52 lines · andrzej zaborowski
Quoted 51 lines

2008/10/25 Blue Swirl <[email protected]>:

Quoted 39 lines · andrzej zaborowski
Quoted 38 lines

2008/10/25 Blue Swirl <[email protected]>:

Quoted 34 lines

Revision: 5532

         http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=5532
Author:   blueswir1
Date:     2008-10-25 11:23:27 +0000 (Sat, 25 Oct 2008)

Log Message:
-----------
Replace uses of strndup (a GNU extension) with Qemu pstrdup

Modified Paths:
--------------
trunk/cutils.c
trunk/hw/bt-hci.c
trunk/qemu-common.h

Modified: trunk/cutils.c
===================================================================

cutils.c 2008-10-25 11:23:27 UTC (rev 5532) renamed from cutils.c 2008-10-25 11:21:28 UTC (rev 5531) +11 -0
@@ -50,6 +50,18 @@
    return buf;
 }
 
+/* strdup with a limit */
+char *pstrdup(const char *str, size_t buf_size)
+{
+    size_t len;
+    char *buf;
+
+    len = MIN(buf_size, strlen(str));
+    buf = qemu_malloc(len);
+    pstrcpy(buf, len, str);
+    return buf;
+}
Quoted 4 lines
Quoted 2 lines

I think here also pstrcpy will only copy up to buf_size - 1 characters
while strndup would copy buf_size chars.

That is actually safer if we always want the strings to be NUL terminated.

strndup also always NUL terminates the string so it's just as safe,
the length is just different.

Quoted 1 line
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

But the allocation length is wrong, it should be MIN(buf_size, strlen(str) + 1).

By my reading of the manual, it should rather be MIN(buf_size, strlen(str)) + 1.

But then the length could be incorrect: buf_size + 1.

That's what it should be in case lmp_name is 248 chars long. I think
the confusion is because you called the parameter buf_size while strndup calls it n (the number of characters.. not buffer size).

Quoted 7 lines
Quoted 3 lines

If strlen(lmp_name) >= 248, only first 247 should be copied and the final character should be NUL, because sizeof(read_local_name_rp.name) == buf_size == 248.

Nope, actually now I notice that I assumed that strndup never executes
strlen() on the parameter... I think it's a sane assumption (?).

Yes, glibc uses strnlen:
http://sources.redhat.com/cgi-bin/cvsweb.cgi/libc/string/strndup.c?rev=1.11&content-type=text/x-cvsweb-markup&cvsroot=glibc

Quoted 10 lines

read_local_name_rp.name format is specified by the bluetooth "Volume 2 Core system package, part E - Host Controller Interface Functional Specification" on pg. 394:

6.24 LOCAL NAME
<...>
If the name contained in the parameter is shorter than 248 octets, the end of the name is indicated by a NULL octet (0x00), and the following octets (to fill up 248 octets, which is the length of the parameter) do not have valid values.

So, in this case neither strndup nor pstrdup are correct, because you do not want the string to be NUL terminated in the strlen(lmp_name) >= 248 case?

#9 lore raw

2008/10/26 Blue Swirl <[email protected]>:

Quoted 73 lines · andrzej zaborowski
Quoted 72 lines

2008/10/25 Blue Swirl <[email protected]>:

Quoted 69 lines · andrzej zaborowski
Quoted 56 lines

2008/10/25 Blue Swirl <[email protected]>:

Quoted 55 lines · andrzej zaborowski
Quoted 54 lines

2008/10/25 Blue Swirl <[email protected]>:

Quoted 39 lines · andrzej zaborowski
Quoted 38 lines

2008/10/25 Blue Swirl <[email protected]>:

Quoted 34 lines

Revision: 5532

         http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=5532
Author:   blueswir1
Date:     2008-10-25 11:23:27 +0000 (Sat, 25 Oct 2008)

Log Message:
-----------
Replace uses of strndup (a GNU extension) with Qemu pstrdup

Modified Paths:
--------------
trunk/cutils.c
trunk/hw/bt-hci.c
trunk/qemu-common.h

Modified: trunk/cutils.c
===================================================================

cutils.c 2008-10-25 11:23:27 UTC (rev 5532) renamed from cutils.c 2008-10-25 11:21:28 UTC (rev 5531) +11 -0
@@ -50,6 +50,18 @@
    return buf;
 }
 
+/* strdup with a limit */
+char *pstrdup(const char *str, size_t buf_size)
+{
+    size_t len;
+    char *buf;
+
+    len = MIN(buf_size, strlen(str));
+    buf = qemu_malloc(len);
+    pstrcpy(buf, len, str);
+    return buf;
+}
Quoted 4 lines
Quoted 2 lines

I think here also pstrcpy will only copy up to buf_size - 1 characters
while strndup would copy buf_size chars.

That is actually safer if we always want the strings to be NUL terminated.

strndup also always NUL terminates the string so it's just as safe,
the length is just different.

Quoted 2 lines

But the allocation length is wrong, it should be MIN(buf_size, strlen(str) + 1).

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

By my reading of the manual, it should rather be MIN(buf_size, strlen(str)) + 1.

But then the length could be incorrect: buf_size + 1.

That's what it should be in case lmp_name is 248 chars long. I think
the confusion is because you called the parameter buf_size while strndup calls it n (the number of characters.. not buffer size).

If strlen(lmp_name) >= 248, only first 247 should be copied and the final character should be NUL, because sizeof(read_local_name_rp.name) == buf_size == 248.

Quoted 5 lines
Quoted 2 lines

Nope, actually now I notice that I assumed that strndup never executes
strlen() on the parameter... I think it's a sane assumption (?).

Yes, glibc uses strnlen:
http://sources.redhat.com/cgi-bin/cvsweb.cgi/libc/string/strndup.c?rev=1.11&content-type=text/x-cvsweb-markup&cvsroot=glibc

Ah, cool.

Quoted 12 lines
Quoted 11 lines

read_local_name_rp.name format is specified by the bluetooth "Volume 2 Core system package, part E - Host Controller Interface Functional Specification" on pg. 394:

6.24 LOCAL NAME
<...>
If the name contained in the parameter is shorter than 248 octets, the end of the name is indicated by a NULL octet (0x00), and the following octets (to fill up 248 octets, which is the length of the parameter) do not have valid values.

Quoted 3 lines

So, in this case neither strndup nor pstrdup are correct, because you do not want the string to be NUL terminated in the strlen(lmp_name) >= 248 case?

PARAM(change_local_name, name) format is defined by the specification, but hci->device.lmp_name was just a C string, so that it could be used for example with printf in qemu. strndup converted it from one format to the other so it was correct. strncpy converted it back.

Cheers

#11 lore raw
In reply to Blue Swirl

2008/10/25 Blue Swirl <[email protected]>:

Quoted 58 lines

Revision: 5532

         http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=5532
Author:   blueswir1
Date:     2008-10-25 11:23:27 +0000 (Sat, 25 Oct 2008)

Log Message:
-----------
Replace uses of strndup (a GNU extension) with Qemu pstrdup

Modified Paths:
--------------
trunk/cutils.c
trunk/hw/bt-hci.c
trunk/qemu-common.h

Modified: trunk/cutils.c
===================================================================

cutils.c 2008-10-25 11:23:27 UTC (rev 5532) renamed from cutils.c 2008-10-25 11:21:28 UTC (rev 5531) +12 -0
@@ -50,6 +50,18 @@
    return buf;
 }
 
+/* strdup with a limit */
+char *pstrdup(const char *str, size_t buf_size)
+{
+    size_t len;
+    char *buf;
+
+    len = MIN(buf_size, strlen(str));
+    buf = qemu_malloc(len);
+    pstrcpy(buf, len, str);
+    return buf;
+}
+
 int strstart(const char *str, const char *val, const char **ptr)
 {
    const char *p, *q;

Modified: trunk/hw/bt-hci.c
===================================================================

hw/bt-hci.c 2008-10-25 11:23:27 UTC (rev 5532) renamed from hw/bt-hci.c 2008-10-25 11:21:28 UTC (rev 5531) +1 -1
@@ -1814,7 +1814,7 @@
 
        if (hci->device.lmp_name)
            free((void *) hci->device.lmp_name);
-        hci->device.lmp_name = strndup(PARAM(change_local_name, name),
+        hci->device.lmp_name = pstrdup(PARAM(change_local_name, name),
                        sizeof(PARAM(change_local_name, name)));
        bt_hci_event_complete_status(hci, HCI_SUCCESS);
        break;

Modified: trunk/qemu-common.h
===================================================================

qemu-common.h 2008-10-25 11:23:27 UTC (rev 5532) line 88

Quoted 7 lines
qemu-common.h 2008-10-25 11:23:27 UTC (rev 5532) renamed from qemu-common.h 2008-10-25 11:21:28 UTC (rev 5531) +1 -0
@@ -82,6 +82,7 @@
 /* cutils.c */
 void pstrcpy(char *buf, int buf_size, const char *str);
 char *pstrcat(char *buf, int buf_size, const char *s);
+char *pstrdup(const char *str, size_t buf_size);
 int strstart(const char *str, const char *val, const char **ptr);
 int stristart(const char *str, const char *val, const char **ptr);
 time_t mktimegm(struct tm *tm);

I would like to revert this and part of the following commit so I can merge the missing interface for bt and have it work.

Cheers

author
#12 lore raw
Quoted 55 lines · andrzej zaborowski

2008/10/25 Blue Swirl <[email protected]>:

Quoted 53 lines

Revision: 5532

         http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=5532
Author:   blueswir1
Date:     2008-10-25 11:23:27 +0000 (Sat, 25 Oct 2008)

Log Message:
-----------
Replace uses of strndup (a GNU extension) with Qemu pstrdup

Modified Paths:
--------------
trunk/cutils.c
trunk/hw/bt-hci.c
trunk/qemu-common.h

Modified: trunk/cutils.c
===================================================================

cutils.c 2008-10-25 11:23:27 UTC (rev 5532) renamed from cutils.c 2008-10-25 11:21:28 UTC (rev 5531) +12 -0
@@ -50,6 +50,18 @@
    return buf;
 }
 
+/* strdup with a limit */
+char *pstrdup(const char *str, size_t buf_size)
+{
+    size_t len;
+    char *buf;
+
+    len = MIN(buf_size, strlen(str));
+    buf = qemu_malloc(len);
+    pstrcpy(buf, len, str);
+    return buf;
+}
+
 int strstart(const char *str, const char *val, const char **ptr)
 {
    const char *p, *q;

Modified: trunk/hw/bt-hci.c
===================================================================

hw/bt-hci.c 2008-10-25 11:23:27 UTC (rev 5532) renamed from hw/bt-hci.c 2008-10-25 11:21:28 UTC (rev 5531) +1 -1
@@ -1814,7 +1814,7 @@
 
        if (hci->device.lmp_name)
            free((void *) hci->device.lmp_name);
-        hci->device.lmp_name = strndup(PARAM(change_local_name, name),
+        hci->device.lmp_name = pstrdup(PARAM(change_local_name, name),
                        sizeof(PARAM(change_local_name, name)));
        bt_hci_event_complete_status(hci, HCI_SUCCESS);
        break;
Quoted 16 lines

qemu-common.h 2008-10-25 11:23:27 UTC (rev 5532) line 88

Quoted 12 lines

Modified: trunk/qemu-common.h
===================================================================

qemu-common.h 2008-10-25 11:23:27 UTC (rev 5532) renamed from qemu-common.h 2008-10-25 11:21:28 UTC (rev 5531) +1 -0
@@ -82,6 +82,7 @@
 /* cutils.c */
 void pstrcpy(char *buf, int buf_size, const char *str);
 char *pstrcat(char *buf, int buf_size, const char *s);
+char *pstrdup(const char *str, size_t buf_size);
 int strstart(const char *str, const char *val, const char **ptr);
 int stristart(const char *str, const char *val, const char **ptr);
 time_t mktimegm(struct tm *tm);

I would like to revert this and part of the following commit so I can
merge the missing interface for bt and have it work.

Well, strndup is still a GNU extension, so it may not be present in all systems. What if pstrdup was changed to match strndup, would that be OK?

#13 lore raw

2008/11/1 Blue Swirl <[email protected]>:

Quoted 70 lines · andrzej zaborowski
Quoted 69 lines

2008/10/25 Blue Swirl <[email protected]>:

Quoted 65 lines

Revision: 5532

         http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=5532
Author:   blueswir1
Date:     2008-10-25 11:23:27 +0000 (Sat, 25 Oct 2008)

Log Message:
-----------
Replace uses of strndup (a GNU extension) with Qemu pstrdup

Modified Paths:
--------------
trunk/cutils.c
trunk/hw/bt-hci.c
trunk/qemu-common.h

Modified: trunk/cutils.c
===================================================================

cutils.c 2008-10-25 11:23:27 UTC (rev 5532) renamed from cutils.c 2008-10-25 11:21:28 UTC (rev 5531) +12 -0
@@ -50,6 +50,18 @@
    return buf;
 }
 
+/* strdup with a limit */
+char *pstrdup(const char *str, size_t buf_size)
+{
+    size_t len;
+    char *buf;
+
+    len = MIN(buf_size, strlen(str));
+    buf = qemu_malloc(len);
+    pstrcpy(buf, len, str);
+    return buf;
+}
+
 int strstart(const char *str, const char *val, const char **ptr)
 {
    const char *p, *q;

Modified: trunk/hw/bt-hci.c
===================================================================

hw/bt-hci.c 2008-10-25 11:23:27 UTC (rev 5532) renamed from hw/bt-hci.c 2008-10-25 11:21:28 UTC (rev 5531) +1 -1
@@ -1814,7 +1814,7 @@
 
        if (hci->device.lmp_name)
            free((void *) hci->device.lmp_name);
-        hci->device.lmp_name = strndup(PARAM(change_local_name, name),
+        hci->device.lmp_name = pstrdup(PARAM(change_local_name, name),
                        sizeof(PARAM(change_local_name, name)));
        bt_hci_event_complete_status(hci, HCI_SUCCESS);
        break;

Modified: trunk/qemu-common.h
===================================================================

qemu-common.h 2008-10-25 11:23:27 UTC (rev 5532) renamed from qemu-common.h 2008-10-25 11:21:28 UTC (rev 5531) +1 -0
@@ -82,6 +82,7 @@
 /* cutils.c */
 void pstrcpy(char *buf, int buf_size, const char *str);
 char *pstrcat(char *buf, int buf_size, const char *s);
+char *pstrdup(const char *str, size_t buf_size);
 int strstart(const char *str, const char *val, const char **ptr);
 int stristart(const char *str, const char *val, const char **ptr);
 time_t mktimegm(struct tm *tm);
Quoted 6 lines
Quoted 2 lines

I would like to revert this and part of the following commit so I can
merge the missing interface for bt and have it work.

Well, strndup is still a GNU extension, so it may not be present in all systems. What if pstrdup was changed to match strndup, would that be OK?

There's still the strncpy change.

My idea was to just add strndup to osdep.c because if the semantics are to match anyway then the rename only adds confusion.

Cheers