title: Valgrind strange "Syscall param socketcall.sendto(msg) points to uninitialised byte(s)"
body: <p>Maybe I am completely blind or stupid but I do not understand how I am getting a valgrind problem here. Valgrind is giving me this:</p>
  
  <pre><code>==3728== Syscall param socketcall.sendto(msg) points to uninitialised byte(s)
  ==3728==    at 0x57835DC: send (send.c:33)
  ==3728==    by 0x4058B7: CBSocketSend (CBLibEventSockets.c:287)
  ==3728==    by 0x4E522FB: CBNetworkCommunicatorOnCanSend (CBNetworkCommunicator.c:649)
  ==3728==    by 0x40564F: CBCanSend (CBLibEventSockets.c:235)
  ==3728==    by 0x526E54B: event_base_loop (event.c:1346)
  ==3728==    by 0x405244: CBStartEventLoop (CBLibEventSockets.c:154)
  ==3728==    by 0x5A54E99: start_thread (pthread_create.c:308)
  ==3728==  Address 0x5e7b6b4 is 20 bytes inside a block of size 24 alloc'd
  ==3728==    at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==3728==    by 0x4E51E2A: CBNetworkCommunicatorOnCanSend (CBNetworkCommunicator.c:592)
  ==3728==    by 0x40564F: CBCanSend (CBLibEventSockets.c:235)
  ==3728==    by 0x526E54B: event_base_loop (event.c:1346)
  ==3728==    by 0x405244: CBStartEventLoop (CBLibEventSockets.c:154)
  ==3728==    by 0x5A54E99: start_thread (pthread_create.c:308)
  ==3728==
  ==3728== (action on error) vgdb me ...
  </code></pre>
  
  <p>When this happens GDB gives the backtrace:</p>
  
  <pre><code>0x00000000057835dc in __libc_send (fd=&lt;optimized out&gt;, buf=&lt;optimized out&gt;,
      n=&lt;optimized out&gt;, flags=&lt;optimized out&gt;) at ../sysdeps/unix/sysv/linux/x86_64/send.c:33
  33  ../sysdeps/unix/sysv/linux/x86_64/send.c: No such file or directory.
  (gdb) bt
  #0  0x00000000057835dc in __libc_send (fd=&lt;optimized out&gt;, buf=&lt;optimized out&gt;,
      n=&lt;optimized out&gt;, flags=&lt;optimized out&gt;) at ../sysdeps/unix/sysv/linux/x86_64/send.c:33
  #1  0x00000000004058b8 in CBSocketSend (socketID=20,
      data=0x5e7b6a0 "\371\276\264\331version", len=24)
      at /media/sf_BitEagle_Projects/cbitcoin/dependencies/sockets/CBLibEventSockets.c:287
  #2  0x0000000004e522fc in CBNetworkCommunicatorOnCanSend (vself=0x5e76c30, vpeer=0x5e791c0)
      at /media/sf_BitEagle_Projects/cbitcoin/src/structures/CBObject/CBNetworkCommunicator/CBNetworkCommunicator.c:649
  #3  0x0000000000405650 in CBCanSend (socketID=20, eventNum=4, arg=0x5e7a2d0)
      at /media/sf_BitEagle_Projects/cbitcoin/dependencies/sockets/CBLibEventSockets.c:235
  #4  0x000000000526e54c in event_process_active_single_queue (activeq=0x5e78be0,
      base=0x5e78710) at event.c:1346
  #5  event_process_active (base=&lt;optimized out&gt;) at event.c:1416
  #6  event_base_loop (base=0x5e78710, flags=0) at event.c:1617
  #7  0x0000000000405245 in CBStartEventLoop (vloop=0x5e78eb0)
      at /media/sf_BitEagle_Projects/cbitcoin/dependencies/sockets/CBLibEventSockets.c:154
  #8  0x0000000005a54e9a in start_thread (arg=0x7a74700) at pthread_create.c:308
  #9  0x00000000057824bd in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:112
  #10 0x0000000000000000 in ?? ()
  </code></pre>
  
  <p>So it can be seen here that the code is sending some allocated data with 24 bytes to send() which is what happens in CBSocketSend and then valgrind complains at sendto at 20 bytes into the data.</p>
  
  <p>I fill in the data with this code:</p>
  
  <pre><code>peer-&gt;sendingHeader = malloc(24);
  if (NOT peer-&gt;sendingHeader) {
      CBNetworkCommunicatorDisconnect(self, peer, 0, false);
      return;
  }
  peer-&gt;sendingHeader[0] = self-&gt;networkID;
  peer-&gt;sendingHeader[1] = self-&gt;networkID &gt;&gt; 8;
  peer-&gt;sendingHeader[2] = self-&gt;networkID &gt;&gt; 16;
  peer-&gt;sendingHeader[3] = self-&gt;networkID &gt;&gt; 24;
  // Get the message we are sending.
  CBMessage * toSend = peer-&gt;sendQueue[peer-&gt;sendQueueFront];
  // Message type text
  if (toSend-&gt;type == CB_MESSAGE_TYPE_VERSION)
      memcpy(peer-&gt;sendingHeader + 4, "version\0\0\0\0\0", 12);
  else if (toSend-&gt;type == CB_MESSAGE_TYPE_VERACK)
      memcpy(peer-&gt;sendingHeader + 4, "verack\0\0\0\0\0\0", 12);
  else if (toSend-&gt;type == CB_MESSAGE_TYPE_ADDR)
      memcpy(peer-&gt;sendingHeader + 4, "addr\0\0\0\0\0\0\0\0", 12);
  else if (toSend-&gt;type == CB_MESSAGE_TYPE_INV)
      memcpy(peer-&gt;sendingHeader + 4, "inv\0\0\0\0\0\0\0\0\0", 12);
  else if (toSend-&gt;type == CB_MESSAGE_TYPE_GETDATA)
      memcpy(peer-&gt;sendingHeader + 4, "getdata\0\0\0\0\0", 12);
  else if (toSend-&gt;type == CB_MESSAGE_TYPE_GETBLOCKS)
      memcpy(peer-&gt;sendingHeader + 4, "getblocks\0\0\0", 12);
  else if (toSend-&gt;type == CB_MESSAGE_TYPE_GETHEADERS)
      memcpy(peer-&gt;sendingHeader + 4, "getheaders\0\0", 12);
  else if (toSend-&gt;type == CB_MESSAGE_TYPE_TX)
      memcpy(peer-&gt;sendingHeader + 4, "tx\0\0\0\0\0\0\0\0\0\0", 12);
  else if (toSend-&gt;type == CB_MESSAGE_TYPE_BLOCK)
      memcpy(peer-&gt;sendingHeader + 4, "block\0\0\0\0\0\0\0", 12);
  else if (toSend-&gt;type == CB_MESSAGE_TYPE_HEADERS)
      memcpy(peer-&gt;sendingHeader + 4, "headers\0\0\0\0\0", 12);
  else if (toSend-&gt;type == CB_MESSAGE_TYPE_GETADDR)
      memcpy(peer-&gt;sendingHeader + 4, "getaddr\0\0\0\0\0", 12);
  else if (toSend-&gt;type == CB_MESSAGE_TYPE_PING)
      memcpy(peer-&gt;sendingHeader + 4, "ping\0\0\0\0\0\0\0\0", 12);
  else if (toSend-&gt;type == CB_MESSAGE_TYPE_PONG)
      memcpy(peer-&gt;sendingHeader + 4, "pong\0\0\0\0\0\0\0\0", 12);
  else if (toSend-&gt;type == CB_MESSAGE_TYPE_ALERT)
      memcpy(peer-&gt;sendingHeader + 4, "alert\0\0\0\0\0\0\0", 12);
  else if (toSend-&gt;type == CB_MESSAGE_TYPE_ALT)
      memcpy(peer-&gt;sendingHeader + 4, toSend-&gt;altText, 12);
  // Length
  if (toSend-&gt;bytes){
      peer-&gt;sendingHeader[16] = toSend-&gt;bytes-&gt;length;
      peer-&gt;sendingHeader[17] = toSend-&gt;bytes-&gt;length &lt;&lt; 8;
      peer-&gt;sendingHeader[18] = toSend-&gt;bytes-&gt;length &lt;&lt; 16;
      peer-&gt;sendingHeader[19] = toSend-&gt;bytes-&gt;length &lt;&lt; 24;
  }else{
      memset(peer-&gt;sendingHeader + 16, 0, 4);
  }
  // Checksum
  peer-&gt;sendingHeader[20] = toSend-&gt;checksum[0];
  peer-&gt;sendingHeader[21] = toSend-&gt;checksum[1];
  peer-&gt;sendingHeader[22] = toSend-&gt;checksum[2];
  peer-&gt;sendingHeader[23] = toSend-&gt;checksum[3];
  </code></pre>
  
  <p>You can see quite clearly there that all of the data is assigned, right? So what is valgrind doing?</p>
  
  <p>You can see the full function apparently causing troubles here: <a href="https://github.com/MatthewLM/cbitcoin/blob/master/src/structures/CBObject/CBNetworkCommunicator/CBNetworkCommunicator.c#L582" rel="noreferrer">https://github.com/MatthewLM/cbitcoin/blob/master/src/structures/CBObject/CBNetworkCommunicator/CBNetworkCommunicator.c#L582</a></p>
  
  <p>CBSocketSend is this:</p>
  
  <pre><code>int32_t CBSocketSend(uint64_t socketID,uint8_t * data,uint32_t len){
      ssize_t res = send((evutil_socket_t)socketID, data, len, CB_SEND_FLAGS);
      if (res &gt;= 0)
          return (int32_t)res;
      if (errno == EAGAIN)
          return 0; // False event. Wait again.
      return CB_SOCKET_FAILURE; // Failure
  }
  </code></pre>
  
  <p>Which is here: <a href="https://github.com/MatthewLM/cbitcoin/blob/master/dependencies/sockets/CBLibEventSockets.c#L286" rel="noreferrer">https://github.com/MatthewLM/cbitcoin/blob/master/dependencies/sockets/CBLibEventSockets.c#L286</a></p>
  
  <p>Thank you.</p>