ldc-developers/ldc — pull request #342 by redstar (2013-05-09 20:16:57+00:00)
Title: Do not memcpy with same source and destination.

This can happen for sret_args. Simply do not perform the memcpy.

I found this with valgrind. The D source looks like this:

char[num*2] foo(size_t num)(in ubyte[num] bar)
{
char[num*2] result = 'A';
return result;
}

void main()
{
ubyte[29] data = 'A';
auto res = foo(data);
}

This produced the following valgrind message:

Source and destination overlap in memcpy(0x7feffffdc, 0x7feffffdc, 42)
at 0x4C2E170: memcpy@@GLIBC_2.14 (mc_replace_strmem.c:877)
by 0x403266: _D8valgrind12__T3fooVm21Z3fooFNaNbNfxG21hZG42a (valgrind.d:4)
by 0x4031A8: _Dmain (valgrind.d:10)
by 0x415C00: _D2rt6dmain211_d_run_mainUiPPaPUAAaZiZi7runMainMFZv (dmain2.d:616)
by 0x415B8B: _D2rt6dmain211_d_run_mainUiPPaPUAAaZiZi7tryExecMFMDFZvZv (dmain2.d:591)
by 0x415C51: _D2rt6dmain211_d_run_mainUiPPaPUAAaZiZi6runAllMFZv (dmain2.d:626)
by 0x415B8B: _D2rt6dmain211_d_run_mainUiPPaPUAAaZiZi7tryExecMFMDFZvZv (dmain2.d:591)
by 0x415B47: _d_run_main (dmain2.d:635)
by 0x4156F2: main (dmain2.d:387)

With this change the message is gone.