title: realloc( ): Invalid next size
body: <p>Minimal reproducible example:</p>
  <blockquote>
  <p>prog.c</p>
  </blockquote>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main(void) {
    size_t bufsiz = 4, tmp;
    char *buffer;
  
    for (buffer = malloc(bufsiz), tmp = 0; buffer[tmp] = fgetc(stdin); ++tmp) {
      if (bufsiz == tmp &amp;&amp; !realloc(buffer, bufsiz *= 2))
        return 1;
  } }
  </code></pre>
  <blockquote>
  <p>Compile and run</p>
  </blockquote>
  <pre><code>gcc prog.c -o prog
  
  echo duck | ./prog
  realloc(): Invalid next size
  Aborted
  </code></pre>
  <p>I have read  many similar threads but could not find the solution.</p>