title: Segmentation fault / Incorrect checksum triggered ocasionally when reading lines from a file
body: <p>I'm trying to implement a function that gets every line from a file and prints it. For some reason, when running it I get a segmentation fault sometimes, sometimes it just runs fine, sometimes it exits with a incorrect checksum for freed object. I don't know which pointer is not being freed/modified after freeing it, can I get some clue?</p>
  <p>The variable <strong>BUFFER_SIZE</strong> is defined from the keyboard, where I compile with the flags <strong>gcc -Wall -Werror -Wextra -D BUFFER_SIZE=23 get_next_line.c main.c</strong></p>
  <pre class="lang-c prettyprint-override"><code>
      #include &lt;string.h&gt;
      #include &lt;stdlib.h&gt;
      #include &lt;sys/types.h&gt;
  
      char    *ft_strjoin(char const *s1, char const *s2)
      {
          char    *s;
          int     p;
          int     p2;
      
          if (s1 == NULL || s2 == NULL)
              return (NULL);
          s = malloc(strlen(s1) + strlen(s2) + 1);
          if (s == NULL)
              return (NULL);
          p = 0;
          while (s1[p])
          {
              s[p] = s1[p];
              p++;
          }
          p2 = 0;
          while (s2[p2])
          {
              s[p] = s2[p2];
              p++;
              p2++;
          }
          s[p] = '\0';
          return (s);
      }
  
      char    *ft_newstatic(char *aux, char *l, char **line)
      {
          char    *temp;
          int     leng;
      
          leng = strlen(l);
          temp = malloc(leng + 1);
          strlcat(temp, l, strlen(temp) + (leng - strlen(aux)) + 1);
          *line = strdup(temp);
          free(temp);
          l = NULL;
          l = strdup(&amp;aux[1]);
          return (l);
      }
      
      int     get_next_line(int fd, char **line)
      {
          static char *stc_line;
          char        *buffer;
          ssize_t     nbytes;
          char        *aux;
      
          stc_line = (!stc_line) ? strdup(&quot;&quot;) : stc_line;
          buffer = malloc(BUFFER_SIZE + 1);
          if (!buffer || fd &lt;= 0 || BUFFER_SIZE &lt;= 0 || !line)
              return (-1);
          while ((nbytes = read(fd, buffer, BUFFER_SIZE)) &gt; 0)
          {
              buffer[nbytes] = '\0';
              stc_line = ft_strjoin(stc_line, buffer);
              if ((aux = strchr(stc_line, '\n')) != NULL)
              {
                  //free(buffer);
                  return (((stc_line = ft_newstatic(aux, stc_line, line)) != NULL) ? 1 : 1);
              }
          }
          if (nbytes &lt; 0)
              return (-1);
          if ((aux = strchr(stc_line, '\n')) != NULL)
          {
              //free(buffer);
              return (((stc_line = ft_newstatic(aux, stc_line, line)) != NULL) ? 1 : 1);
          }
          *line = strdup(stc_line);
          stc_line = NULL;
          free(buffer);
          return (0);
      }
  
  </code></pre>
  <p>If I remove both free(buffer) before returning ft_newstatic I get better results (less running time and not as many seg faults), but if I remove</p>
  <pre><code>stc_line = NULL;
  free(stc_line);
  </code></pre>
  <p>I get weird output again:</p>
  <blockquote>
  <p>2- archivo: texto.txt</p>
  <p>[Return: 1] Line #124458: En un lugar de la mancha</p>
  <p>[Return: 1] Line #124459: de cuyo nombre no quiero acordarme</p>
  <p>[Return: 1] Line #124460: habia un hidalgo de los de lanza en astillero</p>
  <p>[Return: 1] Line #124461: adarga antigua</p>
  <p>[Return: 1] Line #124462: rocín flaco</p>
  <p>[Return: 0] Line #124463: y galgo corredor.</p>
  <p>End of file</p>
  <p>3- archivo: texto2.txt</p>
  <p>[Return: 1] Line #124464: y galgo corredor.linea 1</p>
  <p>[Return: 1] Line #124465: linea 2</p>
  <p>[Return: 1] Line #124466: linea 3</p>
  <p>[Return: 0] Line #124467: linea 4</p>
  <p>End of file</p>
  </blockquote>
  <p>This is the main() that I use</p>
  <pre class="lang-c prettyprint-override"><code>
      #include &lt;sys/types.h&gt;
      
      int get_next_line(int fd, char **line);
      
      int main(int argc, char **argv)
      {
          int fd;
          int ret;
          int line;
          char *buff;
          int     i;
      
          line = 0;
          if (argc &gt; 1)
          {
              i = 0;
              while (++i &lt; argc)
              {
                  fd = open(argv[i], O_RDONLY);
                  printf(&quot;%d- archivo: %s\n&quot;,i, argv[i]);
                  while ((ret = get_next_line(fd, &amp;buff)) &gt; 0)
                  {
                      printf(&quot;[Return: %d] Line #%d: %s\n&quot;, ret, ++line, buff);
                      free(buff);
                  }
                  printf(&quot;[Return: %d] Line #%d: %s\n&quot;, ret, ++line, buff);
                  if (ret == -1)
                      printf(&quot;-----------\nError\n&quot;);
                  else if (ret == 0)
                      printf(&quot;-----------\nEnd of file\n&quot;);
                  free(buff);
              }
          }
          if (argc == 1)
          {
              while ((ret = get_next_line(0, &amp;buff)) &gt; 0)
                  printf(&quot;[Return: %d] Line #%d: %s\n&quot;, ret, ++line, buff);
              if (ret == -1)
                  printf(&quot;-----------\nError\n&quot;);
              else if (ret == 0)
                  printf(&quot;-----------\nEnd of stdin\n&quot;);
              free(buff);
              close(fd);
          }
          return (0);
      }
  </code></pre>