summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLucas De Marchi <lucas.demarchi@intel.com>2014-04-07 12:27:11 -0300
committerLucas De Marchi <lucas.demarchi@intel.com>2014-04-07 12:30:04 -0300
commitf5cdd574a531ed156d30efe2e06c4cf463469588 (patch)
tree26e07952aea7dddd8e2f5701c5efaaa4f7d9e6fa
parent04c0956e207e3b27a700494f8dc77f902cd53731 (diff)
downloadkmod-f5cdd574a531ed156d30efe2e06c4cf463469588.tar.gz
kmod-f5cdd574a531ed156d30efe2e06c4cf463469588.tar.bz2
kmod-f5cdd574a531ed156d30efe2e06c4cf463469588.zip
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.
-rw-r--r--libkmod/missing.h11
1 files changed, 7 insertions, 4 deletions
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 @@ 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