From: Lucas De Marchi Date: Mon, 7 Apr 2014 15:27:11 +0000 (-0300) Subject: Make sure there's NUL byte at the end of strndupa X-Git-Tag: v18~22 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=f5cdd574a531ed156d30efe2e06c4cf463469588;p=thirdparty%2Fkmod.git 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 a2864469..8d47af86 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