Add qemu_strndup: qemu_strdup with length limit.

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


git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@5653 c046a42c-6fe2-441c-8c8c-71466251a162
This commit is contained in:
balrog 2008-11-09 00:28:40 +00:00
parent dc72ac14d8
commit ac4b0d0c4f
3 changed files with 20 additions and 5 deletions

View File

@ -1137,7 +1137,7 @@ static void bt_hci_reset(struct bt_hci_s *hci)
hci->device.inquiry_scan = 0; hci->device.inquiry_scan = 0;
hci->device.page_scan = 0; hci->device.page_scan = 0;
if (hci->device.lmp_name) 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.lmp_name = 0;
hci->device.class[0] = 0x00; hci->device.class[0] = 0x00;
hci->device.class[1] = 0x00; hci->device.class[1] = 0x00;
@ -1815,8 +1815,8 @@ static void bt_submit_hci(struct HCIInfo *info,
LENGTH_CHECK(change_local_name); LENGTH_CHECK(change_local_name);
if (hci->device.lmp_name) if (hci->device.lmp_name)
free((void *) hci->device.lmp_name); qemu_free((void *) hci->device.lmp_name);
hci->device.lmp_name = strndup(PARAM(change_local_name, name), hci->device.lmp_name = qemu_strndup(PARAM(change_local_name, name),
sizeof(PARAM(change_local_name, name))); sizeof(PARAM(change_local_name, name)));
bt_hci_event_complete_status(hci, HCI_SUCCESS); bt_hci_event_complete_status(hci, HCI_SUCCESS);
break; break;
@ -2191,7 +2191,7 @@ static void bt_hci_done(struct HCIInfo *info)
bt_device_done(&hci->device); bt_device_done(&hci->device);
if (hci->device.lmp_name) 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 /* Be gentle and send DISCONNECT to all connected peers and those
* currently waiting for us to accept or reject a connection request. * currently waiting for us to accept or reject a connection request.

View File

@ -98,6 +98,7 @@ void *qemu_realloc(void *ptr, size_t size);
void *qemu_mallocz(size_t size); void *qemu_mallocz(size_t size);
void qemu_free(void *ptr); void qemu_free(void *ptr);
char *qemu_strdup(const char *str); char *qemu_strdup(const char *str);
char *qemu_strndup(const char *str, size_t size);
void *get_mmap_addr(unsigned long size); void *get_mmap_addr(unsigned long size);

View File

@ -60,6 +60,20 @@ char *qemu_strdup(const char *str)
ptr = qemu_malloc(len + 1); ptr = qemu_malloc(len + 1);
if (!ptr) if (!ptr)
return NULL; return NULL;
pstrcpy(ptr, len + 1, str); memcpy(ptr, str, len + 1);
return ptr; 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);
}