title: C Valgrind - Source and destination overlap in memcpy
body: <p>I'm new to c programming and I'm writing a simple client server application. I get this message: </p>
  
  <pre><code> Source and destination overlap in memcpy(0x41f0beb, 0x41f0258, 69141077)
      ==9522==    at 0x402D9A9: memcpy (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
      ==9522==    by 0x8049C13: message_to_buffer (message.c:92)
  </code></pre>
  
  <p>Here is the specific code:</p>
  
  <pre><code>case CT_ENTRY://100
  {
      int c=(2+2+4+strlen(msg-&gt;content.entry-&gt;key)-1+4+4+strlen(msg-&gt;content.entry-&gt;value-&gt;data));
      char *offset=malloc(c);
      *msg_buf=offset;
      memcpy(offset,&amp;opcode,2);
      offset+=2;
      memcpy(offset,&amp;ctype,2 );
      offset+=2;
      int ks=strlen(msg-&gt;content.entry-&gt;key);
      int ksc=host_to_net(ks);
      memcpy(offset,&amp;ksc,4);
      offset+=4;
      memcpy(offset, msg-&gt;content.entry-&gt;key, ks);
      offset+=ks;
      int l=host_to_net(get_time());
      memcpy(offset,&amp;l,4);
      offset+=4;
      int ds=host_to_net(msg-&gt;content.entry-&gt;value-&gt;datasize);
      memcpy(offset,&amp;ds,4);
      offset+=4;
  
      // this line here!
      memcpy(offset,msg-&gt;content.entry-&gt;value-&gt;data, msg-&gt;content.value-&gt;datasize);
  
      return c;
      break;
  </code></pre>
  
  <p>The offending line is </p>
  
  <pre><code>memcpy(offset,msg-&gt;content.entry-&gt;value-&gt;data, msg-&gt;content.value-&gt;datasize);
  </code></pre>
  
  <p>Can anyone please explain why this is happening? Thanks</p>