Problem with Object Size Checking and reallocarray

Yaakov Selkowitz yselkowitz@cygwin.com
Tue Mar 13 20:54:00 GMT 2018


On 2018-03-13 13:28, Richard Earnshaw (lists) wrote:
> On 13/03/18 14:05, Yaakov Selkowitz wrote:
>> On 2018-03-13 07:51, Corinna Vinschen wrote:
>>> On Mar 12 19:58, Jon Turney wrote:
>>>> reallocarray() is annotated in stdlib.h with '__alloc_size(2)
>>>> __alloc_size(3)'
>>>>
>>>> per [1], this doesn't seem to be the correct syntax when the size is the
>>>> product of the arguments, and the last alloc_size seems to be silently
>>>> winning.
>>>>
>>>> If I change this to '__alloc_size((2,3))' (as in the patch attached),
>>>> __builtin_object_size doesn't seem to be a compile-time constant anymore,
>>>> and so memcpy() evaluates differently, so it's hard to be sure that's
>>>> actually correct...
>>>>
>>>> [1] https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html
>>>
>>> Yaakov, care to comment and push if the patch is ok?
>>
>> Thanks, pushed.
>>
> 
> This doesn't work at all.  I get:
> 
> warning: alloc_size parameter outside range [-Wattributes]
> 
> There's too many levels of parenthesis around the parameters, so it
> expands to
> void *reallocarray(void *, size_t, size_t)
> __attribute__((__alloc_size__((2,3))));
> 
> And this causes all the testsuites to start failing due tot he warning.
> 
> To silence the warning it needs to be __attribute__((__alloc_size__(2,3)))
> 
> I'm not sure how you achieve that, given the macro expansion going on here.

Does the attached help?

-- 
Yaakov
-------------- next part --------------
diff --git a/newlib/libc/include/stdlib.h b/newlib/libc/include/stdlib.h
index 593760a12..564ce8a28 100644
--- a/newlib/libc/include/stdlib.h
+++ b/newlib/libc/include/stdlib.h
@@ -324,8 +324,8 @@ extern long double strtold (const char *__restrict, char **__restrict);
  * If we're in a mode greater than C99, expose C11 functions.
  */
 #if __ISO_C_VISIBLE >= 2011
-void *	aligned_alloc(size_t, size_t) __malloc_like __alloc_align(1)
-	    __alloc_size(2);
+void *	aligned_alloc(size_t, size_t) __malloc_like __alloc_align((1))
+	    __alloc_size((2));
 int	at_quick_exit(void (*)(void));
 _Noreturn void
 	quick_exit(int);
diff --git a/newlib/libc/include/sys/cdefs.h b/newlib/libc/include/sys/cdefs.h
index fc564a5c6..b3f8d1965 100644
--- a/newlib/libc/include/sys/cdefs.h
+++ b/newlib/libc/include/sys/cdefs.h
@@ -258,12 +258,12 @@
 #define	__section(x)	__attribute__((__section__(x)))
 #endif
 #if __GNUC_PREREQ__(4, 3) || __has_attribute(__alloc_size__)
-#define	__alloc_size(x)	__attribute__((__alloc_size__(x)))
+#define	__alloc_size(x)	__attribute__((__alloc_size__ x))
 #else
 #define	__alloc_size(x)
 #endif
 #if __GNUC_PREREQ__(4, 9) || __has_attribute(__alloc_align__)
-#define	__alloc_align(x)	__attribute__((__alloc_align__(x)))
+#define	__alloc_align(x)	__attribute__((__alloc_align__ x))
 #else
 #define	__alloc_align(x)
 #endif
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 228 bytes
Desc: OpenPGP digital signature
URL: <http://sourceware.org/pipermail/newlib/attachments/20180313/aa11088e/attachment.sig>


More information about the Newlib mailing list