title: Segmentation fault on malloc function
body: <p>I have a script that connects on one of my 10 servers trough <strong>SSH</strong> (<code>libssh2</code>). The program uses <code>fork</code> as a threading system. It is entering the server, executes a command (like <code>free -m</code> or check the CPU USSAGE), and sends the output trough a <strong>HTTP REQUEST</strong> to another server.</p>
  
  <p>It works just fine, but from time to time it is not sending data back to the HTTP SERVER. I done a little debug with <code>puts('DEBUG123 ...')</code> to see where it is crashing. I do this because i use <code>FORK</code> and i am unable to see where it is crasing. Down there you see the code.</p>
  
  <pre><code>... code from the fork
  puts("DEBUG 6");
  sprintf(param,"%s%s",param,my_encode(info));
  puts("DEBUG 6.5");
  getContent(MY_HOST,HOST_IP,param);
  puts("DEBUG 7");
  ... code from the fork
  </code></pre>
  
  <p>code of the my_encode functions that encrypts my data before sending it to the <strong>HTTP SERVER</strong> :</p>
  
  <pre><code>char *my_encode(char* bytes_to_encode)
  {
      puts("S-DEBUG 1");
      int in_len = strlen(bytes_to_encode);
      puts("S-DEBUG 1.1");
      char buf[5200];
      char *res = (char *)malloc(3600);
      puts("S-DEBUG 1.2");
      memset(buf,0,1200);
      puts("S-DEBUG 1.3");
      memset(res,0,3600);
      puts("S-DEBUG 2");
  
      strcpy(buf,base64_encode(bytes_to_encode,in_len));
  
      for(int i=0;i&lt;strlen(buf);i++)
      {
          strcat(res,switch_encode(buf[i]));
      }
      puts("S-DEBUG 3");
      return res;
  
  }
  </code></pre>
  
  <p>This is the output that i am getting :</p>
  
  <pre><code>Linux -&gt; username password 1.2.3.4 22
  DEBUG 2
  DEBUG 3
  DEBUG 4
  DEBUG 5
  DEBUG 6
  S-DEBUG 1
  S-DEBUG 1.1
  S-DEBUG 1.2
  S-DEBUG 1.3
  S-DEBUG 2
  S-DEBUG 3
  DEBUG 6.5
  DEBUG 7
  DEBUG 1
  -- SUCCESSFUL SEND
  
  Linux -&gt; username password 1.2.3.4 22
  DEBUG 2
  DEBUG 3
  DEBUG 4
  DEBUG 5
  DEBUG 6
  S-DEBUG 1
  S-DEBUG 1.1
  S-DEBUG 1.2
  S-DEBUG 1.3
  S-DEBUG 2
  S-DEBUG 3
  DEBUG 6.5
  DEBUG 7
  DEBUG 1
  -- SUCCESSFUL SEND
  
  Linux -&gt; username password 1.2.3.4 22
  DEBUG 2
  DEBUG 3
  DEBUG 4
  DEBUG 5
  DEBUG 6
  S-DEBUG 1
  S-DEBUG 1.1
  S-DEBUG 1.2
  S-DEBUG 1.3
  S-DEBUG 2
  S-DEBUG 3
  DEBUG 6.5
  DEBUG 7
  DEBUG 1
  -- SUCCESSFUL SEND
  
  Linux -&gt; username password 1.2.3.4 22
  DEBUG 2
  DEBUG 3
  DEBUG 4
  DEBUG 5
  DEBUG 6
  S-DEBUG 1
  S-DEBUG 1.1 &lt;--- HERE IT IS CRASHING
  DEBUG 1
  -- ERROR ON SEND
  
  Linux -&gt; username password 1.2.3.4 22
  DEBUG 2
  DEBUG 3
  DEBUG 4
  DEBUG 5
  DEBUG 6
  S-DEBUG 1
  S-DEBUG 1.1
  S-DEBUG 1.2
  S-DEBUG 1.3
  S-DEBUG 2
  S-DEBUG 3
  DEBUG 6.5
  DEBUG 7
  -- SUCCESSFUL SEND
  
  Linux -&gt; username password 1.2.3.4 22
  DEBUG 2
  DEBUG 3
  DEBUG 4
  DEBUG 5
  DEBUG 6
  S-DEBUG 1
  S-DEBUG 1.1 &lt;--- HERE IT IS CRASHING
  DEBUG 1
  -- ERROR ON SEND
  </code></pre>
  
  <p>So for my understanding, it is crashing at the <code>malloc</code> funtion here :</p>
  
  <pre><code>char *res = (char *)malloc(3600);
  </code></pre>
  
  <p>And this is a basic message that i send :</p>
  
  <pre><code>username:password:1.2.3.4:22:Linux:Intel(R) Core(TM)2 Duo CPU E8200 @ 2.77GHz:242 238 3 0 15 64:no_server_load]
  </code></pre>
  
  <p>Sometimes it is crashing on it.</p>
  
  <p>Also i have changed the line <code>char *res = (char *)malloc(3600);</code> , originally it was <code>char *res = (char *)malloc(1200);</code>. I believed that i don't have enough memory space.</p>
  
  <ol>
  <li>Where is the problem?</li>
  <li>Do i need to rewrite the <code>my_encode</code> function so it will no longer crash? If so, how??</li>
  </ol>
  
  <p>Thanks.</p>