<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>140917</bug_id>
          
          <creation_ts>2015-01-26 19:05:38 -0800</creation_ts>
          <short_desc>Windows return -1 when calling vsnprintf with arguments that exceed target buffer size</short_desc>
          <delta_ts>2015-01-30 05:22:19 -0800</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>Web Template Framework</component>
          <version>528+ (Nightly build)</version>
          <rep_platform>PC</rep_platform>
          <op_sys>Windows 7</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          <see_also>https://bugs.webkit.org/show_bug.cgi?id=140945</see_also>
    
    <see_also>https://bugs.webkit.org/show_bug.cgi?id=140847</see_also>
    
    <see_also>https://bugs.webkit.org/show_bug.cgi?id=141078</see_also>
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>InRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Namhoon Kim">nakim</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>benjamin</cc>
    
    <cc>bfulgham</cc>
    
    <cc>cmarcelo</cc>
    
    <cc>commit-queue</cc>
    
    <cc>webkit-bug-importer</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1064345</commentid>
    <comment_count>0</comment_count>
    <who name="Namhoon Kim">nakim</who>
    <bug_when>2015-01-26 19:05:38 -0800</bug_when>
    <thetext>According to https://msdn.microsoft.com/en-us/library/1kt27hek.aspx windows return -1 when calling vsnprintf with arguments that exceed target buffer size.

e.g.)

   char buf[5];
   int return = vsnprintf(buf, 5, &quot;%s&quot;, &quot;0123456789&quot;);

This API return -1 while other platforms (linux, bsd) return 10 instead. Other platform&apos;s reference can be found below.

bsd - https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/vsnprintf.3.html
linux - http://man7.org/linux/man-pages/man3/vsnprintf.3.html

I found this bug while testing JSC::Profiler::Database which announced in https://docs.google.com/document/d/18MQU5Dm31g4cVweuQuGofQAxfbenAAsE_njeTUuKOVA/edit from windows platform. Current WebKit&apos;s vsnprintf use looks safe to not overflow the buffer. But when this profiler tries to dump its source code by using WTF::StringPrintStream::toCString(), windows&apos; vsnprintf&apos;s -1 return value eventually *broke* WTF::StringPrintStream::vprintf() function&apos;s buffer grow logic. Since WTF::StringPrintStream::toCString() buffer grow logic depends to other (linux, bsd) platform&apos;s vsnprintf behavior, we need to fix return value of wtf_vsnprintf (which is windows&apos; polyfill of vsnprintf) to same as other platform.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1064346</commentid>
    <comment_count>1</comment_count>
    <who name="Namhoon Kim">nakim</who>
    <bug_when>2015-01-26 19:06:35 -0800</bug_when>
    <thetext>I&apos;ll submit a patch also.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1064430</commentid>
    <comment_count>2</comment_count>
      <attachid>245424</attachid>
    <who name="Namhoon Kim">nakim</who>
    <bug_when>2015-01-26 22:31:33 -0800</bug_when>
    <thetext>Created attachment 245424
Patch</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1064748</commentid>
    <comment_count>3</comment_count>
      <attachid>245424</attachid>
    <who name="Brent Fulgham">bfulgham</who>
    <bug_when>2015-01-27 18:09:57 -0800</bug_when>
    <thetext>Comment on attachment 245424
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=245424&amp;action=review

Thank you for noticing this! I had to do some reading just now to understand your change.

&gt; Source/WTF/wtf/StringExtras.h:65
&gt; +        result = _vscprintf(format, args);

I see; MSVC returns negative in the case of a &apos;buffer overrun converted to a truncation&apos;, while POSIX systems return the count as if nothing bad happened.

Very nice fix!</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1064749</commentid>
    <comment_count>4</comment_count>
    <who name="Brent Fulgham">bfulgham</who>
    <bug_when>2015-01-27 18:10:38 -0800</bug_when>
    <thetext>Thank you for tracking this down. Do we have any other cases where a similar mistake is being made?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1064750</commentid>
    <comment_count>5</comment_count>
      <attachid>245424</attachid>
    <who name="Brent Fulgham">bfulgham</who>
    <bug_when>2015-01-27 18:13:38 -0800</bug_when>
    <thetext>Comment on attachment 245424
Patch

Looking over this file, it seems like our &apos;snprintf&apos; (a few lines above) suffers from the exact same problem. Could you please make the same correction there? Then we can land the patch in one go.

r- to request the additional fix be done for &apos;snprintf&apos;. Otherwise, this looks great!</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1064762</commentid>
    <comment_count>6</comment_count>
    <who name="Namhoon Kim">nakim</who>
    <bug_when>2015-01-27 18:39:02 -0800</bug_when>
    <thetext>&gt; Thank you for tracking this down. Do we have any other cases where a similar mistake is being made?

MSVC version of vprintf_stderr_common (in Assertions.cpp) has similar (or worse) problem. If result debug formatted string length is exactly 1024 bytes, there is no null character placed in the buffer. It results following OutputDebugStringA call prints some garbage bytes also.

I think we simply fix it by calling vsnprintf (which is polyfilled by wtf_vsnprintf when using windows) instead of _vsnprintf.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1064763</commentid>
    <comment_count>7</comment_count>
    <who name="Namhoon Kim">nakim</who>
    <bug_when>2015-01-27 18:43:56 -0800</bug_when>
    <thetext>&gt; Looking over this file, it seems like our &apos;snprintf&apos; (a few lines above) suffers from the exact same problem. Could you please make the same correction there? Then we can land the patch in one go.

I fixed it too. Correcting handled before va_end calling. See below hunk please.

 inline int snprintf(char* buffer, size_t count, const char* format, ...) 
 {
     int result;
     va_list args;
     va_start(args, format);
     result = _vsnprintf(buffer, count, format, args);
+    if (result &lt; 0 &amp;&amp; errno != EINVAL)
+        result = _vscprintf(format, args);
     va_end(args);</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1064942</commentid>
    <comment_count>8</comment_count>
    <who name="Brent Fulgham">bfulgham</who>
    <bug_when>2015-01-28 10:06:26 -0800</bug_when>
    <thetext>(In reply to comment #7)
&gt; &gt; Looking over this file, it seems like our &apos;snprintf&apos; (a few lines above) suffers from the exact same problem. Could you please make the same correction there? Then we can land the patch in one go.
&gt; 
&gt; I fixed it too. Correcting handled before va_end calling. See below hunk
&gt; please.
&gt; 
&gt;  inline int snprintf(char* buffer, size_t count, const char* format, ...) 
&gt;  {
&gt;      int result;
&gt;      va_list args;
&gt;      va_start(args, format);
&gt;      result = _vsnprintf(buffer, count, format, args);
&gt; +    if (result &lt; 0 &amp;&amp; errno != EINVAL)
&gt; +        result = _vscprintf(format, args);
&gt;      va_end(args);

Were you planning up submitting an updated patch? Let me know and I&apos;ll try to get this in as soon as you have an update.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1065002</commentid>
    <comment_count>9</comment_count>
    <who name="Brent Fulgham">bfulgham</who>
    <bug_when>2015-01-28 12:49:48 -0800</bug_when>
    <thetext>This bug could have the following impacts:

1. Our NSAPI plugin tests.
2. Tests involving EditingDelegate dump() operations.
3. Stuff passing through JSC&apos;s globalFuncEscape method.
4. JSC date functions (dateProtoFuncToISOString)
5. XML Document parser
6. Windows clipboard utilities.
7. TextStream/TextCodec clients

It will be interesting to see if any of the Windows JSC Date tests start working once this fix is in place.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1065004</commentid>
    <comment_count>10</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2015-01-28 12:51:25 -0800</bug_when>
    <thetext>&lt;rdar://problem/19635602&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1065092</commentid>
    <comment_count>11</comment_count>
    <who name="Namhoon Kim">nakim</who>
    <bug_when>2015-01-28 17:41:57 -0800</bug_when>
    <thetext>(In reply to comment #8)
&gt; (In reply to comment #7)
&gt; &gt; &gt; Looking over this file, it seems like our &apos;snprintf&apos; (a few lines above) suffers from the exact same problem. Could you please make the same correction there? Then we can land the patch in one go.
&gt; &gt; 
&gt; &gt; I fixed it too. Correcting handled before va_end calling. See below hunk
&gt; &gt; please.
&gt; &gt; 
&gt; &gt;  inline int snprintf(char* buffer, size_t count, const char* format, ...) 
&gt; &gt;  {
&gt; &gt;      int result;
&gt; &gt;      va_list args;
&gt; &gt;      va_start(args, format);
&gt; &gt;      result = _vsnprintf(buffer, count, format, args);
&gt; &gt; +    if (result &lt; 0 &amp;&amp; errno != EINVAL)
&gt; &gt; +        result = _vscprintf(format, args);
&gt; &gt;      va_end(args);
&gt; 
&gt; Were you planning up submitting an updated patch? Let me know and I&apos;ll try
&gt; to get this in as soon as you have an update.

What I mean is, that hunk already in the patch I submitted. https://bugs.webkit.org/attachment.cgi?id=245424&amp;action=diff

If I miss understand something on this context, please let me know.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1065093</commentid>
    <comment_count>12</comment_count>
    <who name="Brent Fulgham">bfulgham</who>
    <bug_when>2015-01-28 17:46:56 -0800</bug_when>
    <thetext>(In reply to comment #11)
&gt; What I mean is, that hunk already in the patch I submitted.
&gt; https://bugs.webkit.org/attachment.cgi?id=245424&amp;action=diff
&gt; 
&gt; If I miss understand something on this context, please let me know.

Ah! you are right. Sorry!

But I also though you mentioned that something about a similar error in Assertions.cpp that needed correcting?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1065097</commentid>
    <comment_count>13</comment_count>
      <attachid>245424</attachid>
    <who name="Brent Fulgham">bfulgham</who>
    <bug_when>2015-01-28 17:49:57 -0800</bug_when>
    <thetext>Comment on attachment 245424
Patch

r=me. We can deal with the Assertions.cpp change elsewhere.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1065098</commentid>
    <comment_count>14</comment_count>
    <who name="Brent Fulgham">bfulgham</who>
    <bug_when>2015-01-28 17:50:35 -0800</bug_when>
    <thetext>I&apos;ll land this as-is. Could you please also package the Assertion.cpp change you mentioned as a separate bug and we&apos;ll get that landed, too!</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1065119</commentid>
    <comment_count>15</comment_count>
      <attachid>245424</attachid>
    <who name="WebKit Commit Bot">commit-queue</who>
    <bug_when>2015-01-28 18:32:19 -0800</bug_when>
    <thetext>Comment on attachment 245424
Patch

Clearing flags on attachment: 245424

Committed r179325: &lt;http://trac.webkit.org/changeset/179325&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1065120</commentid>
    <comment_count>16</comment_count>
    <who name="WebKit Commit Bot">commit-queue</who>
    <bug_when>2015-01-28 18:32:23 -0800</bug_when>
    <thetext>All reviewed patches have been landed.  Closing bug.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1065187</commentid>
    <comment_count>17</comment_count>
    <who name="Namhoon Kim">nakim</who>
    <bug_when>2015-01-29 01:34:30 -0800</bug_when>
    <thetext>(In reply to comment #14)
&gt; I&apos;ll land this as-is. Could you please also package the Assertion.cpp change
&gt; you mentioned as a separate bug and we&apos;ll get that landed, too!

Thank you. I&apos;ll pile a bug for that. And I&apos;ll check related bugs you linked also.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1065259</commentid>
    <comment_count>18</comment_count>
    <who name="Brent Fulgham">bfulgham</who>
    <bug_when>2015-01-29 09:06:07 -0800</bug_when>
    <thetext>(In reply to comment #9)
&gt; This bug could have the following impacts:
&gt; 
&gt; 1. Our NSAPI plugin tests.
&gt; 2. Tests involving EditingDelegate dump() operations.
&gt; 3. Stuff passing through JSC&apos;s globalFuncEscape method.
&gt; 4. JSC date functions (dateProtoFuncToISOString)
&gt; 5. XML Document parser
&gt; 6. Windows clipboard utilities.
&gt; 7. TextStream/TextCodec clients
&gt; 
&gt; It will be interesting to see if any of the Windows JSC Date tests start
&gt; working once this fix is in place.

I reran the JSC date function tests, but unfortunately did not see any progressions after applying this patch. Furthermore, the main test units have not shown any new progressions since this change.

So, this is a very useful fix but I don&apos;t think our test infrastructure was triggering any bad behavior due to this problem.</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="1"
              isprivate="0"
          >
            <attachid>245424</attachid>
            <date>2015-01-26 22:31:33 -0800</date>
            <delta_ts>2015-01-28 18:32:19 -0800</delta_ts>
            <desc>Patch</desc>
            <filename>bug-140917-20150127153136.patch</filename>
            <type>text/plain</type>
            <size>2430</size>
            <attacher name="Namhoon Kim">nakim</attacher>
            
              <data encoding="base64">U3VidmVyc2lvbiBSZXZpc2lvbjogMTc5MTU3CmRpZmYgLS1naXQgYS9Tb3VyY2UvV1RGL0NoYW5n
ZUxvZyBiL1NvdXJjZS9XVEYvQ2hhbmdlTG9nCmluZGV4IGY4Yjk1OTcyOTgyMzliMmNmODYyYzU2
MjZhMzFmMjJlZjg4NDYzNzMuLjAxZDJhYmM1YjE0ZDhiODY4NzQwOWI0ZDNmNzA0YWI2Mzg4MDlh
NGQgMTAwNjQ0Ci0tLSBhL1NvdXJjZS9XVEYvQ2hhbmdlTG9nCisrKyBiL1NvdXJjZS9XVEYvQ2hh
bmdlTG9nCkBAIC0xLDMgKzEsMTcgQEAKKzIwMTUtMDEtMjYgIE5hbWhvb24gS2ltICA8bmFta2lt
QGVhLmNvbT4KKworICAgICAgICBXaW5kb3dzIHJldHVybiAtMSB3aGVuIGNhbGxpbmcgdnNucHJp
bnRmIHdpdGggYXJndW1lbnRzIHRoYXQgZXhjZWVkIHRhcmdldCBidWZmZXIgc2l6ZQorICAgICAg
ICBodHRwczovL2J1Z3Mud2Via2l0Lm9yZy9zaG93X2J1Zy5jZ2k/aWQ9MTQwOTE3CisKKyAgICAg
ICAgUmV2aWV3ZWQgYnkgTk9CT0RZIChPT1BTISkuCisKKyAgICAgICAgRml4IHJldHVybiB2YWx1
ZSBvZiB2c25wcmludGYgd2hlbiB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gdmFfbGlzdCBhcmdzKQogewogICAgIGludCByZXN1
bHQgPSBfdnNucHJpbnRmKGJ1ZmZlciwgY291bnQsIGZvcm1hdCwgYXJncyk7CisgICAgaWYgKHJl
c3VsdCA8IDAgJiYgZXJybm8gIT0gRUlOVkFMKQorICAgICAgICByZXN1bHQgPSBfdnNjcHJpbnRm
KGZvcm1hdCwgYXJncyk7CiAKICAgICAvLyBJbiB0aGUgY2FzZSB3aGVyZSB0aGUgc3RyaW5nIGVu
dGlyZWx5IGZpbGxlZCB0aGUgYnVmZmVyLCBfdnNucHJpbnRmIHdpbGwgbm90CiAgICAgLy8gbnVs
bC10ZXJtaW5hdGUgaXQsIGJ1dCB2c25wcmludGYgbXVzdC4K
</data>

          </attachment>
      

    </bug>

</bugzilla>