title: Conditional jump or move depends on uninitialised value(s) using realloc
body: <p>I wrote a simple echo-server in c that writes back all clients besides the one that wrote (using realloc) , I get errors in valgrind and I'm not sure what I'm doing wrong. I would like to get some help.
  here is my code:</p>
  
  <pre><code>int read_from_socket(int connfd,char **usr_str);
  
  int main (int argc,char* argv[])
  {
      int sockFD;
      int port, new;
      fd_set active_fd_set, read_fd_set;
      int i;
      struct sockaddr_in serveraddr;
      struct sockaddr_in clientname;
      int size;
      char buf[4096];
     int j;
     bzero(&amp;buf, sizeof(buf));
     char* data;
  
      //Check command line args
      if (argc != 2)
     {
        fprintf(stderr, "usage: %s &lt;port&gt;\n", argv[0]);
        exit(EXIT_FAILURE);
     }
      port = atoi(argv[1]);
  
      // socket: create a socket 
      sockFD = socket(AF_INET, SOCK_STREAM, 0);
      if (sockFD &lt; 0) 
        printf("ERROR opening socket\n");
  
      // build the server's internet address 
      bzero((char *) &amp;serveraddr, sizeof(serveraddr));
      serveraddr.sin_family = AF_INET; // we are using the Internet
      serveraddr.sin_addr.s_addr = htonl(INADDR_ANY); // accept reqs to any IP addr
      serveraddr.sin_port = htons((unsigned short)port); // port to listen on 
  
      // bind: associate the listening socket with a port 
      if (bind(sockFD, (struct sockaddr *) &amp;serveraddr, sizeof(serveraddr)) &lt; 0) 
        printf("ERROR on binding\n");
  
  
      // Create the socket and set it up to accept connections. 
      if (listen (sockFD, 20) &lt; 0)
     {
        printf("ERROR on listening\n");
         exit (EXIT_FAILURE);
     }
  
      // Initialize the set of active sockets. 
      FD_ZERO (&amp;active_fd_set);
      FD_SET (sockFD, &amp;active_fd_set);
  
      while (1)
     {    
          // Block until input arrives on one or more active sockets. 
          read_fd_set = active_fd_set;
  
      if (select (FD_SETSIZE, &amp;read_fd_set, NULL, NULL, NULL) &lt; 0)
        {
          printf("ERROR in selecting");
          exit (EXIT_FAILURE);
        }
  
      // Service all the sockets with input pending. 
  
      if (FD_ISSET (sockFD, &amp;read_fd_set))
        {
          // Connection request on original socket. 
          size = sizeof (clientname);
          new = accept (sockFD,(struct sockaddr *) &amp;clientname, &amp;size);
           if (new &lt; 0)
           {
              printf("ERROR in accepting");
              exit (EXIT_FAILURE);
           }
           fprintf (stderr, "Server: connect from host %s, port %hd.\n", inet_ntoa (clientname.sin_addr), ntohs (clientname.sin_port));
           FD_SET (new, &amp;active_fd_set);
  
  
           continue;
        }
  
  
       data = NULL;
       for (i = 0 ; i&lt;FD_SETSIZE; ++i)
           if (i != sockFD &amp;&amp; FD_ISSET(i, &amp;read_fd_set))
           {
  
              if(read_from_socket(i, &amp;data) == -1)//(line: 99)
              {
                 printf("Closing read\n");
                 close(i);
                 FD_CLR(i, &amp;active_fd_set);   
              }
  
              for (j = 0; j &lt; FD_SETSIZE; ++j)
                 if (j != i &amp;&amp; j != sockFD &amp;&amp; FD_ISSET(j, &amp;active_fd_set))
                    write(j,data,strlen(data));//(line: 108)
           }
  
        free(data);
     }
  
  }
  
  
  int read_from_socket(int connfd,char **usr_str)
  {
     int count = 0, bytesread = 0;
     char *temp;
     char buf[16] = {0};
     while((bytesread = read(connfd,buf,16)) &gt; 0)
     {
        if(bytesread == 0)
           return -1;
        temp = NULL;
        count = count + bytesread;
        temp = realloc(*usr_str, count+1);//(line: 128)
        if(NULL == temp)
        {
           printf("\nMemory Error\n");
           return -1;
        }
        *usr_str = temp;
        memcpy(((*usr_str) + count - bytesread),buf, bytesread);
        if(strstr(*usr_str,"\n") != NULL)//(line: 136) 
        {
           (*usr_str)[count] = '\0';
           break;
        }
  
     }
     return 0;
  }   
  </code></pre>
  
  <p>here is my valgrind errors (i wrote the line number in the code):</p>
  
  <pre><code>==4213== Conditional jump or move depends on uninitialised value(s)
  ==4213==    at 0x4C2D6DD: index (vg_replace_strmem.c:232)
  ==4213==    by 0x401303: read_from_socket (echoServerNew.c:136)
  ==4213==    by 0x4010D0: main (echoServerNew.c:99)
  ==4213==  Uninitialised value was created by a heap allocation
  ==4213==    at 0x4C2D094: malloc (vg_replace_malloc.c:296)
  ==4213==    by 0x4C2D208: realloc (vg_replace_malloc.c:692)
  ==4213==    by 0x401291: read_from_socket (echoServerNew.c:128)
  ==4213==    by 0x4010D0: main (echoServerNew.c:99)
  ==4213== 
  ==4213== Conditional jump or move depends on uninitialised value(s)
  ==4213==    at 0x4C2D6D2: index (vg_replace_strmem.c:232)
  ==4213==    by 0x401303: read_from_socket (echoServerNew.c:136)
  ==4213==    by 0x4010D0: main (echoServerNew.c:99)
  ==4213==  Uninitialised value was created by a heap allocation
  ==4213==    at 0x4C2D094: malloc (vg_replace_malloc.c:296)
  ==4213==    by 0x4C2D208: realloc (vg_replace_malloc.c:692)
  ==4213==    by 0x401291: read_from_socket (echoServerNew.c:128)
  ==4213==    by 0x4010D0: main (echoServerNew.c:99)
  </code></pre>