memcpy behaviour when destination unknown

Hi *,

General behaviour is __builtin_memcpy_chk function should be called for all memcpy calls, to check object size(destination).

I have noticed in clang for memcpy function when destination buffer unknown than, memcpy calls optimized to memcpy.
Where as if destination has known pointer at compilation time than it will call builtin_memcpy_chk which in turn calls memcpy.

extern int l1;

test_func ()
char buf1[10], buf2[10];

memcpy(buf1, s3, l1); // Destination is known(buf1), length is not known calls memcpy_chk

r = l1 == 1 ? buf1 : buf2;
memcpy(r, s3, l1 + 1); // Destination is not known(might be buf1 or buf2), so optimizing call to memcpy


To compare the behaviour with gcc, for all above memcpy calls memcpy_chk function called.

I have attached codes of sample program (test program, as well library call impl).

When sample code run on CLANG, Its abort. where as in GCC it run sucessfully.

Any info why the optimization behaviour in clang is differ from gcc.


further info about object size checking in gcc.

main.c (1.23 KB)

chk.c (643 Bytes)


So this is related with the way that the objectsize intrinsic is implemented in LLVM.
At the moment, LLVM doesn't support fetching the minimum/maximum size of a set of objects. It can only return the size of a single object. However, your example is trivial to handle since both objects have the same size.

Feel free to submit a bug report to LLVM's bug database.


Citando rajesh viswabramana <>: