title: Getting valgrind errors with static char *
body: <p>I post because i'm really lost.
  I've to do a function that reproduce the behavior of <code>getline</code>.</p>
  
  <p>To do that, I have to create a <code>static</code> buffer that save the beginning of the next line for next calls. But making my buffer static generate some errors. I know that I can't <code>malloc</code> a <code>static char *</code>, and that I've to initialize only the first time. But I think the buffer is not really malloc when I recall the function, because I've some errors during the second and more calls. I put my code thereafter.</p>
  
  <pre><code>char *get_next_line(int fd)
  {
      static char *s_buffer = NULL;
      char *f_buffer = malloc(sizeof(char));
      static int read_size = 0;
  
      f_buffer[0] = '\0';
      if (!s_buffer) {
          s_buffer = malloc(sizeof(char) * (READ_SIZE + 1));
          for (int i = 0; i &lt; READ_SIZE + 1; i++)
              s_buffer[i] = '\0';
      }
      do {
          for (int i = 0; s_buffer[i] != '\0'; i++) {
              if (s_buffer[i] == '\n') {
                  s_buffer = my_substr(s_buffer, i + 1, READ_SIZE);
                  return (f_buffer);
              }
              f_buffer = my_realloc(f_buffer, s_buffer[i]);
          }
          read_size = read(fd, s_buffer, READ_SIZE);
          if (read_size &lt;= 0)
              return (NULL);
          s_buffer[read_size] = '\0';
      } while (1);
  }
  </code></pre>
  
  <p>Functions <code>my_realloc</code> and <code>my_substr</code> are working, I tested them.</p>
  
  <p>So first I get errors on <code>s_buffer[read_size] = '\0';</code>, invalid write.
  And as I said before, I get some errors on the <code>read</code> function.</p>
  
  <p>I put logs :</p>
  
  <pre><code>==2004== Syscall param read(buf) points to unaddressable byte(s)
  ==2004==    at 0x4F3B1B0: __read_nocancel (in /usr/lib64/libc-2.25.so)
  ==2004==    by 0x4008D0: get_next_line (get_next_line.c:69)
  ==2004==    by 0x40094D: main (get_next_line.c:81)
  ==2004==  Address 0x5210ee6 is 0 bytes after a block of size 182 alloc'd
  ==2004==    at 0x4C2EB6B: malloc (vg_replace_malloc.c:299)
  ==2004==    by 0x400747: my_substr (get_next_line.c:36)
  ==2004==    by 0x400862: get_next_line (get_next_line.c:64)
  ==2004==    by 0x40094D: main (get_next_line.c:81)
  ==2004==
  ==2004== Invalid write of size 1
  ==2004==    at 0x4008FA: get_next_line (get_next_line.c:72)
  ==2004==    by 0x40094D: main (get_next_line.c:81)
  ==2004==  Address 0x5210ef8 is 18 bytes after a block of size 182 alloc'd
  ==2004==    at 0x4C2EB6B: malloc (vg_replace_malloc.c:299)
  ==2004==    by 0x400747: my_substr (get_next_line.c:36)
  ==2004==    by 0x400862: get_next_line (get_next_line.c:64)
  ==2004==    by 0x40094D: main (get_next_line.c:81)
  </code></pre>
  
  <p>I know that <code>Syscall param read(buf) points to unaddressable byte(s)</code> means that my buffer is NULL, but it is not, i think.
  However, I don't know for the invalid write.</p>
  
  <p>If someone understand my code, I'm blocked since 3 days and I continue to not understand these errors.</p>
  
  <p>Thanks for reading, bye ! (sorry for bad english)</p>