title: Valgrind reports uninitialized value inside standard library (vfprintf.c)
body: <p>I have a function which does a <code>vsnsprintf</code> into a temporary buffer in an object created on the stack. </p>
  
  <p>In the object's constructor I initialise the first character of the buffer to null.</p>
  
  <p>Valgrind complains about an <strong>uninitialised value</strong> created on the stack in <code>vfprintf.c</code></p>
  
  <p><strong>Full working example below, followed by valgrind output</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdarg.h&gt;
  #include &lt;string.h&gt;
  
  struct tmp_buf
  {
      tmp_buf() { *b = 0; }
      mutable char b[1024];
  };
  
  char const* va_stack_str(const char* format, va_list ap, const tmp_buf&amp; b = tmp_buf())
  {
      vsnprintf(b.b, sizeof(b.b), format, ap);
      return b.b;
  }
  
  char const* stack_str(const char* format, ...)
  {
      va_list ap;
      va_start(ap, format);
      const char* str = va_stack_str(format, ap);
      va_end(ap);
      return str;
  }
  
  int main()
  {
      printf("%s", stack_str("hello %s", "world"));
      return 0;
  }
  </code></pre>
  
  <p>The app works as expected, but running it through valgrind complains about <strong>uninitialised values</strong></p>
  
  <p>My valgrind command line is <code>valgrind --leak-check=full --track-origins=yes --quiet</code></p>
  
  <p><strong>Valgrind output:</strong></p>
  
  <pre><code>==30513== Conditional jump or move depends on uninitialised value(s)
  ==30513==    at 0x4E828F3: vfprintf (vfprintf.c:1661)
  ==30513==    by 0x4E8B388: printf (printf.c:33)
  ==30513==    by 0x400A73: main (main.cpp:28)
  ==30513==  Uninitialised value was created by a stack allocation
  ==30513==    at 0x4E80BF6: vfprintf (vfprintf.c:235)
  ==30513== 
  ==30513== Syscall param write(buf) points to uninitialised byte(s)
  ==30513==    at 0x4F233B0: __write_nocancel (syscall-template.S:81)
  ==30513==    by 0x4EB0A82: _IO_file_write@@GLIBC_2.2.5 (fileops.c:1261)
  ==30513==    by 0x4EB1F5B: _IO_do_write@@GLIBC_2.2.5 (fileops.c:538)
  ==30513==    by 0x4EB3ADD: _IO_flush_all_lockp (genops.c:848)
  ==30513==    by 0x4EB3C39: _IO_cleanup (genops.c:1013)
  ==30513==    by 0x4E730FA: __run_exit_handlers (exit.c:95)
  ==30513==    by 0x4E73194: exit (exit.c:104)
  ==30513==    by 0x4E58ECB: (below main) (libc-start.c:321)
  ==30513==  Address 0x4025000 is not stack'd, malloc'd or (recently) free'd
  ==30513==  Uninitialised value was created by a stack allocation
  ==30513==    at 0x4E80BF6: vfprintf (vfprintf.c:235)
  </code></pre>
  
  <p>Changing the <code>tmp_buf</code> constructor to <code>memset</code> the whole buffer doesn't change valgrind's output</p>
  
  <pre><code>tmp_buf() { memset(b, 0, sizeof(b)); }
  </code></pre>