From: Ben Pfaff Date: Thu, 10 Jul 2008 17:45:05 +0000 (-0700) Subject: New function xmemdup0(). X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=47cebfdbefaf502e557e63beab2d68bd1cc462a7;p=openvswitch New function xmemdup0(). --- diff --git a/include/util.h b/include/util.h index 8b0bb402..99319d4c 100644 --- a/include/util.h +++ b/include/util.h @@ -81,6 +81,7 @@ void *xmalloc(size_t); void *xcalloc(size_t, size_t); void *xrealloc(void *, size_t); void *xmemdup(const void *, size_t); +char *xmemdup0(const char *, size_t); char *xstrdup(const char *); char *xasprintf(const char *format, ...) PRINTF_FORMAT(1, 2); diff --git a/lib/util.c b/lib/util.c index 04fcd90d..230a2470 100644 --- a/lib/util.c +++ b/lib/util.c @@ -83,10 +83,19 @@ xmemdup(const void *p_, size_t size) return p; } +char * +xmemdup0(const char *p_, size_t length) +{ + char *p = xmalloc(length + 1); + memcpy(p, p_, length); + p[length] = '\0'; + return p; +} + char * xstrdup(const char *s) { - return xmemdup(s, strlen(s) + 1); + return xmemdup0(s, strlen(s)); } char *