title: c++ linux send uninitialised sockets memory
body: <p>I'm new and I'm using debian 10 x64. Why is error in the line with SEND? Someone can sugest me how to fix that problem?</p>
  
  <p><strong>send(socket, (char*)m_MsgBuf+sendBytes, std::min(m_MsgSize-sendBytes+2, 1000),</strong></p>
  
  <p>I got that ERROR LOG:</p>
  
  <pre><code>==24819== Thread 3:
  ==24819== Syscall param socketcall.sendto(msg) points to uninitialised byte(s)
  ==24819==    at 0x51442B4: __libc_send (send.c:28)
  ==24819==    by 0x51442B4: send (send.c:23)
  ==24819==    by 0x1FCFC9: NetworkMessage::WriteToSocket(int) (networkmessage.cpp:117)
  ==24819==    by 0x231624: Protocol76::flushOutputBuffer() (protocol76.cpp:3574)
  ==24819==    by 0x22ECF4: Protocol76::sendThingAppear(Thing const*) (protocol76.cpp:3039)
  ==24819==    by 0x214DE1: Player::onThingAppear(Thing const*) (player.cpp:2165)
  ==24819==    by 0x1829D0: Game::sendAddThing(Player*, Position const&amp;, Thing const*) (game.cpp:7005)
  ==24819==    by 0x16789F: Game::placeCreature(Position&amp;, Creature*, int*) (game.cpp:1260)
  ==24819==    by 0x223995: Protocol76::ConnectPlayer(int*) (protocol76.cpp:66)
  ==24819==    by 0x208C43: ConnectionHandler(void*) (otserv.cpp:575)
  ==24819==    by 0x5139F26: start_thread (pthread_create.c:479)
  ==24819==    by 0x55F12AE: clone (clone.S:95)
  ==24819==  Address 0x111137f1 is 289 bytes inside a block of size 16,912 alloc'd
  ==24819==    at 0x4836DEF: operator new(unsigned long) (vg_replace_malloc.c:344)
  ==24819==    by 0x2086ED: ConnectionHandler(void*) (otserv.cpp:510)
  ==24819==    by 0x5139F26: start_thread (pthread_create.c:479)
  ==24819==    by 0x55F12AE: clone (clone.S:95)
  ==24819== 
  </code></pre>
  
  <p>CODE:</p>
  
  <pre><code>bool NetworkMessage::WriteToSocket(SOCKET socket)
  {
      if (m_MsgSize == 0)
          return true;
  
      m_MsgBuf[0] = (unsigned char)(m_MsgSize);
      m_MsgBuf[1] = (unsigned char)(m_MsgSize &gt;&gt; 8);
  
      bool ret = true;
      int32_t sendBytes = 0;
      int32_t flags = 0;
      int32_t retry = 0;
  
      flags = MSG_DONTWAIT; // 2 ?
  
      do
      {
          int32_t b = send(socket, (char*)m_MsgBuf+sendBytes, std::min(m_MsgSize-sendBytes+2, 1000), flags);
  
          if(b &lt;= 0)
          {   
              int32_t errnum;
  
              if(errnum == EWOULDBLOCK) 
              {
                  b = 0;
                  OTSYS_SLEEP(10);
                  retry++;
  
                  if(retry == 10) 
                  {
                      ret = false;
                      break;
                  }
              }
              else
              {
                  ret = false;
                  break;
              }
          }
          sendBytes += b;
      }while(sendBytes &lt; m_MsgSize+2);
  
      return ret;
  }
  
  
  ==930== Thread 3:
  ==930== Conditional jump or move depends on uninitialised value(s)
  ==930==    at 0x1FCFE7: NetworkMessage::WriteToSocket(int) (networkmessage.cpp:106)
  ==930==    by 0x208340: ConnectionHandler(void*) (otserv.cpp:427)
  ==930==    by 0x5139F26: start_thread (pthread_create.c:479)
  ==930==    by 0x55F12AE: clone (clone.S:95)
  ==930==
  ==930== Syscall param socketcall.sendto(msg) points to uninitialised byte(s)
  ==930==    at 0x51442B4: __libc_send (send.c:28)
  ==930==    by 0x51442B4: send (send.c:23)
  ==930==    by 0x1FCFD9: NetworkMessage::WriteToSocket(int) (networkmessage.cpp:100)
  ==930==    by 0x231650: Protocol76::flushOutputBuffer() (protocol76.cpp:3574)
  ==930==    by 0x22ED20: Protocol76::sendThingAppear(Thing const*) (protocol76.cpp:3039)
  ==930==    by 0x214E0D: Player::onThingAppear(Thing const*) (player.cpp:2165)
  ==930==    by 0x1829D0: Game::sendAddThing(Player*, Position const&amp;, Thing const*) (game.cpp:7005)
  ==930==    by 0x16789F: Game::placeCreature(Position&amp;, Creature*, int*) (game.cpp:1260)
  ==930==    by 0x2239C1: Protocol76::ConnectPlayer(int*) (protocol76.cpp:66)
  ==930==    by 0x208C6F: ConnectionHandler(void*) (otserv.cpp:575)
  ==930==    by 0x5139F26: start_thread (pthread_create.c:479)
  ==930==    by 0x55F12AE: clone (clone.S:95)
  ==930==  Address 0x111198d1 is 289 bytes inside a block of size 16,912 alloc'd
  ==930==    at 0x4836DEF: operator new(unsigned long) (vg_replace_malloc.c:344)
  ==930==    by 0x208719: ConnectionHandler(void*) (otserv.cpp:510)
  ==930==    by 0x5139F26: start_thread (pthread_create.c:479)
  ==930==    by 0x55F12AE: clone (clone.S:95)
  ==930==
  ==930== Conditional jump or move depends on uninitialised value(s)
  ==930==    at 0x1FCFE7: NetworkMessage::WriteToSocket(int) (networkmessage.cpp:106)
  ==930==    by 0x231650: Protocol76::flushOutputBuffer() (protocol76.cpp:3574)
  ==930==    by 0x22ED20: Protocol76::sendThingAppear(Thing const*) (protocol76.cpp:3039)
  ==930==    by 0x214E0D: Player::onThingAppear(Thing const*) (player.cpp:2165)
  ==930==    by 0x1829D0: Game::sendAddThing(Player*, Position const&amp;, Thing const*) (game.cpp:7005)
  ==930==    by 0x16789F: Game::placeCreature(Position&amp;, Creature*, int*) (game.cpp:1260)
  ==930==    by 0x2239C1: Protocol76::ConnectPlayer(int*) (protocol76.cpp:66)
  ==930==    by 0x208C6F: ConnectionHandler(void*) (otserv.cpp:575)
  ==930==    by 0x5139F26: start_thread (pthread_create.c:479)
  ==930==    by 0x55F12AE: clone (clone.S:95)
  ==930== Thread 2:
  ==930== Conditional jump or move depends on uninitialised value(s)
  ==930==    at 0x1FCFE7: NetworkMessage::WriteToSocket(int) (networkmessage.cpp:106)
  ==930==    by 0x231650: Protocol76::flushOutputBuffer() (protocol76.cpp:3574)
  ==930==    by 0x214089: Player::flushMsg() (player.cpp:1867)
  ==930==    by 0x182338: Game::flushSendBuffers() (game.cpp:6879)
  ==930==    by 0x17AAB2: Game::checkCreature(unsigned int) (game.cpp:5461)
  ==930==    by 0x1A4B67: std::mem_fun1_t&lt;void, Game, unsigned int&gt;::operator()(Game*, unsigned int) const (stl_function.h:1284)
  ==930==    by 0x1A1668: std::binder2nd&lt;std::mem_fun1_t&lt;void, Game, unsigned int&gt; &gt;::operator()(Game* const&amp;)const (binders.h:158)
  ==930==    by 0x19BCCD: boost::detail::function::void_function_obj_invoker1&lt;std::binder2nd&lt;std::mem_fun1_t&lt;void, Game, unsigned int&gt;&gt;,void, Game*&gt;::invoke(boost::detail::function::function_buffer&amp;, Game*) (function_template.hpp:159)
  ==930==    by 0x23810D: boost::function1&lt;void, Game*&gt;::operator()(Game*) const (function_template.hpp:768)
  ==930==    by 0x237FBC: TSchedulerTask::operator()(Game*) (scheduler.h:63)
  ==930==    by 0x166D2B: Game::eventThread(void*) (game.cpp:1045)
  ==930==    by 0x5139F26: start_thread (pthread_create.c:479)
  ==930==
  ==930== Thread 3:
  ==930== Syscall param socketcall.sendto(msg) points to uninitialised byte(s)
  ==930==    at 0x51442B4: __libc_send (send.c:28)
  ==930==    by 0x51442B4: send (send.c:23)
  ==930==    by 0x1FCFD9: NetworkMessage::WriteToSocket(int) (networkmessage.cpp:100)
  ==930==    by 0x231650: Protocol76::flushOutputBuffer() (protocol76.cpp:3574)
  ==930==    by 0x214089: Player::flushMsg() (player.cpp:1867)
  ==930==    by 0x182338: Game::flushSendBuffers() (game.cpp:6879)
  ==930==    by 0x224573: Protocol76::parsePacket(NetworkMessage&amp;) (protocol76.cpp:405)
  ==930==    by 0x223A3F: Protocol76::ReceiveLoop() (protocol76.cpp:86)
  ==930==    by 0x208E70: ConnectionHandler(void*) (otserv.cpp:611)
  ==930==    by 0x5139F26: start_thread (pthread_create.c:479)
  ==930==    by 0x55F12AE: clone (clone.S:95)
  ==930==  Address 0x11119f5f is 1,967 bytes inside a block of size 16,912 alloc'd
  ==930==    at 0x4836DEF: operator new(unsigned long) (vg_replace_malloc.c:344)
  ==930==    by 0x208719: ConnectionHandler(void*) (otserv.cpp:510)
  ==930==    by 0x5139F26: start_thread (pthread_create.c:479)
  ==930==    by 0x55F12AE: clone (clone.S:95)
  ==930==
  </code></pre>
  
  <p>I use code like this:</p>
  
  <pre><code>bool NetworkMessage::WriteToSocket(SOCKET socket)
  {
      if (m_MsgSize == 0)
          return true;
  
      m_MsgBuf[0] = (unsigned char)(m_MsgSize);
      m_MsgBuf[1] = (unsigned char)(m_MsgSize &gt;&gt; 8);
  
      bool ret = true;
      int32_t sendBytes = 0;
      int32_t flags = 0;
      int32_t retry = 0;
  
      flags = MSG_DONTWAIT; // 2 ?
  
  
      while (sendBytes != m_MsgSize) 
      { /* != rather than &lt; */
          int32_t b = send(socket, (char*)m_MsgBuf+sendBytes, std::min(m_MsgSize-sendBytes+2, 1000), flags);
  
          if(b &lt;= 0)
          {   
              int32_t errnum;
  
              if(errnum == EWOULDBLOCK) 
              {
                  b = 0;
                  OTSYS_SLEEP(10);
                  retry++;
  
                  if(retry == 10) 
                  {
                      ret = false;
                      break;
                  }
              }
              else
              {
                  ret = false;
                  break;
              }
          }
          sendBytes += b;
      }
  
      return ret;
  }
  </code></pre>
  
  <blockquote>
    <p><strong>EDIT</strong></p>
  </blockquote>
  
  <p>the last time when i had similar error I needed to use that code and now everything is working: <code>sigemptyset(&amp;sigh.sa_mask);</code> OR <code>memset(&amp;sigh, 0, sizeof(sigh));</code> here i found all msgBuf and msg Size in my project files. Do U see something intresting here? <strong>m_MsgBuf</strong> <a href="http://wklejto.pl/828244" rel="nofollow noreferrer">wklejto.pl/828244</a> <strong>m_MsgSize</strong> <a href="http://wklejto.pl/828245" rel="nofollow noreferrer">wklejto.pl/828245</a></p>
  
  <blockquote>
    <p><strong>EDIT</strong></p>
  </blockquote>
  
  <p>so I did this, in file <strong><em>NetworkMessage.cpp</em></strong> I have added under AddString/AddU32/AddU16/AddByte this:</p>
  
  <pre><code>#ifdef CHECKVAL
      checkval(m_MsgBuf+m_ReadPos, stringlen);
  #endif  
  </code></pre>
  
  <p>on the top this file I have added:</p>
  
  <pre><code>#include "player.h"
  
  #ifdef CHECKVAL
      extern int checkval(unsigned char * buff, int len);
  #endif
  </code></pre>
  
  <p>and to the end added this code to <strong><em>player.cpp</em></strong></p>
  
  <pre><code>#ifdef CHECKVAL
  int checkval(unsigned char * buff, int len)
  {
     int r = 0;
  
     buff -= len;
     while (len--)
       r += *buff++;
     return r;
  }
  #endif
  </code></pre>
  
  <p>and also I have changed <code>char</code> to <code>unsigned char</code> - bcs I got error <code>invalid conversion from ‘unsigned char*’ to ‘char*’</code></p>
  
  <p>to END I added to compilator this <code>-DCHECKVAL</code></p>
  
  <p>after that I see same error</p>
  
  <pre><code>==15290== Thread 3:
  ==15290== Syscall param socketcall.sendto(msg) points to uninitialised byte(s)
  ==15290==    at 0x51442B4: __libc_send (send.c:28)
  ==15290==    by 0x51442B4: send (send.c:23)
  ==15290==    by 0x1FCFDC: NetworkMessage::WriteToSocket(int) (networkmessage.cpp:105)
  ==15290==    by 0x231728: Protocol76::flushOutputBuffer() (protocol76.cpp:3574)
  ==15290==    by 0x22EDF8: Protocol76::sendThingAppear(Thing const*) (protocol76. cpp:3039)
  ==15290==    by 0x214E99: Player::onThingAppear(Thing const*) (player.cpp:2165)
  ==15290==    by 0x1829D0: Game::sendAddThing(Player*, Position const&amp;, Thing const*) (game.cpp:7005)
  ==15290==    by 0x16789F: Game::placeCreature(Position&amp;, Creature*, int*) (game.cpp:1260)
  ==15290==    by 0x223A99: Protocol76::ConnectPlayer(int*) (protocol76.cpp:66)
  ==15290==    by 0x208CFB: ConnectionHandler(void*) (otserv.cpp:575)
  ==15290==    by 0x5139F26: start_thread (pthread_create.c:479)
  ==15290==    by 0x55F12AE: clone (clone.S:95)
  ==15290==  Address 0x11116331 is 289 bytes inside a block of size 16,912 alloc'd
  ==15290==    at 0x4836DEF: operator new(unsigned long) (vg_replace_malloc.c:344)
  ==15290==    by 0x2087A5: ConnectionHandler(void*) (otserv.cpp:510)
  ==15290==    by 0x5139F26: start_thread (pthread_create.c:479)
  ==15290==    by 0x55F12AE: clone (clone.S:95)
  ==15290==
  ==15290== Syscall param socketcall.sendto(msg) points to uninitialised byte(s)
  ==15290==    at 0x51442B4: __libc_send (send.c:28)
  ==15290==    by 0x51442B4: send (send.c:23)
  ==15290==    by 0x1FCFDC: NetworkMessage::WriteToSocket(int) (networkmessage.cpp:105)
  ==15290==    by 0x231728: Protocol76::flushOutputBuffer() (protocol76.cpp:3574)
  ==15290==    by 0x214115: Player::flushMsg() (player.cpp:1867)
  ==15290==    by 0x182338: Game::flushSendBuffers() (game.cpp:6879)
  ==15290==    by 0x22464B: Protocol76::parsePacket(NetworkMessage&amp;) (protocol76.cpp:405)
  ==15290==    by 0x223B17: Protocol76::ReceiveLoop() (protocol76.cpp:86)
  ==15290==    by 0x208EFC: ConnectionHandler(void*) (otserv.cpp:611)
  ==15290==    by 0x5139F26: start_thread (pthread_create.c:479)
  ==15290==    by 0x55F12AE: clone (clone.S:95)
  ==15290==  Address 0x111169bf is 1,967 bytes inside a block of size 16,912 alloc'd
  ==15290==    at 0x4836DEF: operator new(unsigned long) (vg_replace_malloc.c:344)
  ==15290==    by 0x2087A5: ConnectionHandler(void*) (otserv.cpp:510)
  ==15290==    by 0x5139F26: start_thread (pthread_create.c:479)
  ==15290==    by 0x55F12AE: clone (clone.S:95)
  ==15290==
  </code></pre>
  
  <p>hmm.. but this function looks intresting <code>ConnectionHandler</code> in the line 510 is problem?</p>
  
  <pre><code>            Protocol76* protocol;
              protocol = new Protocol76(s);
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>Protocol76::Protocol76(SOCKET s)
  {
      OTSYS_THREAD_LOCKVARINIT(bufferLock);
  
      player = NULL;
      pendingLogout = false;
  
      windowTextID = 0;
      readItem = NULL;
      this-&gt;s = s;
  }
  </code></pre>
  
  <p>full <code>ConnectionHandler</code> and <em>password</em> is <strong>123</strong></p>
  
  <p><a href="http://www.wklejto.pl/828332" rel="nofollow noreferrer">enter link description here</a></p>
  
  <p>line <code>510</code> in the file is <code>152</code>
  how do u think?</p>
  
  <p>something like this can be enough?</p>
  
  <pre><code>int checkval(unsigned char * buff, int len)
  {
      buff -= len;
  
      ofstream bufile ("buff.txt");
  
      if (bufile.is_open())
      {
          while (len--)
          {
              bufile &lt;&lt; "buff: " &lt;&lt; *buff &lt;&lt; "len: " &lt;&lt; len;
          }
          bufile.close();
      }
  }
  </code></pre>