qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Andrzej Zaborowski <balrogg@gmail.com>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [5653] Add qemu_strndup: qemu_strdup with length limit.
Date: Sun, 09 Nov 2008 00:28:40 +0000	[thread overview]
Message-ID: <E1KyyAS-00055l-Ip@cvs.savannah.gnu.org> (raw)

Revision: 5653
          http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=5653
Author:   balrog
Date:     2008-11-09 00:28:40 +0000 (Sun, 09 Nov 2008)

Log Message:
-----------
Add qemu_strndup: qemu_strdup with length limit.

Also optimise qemu_strdup by using memcpy - using pstrcpy is usually 
suboptimal.

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

Modified: trunk/hw/bt-hci.c
===================================================================
--- trunk/hw/bt-hci.c	2008-11-09 00:04:26 UTC (rev 5652)
+++ trunk/hw/bt-hci.c	2008-11-09 00:28:40 UTC (rev 5653)
@@ -1137,7 +1137,7 @@
     hci->device.inquiry_scan = 0;
     hci->device.page_scan = 0;
     if (hci->device.lmp_name)
-        free((void *) hci->device.lmp_name);
+        qemu_free((void *) hci->device.lmp_name);
     hci->device.lmp_name = 0;
     hci->device.class[0] = 0x00;
     hci->device.class[1] = 0x00;
@@ -1815,8 +1815,8 @@
         LENGTH_CHECK(change_local_name);
 
         if (hci->device.lmp_name)
-            free((void *) hci->device.lmp_name);
-        hci->device.lmp_name = strndup(PARAM(change_local_name, name),
+            qemu_free((void *) hci->device.lmp_name);
+        hci->device.lmp_name = qemu_strndup(PARAM(change_local_name, name),
                         sizeof(PARAM(change_local_name, name)));
         bt_hci_event_complete_status(hci, HCI_SUCCESS);
         break;
@@ -2191,7 +2191,7 @@
     bt_device_done(&hci->device);
 
     if (hci->device.lmp_name)
-        free((void *) hci->device.lmp_name);
+        qemu_free((void *) hci->device.lmp_name);
 
     /* Be gentle and send DISCONNECT to all connected peers and those
      * currently waiting for us to accept or reject a connection request.

Modified: trunk/qemu-common.h
===================================================================
--- trunk/qemu-common.h	2008-11-09 00:04:26 UTC (rev 5652)
+++ trunk/qemu-common.h	2008-11-09 00:28:40 UTC (rev 5653)
@@ -98,6 +98,7 @@
 void *qemu_mallocz(size_t size);
 void qemu_free(void *ptr);
 char *qemu_strdup(const char *str);
+char *qemu_strndup(const char *str, size_t size);
 
 void *get_mmap_addr(unsigned long size);
 

Modified: trunk/qemu-malloc.c
===================================================================
--- trunk/qemu-malloc.c	2008-11-09 00:04:26 UTC (rev 5652)
+++ trunk/qemu-malloc.c	2008-11-09 00:28:40 UTC (rev 5653)
@@ -60,6 +60,20 @@
     ptr = qemu_malloc(len + 1);
     if (!ptr)
         return NULL;
-    pstrcpy(ptr, len + 1, str);
+    memcpy(ptr, str, len + 1);
     return ptr;
 }
+
+char *qemu_strndup(const char *str, size_t size)
+{
+    const char *end = memchr(str, 0, size);
+    char *new;
+
+    if (end)
+        size = end - str;
+
+    new = qemu_malloc(size + 1);
+    new[size] = 0;
+
+    return memcpy(new, str, size);
+}

                 reply	other threads:[~2008-11-09  0:28 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=E1KyyAS-00055l-Ip@cvs.savannah.gnu.org \
    --to=balrogg@gmail.com \
    --cc=qemu-devel@nongnu.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).