title: swprintf truncation causes unexpected output
body: <p>I'm fixing legacy code that runs on linux and windows and in some cases buffers that are supposed to contain formatted content are smaller than that content.</p>
  
  <p>The code uses swprintf which according to the <a href="https://en.cppreference.com/w/cpp/io/c/fwprintf" rel="nofollow noreferrer">documentation</a> </p>
  
  <blockquote>
    <p>size  -   up to size - 1 characters may be written, plus the null terminator </p>
  </blockquote>
  
  <p>Will indeed truncate the string, but while trying it on <a href="http://coliru.stacked-crooked.com/a/3e674ccafeac7dd2" rel="nofollow noreferrer">coliru</a> I encountered unexpected results:</p>
  
  <pre><code>#include &lt;iostream&gt; 
  #include &lt;string&gt; 
  #include &lt;cwchar&gt; 
  
  int main()
  {
  
      wchar_t wide[5];
  
      std::swprintf(wide, sizeof wide/sizeof *wide, L"%ls", L"111111111");
  
      std::wcout &lt;&lt; wide;
  }
  </code></pre>
  
  <p>will result in <code>1111??</code> but</p>
  
  <pre><code>#include &lt;iostream&gt; 
  #include &lt;string&gt; 
  #include &lt;cwchar&gt; 
  
  int main()
  {
  
      wchar_t wide[20];
  
      std::swprintf(wide, sizeof wide/sizeof *wide, L"%ls", L"111111111");
  
      std::wcout &lt;&lt; wide;
  }
  </code></pre>
  
  <p>Works just fine. </p>
  
  <p>What's wrong ?</p>
  
  <p>P.S. 
  I wish I could change everything to C++ streams/string but I can't, <code>wchar_t</code> arrays are used everywhere</p>