Force nul-termination of strings after vsnprintf().
[oweals/tinc.git] / src / xmalloc.c
index 1e4001738aca3b59a2408759d23a199607019c12..a1b1fe8be87704d3cacdf37de7e2f728f1b9a2f8 100644 (file)
@@ -113,6 +113,9 @@ char *xstrdup(const char *s)
 {
   char *p;
   
+  if(!s)
+    return NULL;
+
   p = strdup(s);
   if(!p)
     xalloc_fail ((int)strlen(s));
@@ -152,6 +155,7 @@ int xvasprintf(char **strp, const char *fmt, va_list ap) {
        int result = vsnprintf(buf, sizeof buf, fmt, ap);
        if(result < 0)
                exit(xalloc_exit_failure);
+       buf[sizeof buf - 1] = 0;
        *strp = xstrdup(buf);
 #else
        int result = vasprintf(strp, fmt, ap);