mirror of
https://github.com/Motorhead1991/qemu.git
synced 2025-09-09 00:07:57 -06:00
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:
parent
dc72ac14d8
commit
ac4b0d0c4f
3 changed files with 20 additions and 5 deletions
|
@ -60,6 +60,20 @@ char *qemu_strdup(const char *str)
|
|||
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);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue