title: Unix to Windows: Alternative to vsnprintf to determine length?
body: <p>I am currently converting the code of one of our Linux libraries to a Windows DLL.</p>
  
  <p>Within this library I have a function which takes the last parameters in a printf-way (format string,
  then ellipsis). Within this function I use vsnprintf to format the supplied arguments.
  Since I want to know whether I can cram the final string into a small buffer or if I'd have to allocate
  memory for it, I am interested in determining the "would-be-length" of the formatted string.</p>
  
  <p>To do this, I am currently using vsnprintf like this (made up example code, obviously):</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdarg.h&gt;
  
  void foo(const char* fmt, ...)
  {
     int len = 0;
     va_list ap;
  
     va_start(ap, fmt);
     len = vsnprintf(0, 0, fmt, ap);
     printf("len = %d\n", len);
     va_end(ap);
  }
  
  int main(void)
  {
     foo("12345%s", "67890");
     exit(0);
  }
  </code></pre>
  
  <p>This usage is covered by the <a href="http://pubs.opengroup.org/onlinepubs/009695399/toc.htm" rel="noreferrer">Open Group Base Specifications Issue 6</a>:</p>
  
  <blockquote>
    <p>vsnprintf(char *restrict s, size_t n, const char *restrict format, va_list ap)</p>
    
    <p><em>The [...] vsnprintf() [...] functions shall be equivalent to [...] snprintf().</em></p>
    
    <p>snprintf(char *restrict s, size_t n, const char *restrict format, ...)</p>
    
    <p><em>If the value of n is zero on a call to snprintf(), nothing shall be written, the number of bytes that would have been written had n been sufficiently large excluding the terminating null shall be returned, and s may be a null pointer.</em></p>
  </blockquote>
  
  <p>The problem arised as I was compiling this code on the Windows-System (Visual Studio 2010) with /analyze on. The compiler/analyzer gave me the following:</p>
  
  <blockquote>
    <p>test.c(11) : warning C6309: Argument '1' is null: this does not adhere to function specification of 'vsnprintf'</p>
    
    <p>test.c(11) : warning C6387: 'argument 1' might be '0': this does not adhere to the specification for the function 'vsnprintf': Lines: 7, 8, 10, 11</p>
  </blockquote>
  
  <p>A quick look at the <a href="http://msdn.microsoft.com/de-de/library/1kt27hek%28v=vs.80%29.aspx" rel="noreferrer">MSDN entry for vsnprintf</a> gave me this:</p>
  
  <blockquote>
    <p>If <em>buffer</em> or <em>format</em> is <strong>NULL</strong>, or if count is less than or equal to zero, these functions invoke the invalid parameter handler, as described in Parameter Validation. If execution is allowed to continue, these functions return -1.</p>
  </blockquote>
  
  <p>Curious enough, the above sample works nonetheless on Windows "as expected" (i.e. it returns to me the count of the characters that would be written).</p>
  
  <p>But since I don't want this to rely on something unspecified I'd like to know if there is a better, official way of achieving the same, without having to hope that this won't break in some future release.</p>
  
  <p>Thanks for your time!</p>