diff options
author | balrog <balrog@c046a42c-6fe2-441c-8c8c-71466251a162> | 2008-11-09 00:28:40 +0000 |
---|---|---|
committer | balrog <balrog@c046a42c-6fe2-441c-8c8c-71466251a162> | 2008-11-09 00:28:40 +0000 |
commit | ac4b0d0c4feb291643c0e8a07a92e449e13881b5 (patch) | |
tree | eaa7265c8e10149b0fe4a7a3a8c995b1026cd1ae /qemu-malloc.c | |
parent | dc72ac14d8ceeaac0ca63f385ba3f44fd580b963 (diff) |
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
Diffstat (limited to 'qemu-malloc.c')
-rw-r--r-- | qemu-malloc.c | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/qemu-malloc.c b/qemu-malloc.c index 3bffae1fb..dc74efed1 100644 --- a/qemu-malloc.c +++ b/qemu-malloc.c @@ -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); +} |