Make sure there's NUL byte at the end of strndupa
authorLucas De Marchi <lucas.demarchi@intel.com>
Mon, 7 Apr 2014 15:27:11 +0000 (12:27 -0300)
committerLucas De Marchi <lucas.demarchi@intel.com>
Mon, 7 Apr 2014 15:30:04 +0000 (12:30 -0300)
Since strcpy() doesn't ensure we have a NUL byte in the resulting
string, use alloca() + memcpy(). Also make sure we don't evaluate "s"
twice.

libkmod/missing.h

index a286446..8d47af8 100644 (file)
@@ -34,9 +34,12 @@ static inline int finit_module(int fd, const char *uargs, int flags)
 #endif
 
 #if !HAVE_DECL_STRNDUPA
-#define strndupa(s, length) \
-       ({ \
-               size_t __len = strnlen((s), (length)); \
-               strncpy(alloca(__len + 1), (s), __len);  \
+#define strndupa(s, n)                                                 \
+       ({                                                              \
+               const char *__old = (s);                                \
+               size_t __len = strnlen(__old, (n));                     \
+               char *__new = alloca(__len + 1);                        \
+               __new[__len] = '\0';                                    \
+               memcpy(__new, __old, __len);                            \
         })
 #endif