title: file pointer set to NULL after fprintf
body: <p>I am having the following piece of code:</p>
  
  <pre><code>void mylog(const char *fmt, ...) 
  {
      static FILE *logfp = NULL;
      struct timeval  tv;
      struct timezone tz;
      struct tm      *tm;
      va_list argptr;
      char fname[256];
      extern char *__progname;
  
      if(logfp == NULL) {
         snprintf(fname, 256, "%s.log", __progname);
         logfp = fopen(fname, "a");
         if(logfp == NULL) {
            fprintf(stderr, "Unable to create log file.\n");
            return;
         }
      }
  
      va_start(argptr, fmt);
      gettimeofday(&amp;tv, &amp;tz);
      tm = localtime(&amp;tv.tv_sec);
      fprintf(logfp, "%04d-%02d-%02d:%02d.%02d.%02d.%lu:",
          tm-&gt;tm_year+1900, tm-&gt;tm_mon, tm-&gt;tm_mday,
          tm-&gt;tm_hour, tm-&gt;tm_min, tm-&gt;tm_sec, tv.tv_usec);
      vfprintf(logfp, fmt, argptr);
      va_end(argptr);
      fflush(logfp);
  
      if(ftell(logfp) &gt; 256*1024) {
         fclose(logfp);
         logfp = NULL;
  
         /* Below - Logic to rename this log file for log rotation */
      }
  }
  </code></pre>
  
  <p>I observed a crash in <code>vfprintf</code> call. The gdb stack trace is:</p>
  
  <pre><code>#0  _IO_vfprintf_internal (s=0x0, format=0x4a8960 "log message\n", ap=0x7fa4b4202d30) at vfprintf.c:1269
  </code></pre>
  
  <p>The above piece of code has worked before without any problem. But, this crash is strange for me. I am not able to find, how the <code>fprintf</code> is setting <code>logfp</code> to NULL. The <code>fmt</code> was set to "log message\n".</p>