From f5cdd574a531ed156d30efe2e06c4cf463469588 Mon Sep 17 00:00:00 2001 From: Lucas De Marchi Date: Mon, 7 Apr 2014 12:27:11 -0300 Subject: [PATCH] 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. --- libkmod/missing.h | 11 +++++++---- 1 file 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 -- 2.7.4