mirror of
https://gitlab.uni-freiburg.de/opensourcevdi/spice-common
synced 2025-12-28 16:14:34 +00:00
Add spice_strndup
Also, make str(n)dup handle NULL correctly
This commit is contained in:
parent
a12f3fe242
commit
998bf873bf
18
common/mem.c
18
common/mem.c
@ -32,11 +32,29 @@ char *spice_strdup(const char *str)
|
||||
{
|
||||
char *copy;
|
||||
|
||||
if (str == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
copy = (char *)spice_malloc(strlen(str) + 1);
|
||||
strcpy(copy, str);
|
||||
return copy;
|
||||
}
|
||||
|
||||
char *spice_strndup(const char *str, size_t n_bytes)
|
||||
{
|
||||
char *copy;
|
||||
|
||||
if (str == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
copy = (char *)spice_malloc(n_bytes + 1);
|
||||
strncpy(copy, str, n_bytes);
|
||||
copy[n_bytes] = 0;
|
||||
return copy;
|
||||
}
|
||||
|
||||
void *spice_memdup(const void *mem, size_t n_bytes)
|
||||
{
|
||||
void *copy;
|
||||
|
||||
@ -23,6 +23,7 @@
|
||||
#include <spice/macros.h>
|
||||
|
||||
char *spice_strdup(const char *str) SPICE_GNUC_MALLOC;
|
||||
char *spice_strndup(const char *str, size_t n_bytes) SPICE_GNUC_MALLOC;
|
||||
void *spice_memdup(const void *mem, size_t n_bytes) SPICE_GNUC_MALLOC;
|
||||
void *spice_malloc(size_t n_bytes) SPICE_GNUC_MALLOC SPICE_GNUC_ALLOC_SIZE(1);
|
||||
void *spice_malloc0(size_t n_bytes) SPICE_GNUC_MALLOC SPICE_GNUC_ALLOC_SIZE(1);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user