title: IRC server segfault when clients, send more than 10 messages
body: <p>When client send more than 10 messages,the server segfault but the server havn't this problem. he can send not restricted numbers of messages.</p>
  
  <p>it's probably caused by my char *buffer malloc() and badly free() maybe ???
  i have try to use static array buffer without free() evidently but it's worst.</p>
  
  <p>Client</p>
  
  <pre><code>#include "header.h"
  
  void    app(SOCKET socket)
  {
      char pseudo[24];
      int statu;
      fd_set readfs;
  
      strcpy(pseudo, "[");
      strcat(pseudo, getname(sizeof(pseudo)));
      strcat(pseudo, "] ");
  
      while (1)
      {
          char *buffer = malloc(sizeof(char) * 1024);
          char *msg = malloc(sizeof(char) * 1000);
  
          FD_ZERO(&amp;readfs);
          FD_SET(socket, &amp;readfs);
          FD_SET(STDIN_FILENO, &amp;readfs);
  
          if (select(socket + 1, &amp;readfs, NULL, NULL, NULL) == -1)
          {
              perror("select()");
              exit(errno);
          }
          if (FD_ISSET(STDIN_FILENO, &amp;readfs))
          {
              fgets(msg, sizeof(char) * 1000, stdin);
              strcpy(buffer, pseudo);
              strcat(buffer, msg);
              send_message(socket, buffer);
              cleanMsg(buffer, msg);
          }
          else if (FD_ISSET(socket, &amp;readfs))
          {
              statu = receive_message(socket, buffer);
              if (statu == 0)
              {
                  printf("Server disconnected !\n");
                  break;
              }
              else
              {
                  printf("%s\n", buffer);
                  cleanMsg(buffer, msg);
              }
          }
      }
      close_connection(socket);
  }
  
  void    send_message(SOCKET socket, char *buffer)
  {   
      int i;
  
      for (i = 0; buffer[i] != '\n'; i++) ;
      buffer[i] = '\0';
  
      if (send(socket, buffer, strlen(buffer), 0) &lt; 0)
      {
          perror("send()");
          exit(errno);
      }
  }
  
  int receive_message(SOCKET socket, char *buffer)
  {
      int statu = 0;
      if ((statu = recv(socket, buffer, 1024, 0)) &lt; 0)
          perror("recv()");
      return statu;
  }
  
  void    cleanMsg(char *buffer, char *msg)
  {
      memset(buffer, 0, strlen(buffer));
      memset(msg, 0, strlen(msg));
      free(buffer);
      free(msg);
  }
  
  char    *getname(size_t namesize)
  {
      char *tmp = NULL;
  
      tmp = malloc(namesize);
      getlogin_r(tmp, namesize);
  
      return tmp;
  }
  </code></pre>
  
  <p>Server:</p>
  
  <pre><code>#include "header.h"
  
  int app(SOCKET master_socket, serv_config *s_conf)
  {
      SOCKADDR_IN client_address = {0};
      SOCKET new_socket;
      int address_size = sizeof(client_address);
      int *clients_socket = NULL;
      int statu = 0;
      int fdmax;
      int sd;
      int i;
  
      clients_socket = malloc(sizeof(int) * s_conf-&gt;max_client);
  
      fd_set readfs;
  
      for (i = 0; i &lt; s_conf-&gt;max_client; i++)
          clients_socket[i] = 0;
  
      while (1)
      {
  
          char *buffer = malloc(sizeof(char) * 1024);
          char *msg = malloc(sizeof(char) * 1000);
  
          FD_ZERO(&amp;readfs);
          FD_SET(STDIN_FILENO, &amp;readfs);
          FD_SET(master_socket, &amp;readfs);
  
          fdmax = master_socket;
  
          for (i = 0; i &lt; s_conf-&gt;max_client; i++)
          {
              sd = clients_socket[i];
              if (sd &gt; 0)
                  FD_SET(sd, &amp;readfs);
              if (sd &gt; fdmax)
                  fdmax = sd;
          }
  
          if (select(fdmax + 1, &amp;readfs, NULL, NULL, NULL) == -1)
          {
              perror("select()");
              exit(errno);
          }
  
          if (FD_ISSET(STDIN_FILENO, &amp;readfs))
          {
              fgets(msg, sizeof(char) * 1000, stdin);
              strcpy(buffer, "[Server] ");
              strcat(buffer, msg);
              send_toall(clients_socket, 0, s_conf-&gt;max_client, buffer);
              cleanMsg(buffer, msg);
          }
          else if (FD_ISSET(master_socket, &amp;readfs))
          {
              new_socket = accept(master_socket, (SOCKADDR *)&amp;client_address, &amp;address_size);
  
              if (new_socket == INVALID_SOCKET)
              {
                  perror("accept()");
                  closesocket(new_socket);
                  exit(errno);
              }
  
              for (i = 0; i &lt; s_conf-&gt;max_client; i++)
              {
                  if (clients_socket[s_conf-&gt;max_client - 1] != 0)
                  {
                      strcpy(buffer, "Connection error: no more client can be connected.\n");
                      send_message(new_socket, buffer);
                      cleanMsg(buffer, msg);
                      shutdown(new_socket, 2);
                      closesocket(new_socket);
                      break;
                  }
                  else if (clients_socket[i] == 0)
                  {
                      clients_socket[i] = new_socket;
                      printf("New client connected with socket %d from %s:%d, in slot %d\n", clients_socket[i], inet_ntoa(client_address.sin_addr), ntohs(client_address.sin_port), i);
                      strcpy(buffer, "Success connecting.\n");
                      send_message(clients_socket[i], buffer);
                      cleanMsg(buffer, msg);
                      break;
                  }
              }
          }
          else
          {
              for (i = 0; i &lt; s_conf-&gt;max_client; i++)
              {
                  if (FD_ISSET(clients_socket[i], &amp;readfs))
                  {
                      statu = receive_message(clients_socket[i], buffer);
                      if (statu == 0)
                      {
                          printf("Socket %d Disconnect\n", clients_socket[i]);
                          shutdown(clients_socket[i], 2);
                          closesocket(clients_socket[i]);
                          clients_socket[i] = 0;
                          break;
                      }
                      else
                      {
                          send_toall(clients_socket, clients_socket[i], s_conf-&gt;max_client, buffer);
                          cleanMsg(buffer, msg);
                          break;
                      }
                  }
              }
          }
      }
      return *clients_socket;
  }
  
  void    send_toall(int *clients_socket, int actual_socket, int max, char *buffer)
  {
      int i;
  
      for (i = 0; clients_socket[i] &lt; max; i++)
      {
          if (clients_socket[i] != actual_socket &amp;&amp; clients_socket[i] != 0)
              send_message(clients_socket[i], buffer);
      }
  }
  
  void    send_message(SOCKET socket, char *buffer)
  {
      int i;
  
      for (i = 0; buffer[i] != '\n'; i++) ;
      buffer[i] = '\0';
  
      if (send(socket, buffer, strlen(buffer), 0) &lt; 0)
      {
          perror("send()");
          exit(errno);
      }
  }
  
  int receive_message(SOCKET socket, char *buffer)
  {
      int statu = 0;
      if ((statu = recv(socket, buffer, 1024, 0)) &lt; 0)
          perror("recv()");
      return statu;
  }
  
  void cleanMsg(char *buffer, char *msg)
  {
      memset(buffer, 0, strlen(buffer));
      memset(msg, 0, strlen(msg));
      free(buffer);
      free(msg);
  }
  </code></pre>
  
  <p>Server just segfault and quit after client sending more than 10 messages.</p>