Make sure there's NUL byte at the end of strndupa
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.
diff --git a/libkmod/missing.h b/libkmod/missing.h
index a286446..8d47af8 100644
--- a/libkmod/missing.h
+++ b/libkmod/missing.h
@@ -34,9 +34,12 @@
#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