From 70f4df4cd8b51f7406914a38d268d0d431e8f55a Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 31 Aug 2015 20:00:31 +0200 Subject: fix dupa macros MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- include/string.h | 19 +++++++++++-------- include/wchar.h | 27 +++++++++++++++------------ 2 files changed, 26 insertions(+), 20 deletions(-) (limited to 'include') diff --git a/include/string.h b/include/string.h index 6c5c80e..09bdc5e 100644 --- a/include/string.h +++ b/include/string.h @@ -655,9 +655,10 @@ void* memdup(const void*, size_t) */ # define strdupa(string) \ ({ \ - size_t n = strlen(string) + 1; \ - char* r = __builtin_alloca(n * sizeof(char)); \ - memcpy(r, string, n); \ + const char* __s = (string); \ + size_t __n = strlen(__s) + 1; \ + char* __r = __builtin_alloca(__n * sizeof(char)); \ + memcpy(__r, __s, __n); \ }) # endif @@ -676,9 +677,10 @@ void* memdup(const void*, size_t) */ # define strndupa(string, maxlen) \ ({ \ - size_t n = strnlen(string, maxlen) + 1; \ - char* r = __builtin_alloca(n * sizeof(char)); \ - memcpy(r, string, n); \ + const char* __s = (string); \ + size_t __n = strnlen(__s, (maxlen)) + 1; \ + char* __r = __builtin_alloca(__n * sizeof(char)); \ + memcpy(__r, __s, __n); \ }) # endif @@ -696,8 +698,9 @@ void* memdup(const void*, size_t) */ # define memdupa(segment, size) \ ({ \ - wchar_t* r = __builtin_alloca(size * sizeof(wchar_t)); \ - memcpy(r, segment, size); \ + size_t __n = (size); \ + wchar_t* __r = __builtin_alloca(__n * sizeof(wchar_t)); \ + memcpy(__r, (segment), __n); \ }) # endif # endif diff --git a/include/wchar.h b/include/wchar.h index 32f32b0..d00056e 100644 --- a/include/wchar.h +++ b/include/wchar.h @@ -580,11 +580,12 @@ wchar_t* wmemdup(const wchar_t*, size_t) * @return :size_t The new string. There is no way to * detect whether the allocation failed. */ -# define wcsdupa(string) \ - ({ \ - size_t n = wcslen(string) + 1; \ - wchar_t* r = __builtin_alloca(n * sizeof(char)); \ - wmemcpy(r, string, n); \ +# define wcsdupa(string) \ + ({ \ + const char* __s = (string); \ + size_t __n = wcslen(__s) + 1; \ + wchar_t* __r = __builtin_alloca(__n * sizeof(char)); \ + wmemcpy(__r, __s, __n); \ }) /** @@ -600,11 +601,12 @@ wchar_t* wmemdup(const wchar_t*, size_t) * @return :size_t The new string. There is no way to * detect whether the allocation failed. */ -# define wstrndupa(string, maxlen) \ - ({ \ - size_t n = wcsnlen(string, maxlen) + 1; \ - wchar_t* r = __builtin_alloca(n * sizeof(wchar_t)); \ - wmemcpy(r, string, n); \ +# define wstrndupa(string, maxlen) \ + ({ \ + const char* __s = (string); \ + size_t __n = wcsnlen(__s, (maxlen)) + 1; \ + wchar_t* __r = __builtin_alloca(__n * sizeof(wchar_t)); \ + wmemcpy(__r, __s, __n); \ }) # endif @@ -621,8 +623,9 @@ wchar_t* wmemdup(const wchar_t*, size_t) */ # define wmemdupa(segment, size) \ ({ \ - wchar_t* r = __builtin_alloca(size * sizeof(wchar_t)); \ - wmemcpy(r, segmetn, size); \ + size_t __n = (size); \ + wchar_t* __r = __builtin_alloca(__n * sizeof(wchar_t)); \ + wmemcpy(__r, (segmetn), __n); \ }) # endif # endif -- cgit v1.2.3-70-g09d2