This is the mail archive of the newlib@sourceware.org mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Problem with Object Size Checking and reallocarray


On Mar 12 19:58, Jon Turney wrote:
> 
> // gcc test.c -o test.exe -g -O2 -Wp,-D_FORTIFY_SOURCE=2
> 
> //
> // extracted from InputLineAddChar in xserver/xkb/maprules.c
> //
> 
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> 
> int main()
> {
>   const char *buf[128];
>   char *line = reallocarray(NULL, 128, 2);
>   // size of line is 128*2 = 256
>   printf("%zu\n", __builtin_object_size(line, 0));
>   memcpy(line, buf, 128);
>   // __mempcy_chk tests against size 2, and terminates
> }
> 
> 
> 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,
Corinna

-- 
Corinna Vinschen
Cygwin Maintainer
Red Hat

Attachment: signature.asc
Description: PGP signature


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]