Boost C++ Libraries: Ticket #4636: Run-Time Check Failure #1 in feed_args.hpp using Visual Studio 2010 https://svn.boost.org/trac10/ticket/4636 <p> Hello, </p> <p> the statement </p> <blockquote> <p> size_type res_size = (std::min)( </p> <blockquote> <p> static_cast&lt;size_type&gt;(specs.truncate_ - !!prefix_space), buf.pcount() ); </p> </blockquote> </blockquote> <p> on line 166 ff. causes an error message in Debug mode in VS 2010 when run-time checks are enabled: </p> <p> "Run-Time Check Failure <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/1" title="#1: Bugs: boost.build causes ftjam to segfault (closed: Wont Fix)">#1</a> - A cast to a smaller data type has caused a loss of data. If this was intentional, you should mask the source of the cast with the appropriate bitmask. For example: </p> <blockquote> <p> char c = (i &amp; 0xFF); </p> </blockquote> <p> Changing the code in this way will not affect the quality of the resulting optimized code." </p> <p> The value of specs.truncate_ (64 bits) is LLONG_MAX that is going to be cast down to a size_t (32 bits). This causes that warning. Would it be feasible to initalise the truncate_ member to INT_MAX on 32 bit systems to avoid this issue? </p> <p> This message is *very* annoying since it pops up very frequently. </p> <p> Best regards Rüdiger </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/4636 Trac 1.4.3 Steven Watanabe Fri, 11 Feb 2011 04:19:44 GMT <link>https://svn.boost.org/trac10/ticket/4636#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4636#comment:1</guid> <description> <p> FWIW, I advise against enabling run-time checks, since the result is non-conformant. The behavior of this cast is well defined according to the standard. </p> </description> <category>Ticket</category> </item> <item> <author>Rüdiger Brünner <rbruenner@…></author> <pubDate>Mon, 14 Feb 2011 07:11:58 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4636#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4636#comment:2</guid> <description> <p> Yes, I understand that these run-time checks produce a lot of noise. They are good for finding obscure bugs but I no longer use them permanently. </p> <p> Best regards </p> </description> <category>Ticket</category> </item> <item> <author>kurt@…</author> <pubDate>Wed, 02 Mar 2011 21:58:46 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4636#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4636#comment:3</guid> <description> <p> We just ran into this as well. That type check is useful for debugging, and it would be great if Boost played nice with it. </p> </description> <category>Ticket</category> </item> <item> <author>Gordon.Geddes@…</author> <pubDate>Fri, 06 May 2011 09:46:14 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4636#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4636#comment:4</guid> <description> <p> We've also just come across this problem. By default we run our code in debug with all runtime checks as they can be useful to highlight potential problems but with boost not working with them we're having to switch them off in affected projects. It would be nice if boost did work with these runtime checks. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Fri, 21 Oct 2011 05:39:48 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4636#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4636#comment:5</guid> <description> <p> I just ran into this very annoying problem as well. </p> <p> The cast may be well defined by the standard, but there are quite a few situations where you want to detect overflowing a 32-bit variable with a 64-bit value. So those checks do make sense to me. </p> <p> I second that it would be nice if boost worked with these checks. </p> <p> -Matthias </p> </description> <category>Ticket</category> </item> <item> <author>dxc@…</author> <pubDate>Tue, 29 Nov 2011 12:50:52 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4636#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4636#comment:6</guid> <description> <p> it's still present in boost 1.48.0. we run our regressions on both debug and release builds, and for debug we turn on all the runtime checks. it is well and fine that the standard defines the way the cast is supposed to work, but it is also true that sometimes this runtime check catches a programmer error. we have caught a few corner-cases using this check that otherwise would have been lurking until the customer found it, no doubt at the worst possible time. </p> <p> sure, I can add the masking trick myself, but 1) not having to mess around in the code is one reason to use high quality libraries; and 2) if *I* patch it now I have to remember to patch every time, and if the code changes this may be non-trivial. </p> <p> can this really not be fixed? it has been there since at least 1.38 according to the boost users mailing list. </p> <p> dxc </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Fri, 01 Jun 2012 19:02:46 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4636#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4636#comment:7</guid> <description> <p> Still there in 1.49. I've told VS2010 to not do any exception checks in Debug mode and it still pops up a dialog box. Very annoying. Works just fine in Release mode, tho'. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Mon, 10 Jun 2013 22:03:47 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4636#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4636#comment:8</guid> <description> <p> Still an issue in 1.53. We added the masking trick in when we were using 1.48, but forgot we did when we upgraded and were confused when our unit test failed. Wasted time. Can we please fix this? </p> </description> <category>Ticket</category> </item> <item> <author>emmanuel.andre@…</author> <pubDate>Wed, 19 Jun 2013 10:01:49 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4636#comment:9 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4636#comment:9</guid> <description> <p> Same issue here... We had to deactivate runtime checking of overflows for the whole impacted component. </p> <p> When can we expect a fix ? Do you want that we propose/work on a solution ? </p> <p> Best regards </p> </description> <category>Ticket</category> </item> <item> <dc:creator>timblechmann</dc:creator> <pubDate>Mon, 18 Nov 2013 08:43:41 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4636#comment:10 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4636#comment:10</guid> <description> <p> very annoying issue. even if the behaviour of the cast may be well defined according to the standard, it would not hurt to implement it explicitly to avoid msvc firing on a potential false positive. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>timblechmann</dc:creator> <pubDate>Mon, 18 Nov 2013 09:14:21 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4636#comment:11 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4636#comment:11</guid> <description> <p> i'd suggest the following patch: </p> <pre class="wiki">--- a/boost/format/feed_args.hpp +++ b/boost/format/feed_args.hpp @@ -21,6 +21,7 @@ #include &lt;boost/format/format_class.hpp&gt; #include &lt;boost/format/group.hpp&gt; #include &lt;boost/format/detail/msvc_disambiguater.hpp&gt; +#include &lt;boost/integer_traits.hpp&gt; namespace boost { namespace io { @@ -171,8 +172,8 @@ namespace detail { (res_beg[0] !=oss.widen('+') &amp;&amp; res_beg[0] !=oss.widen('-') )) prefix_space = oss.widen(' '); size_type res_size = (std::min)( - static_cast&lt;size_type&gt;(specs.truncate_ - !!prefix_space), - buf.pcount() ); + static_cast&lt;size_type&gt;(specs.truncate_ &amp; integer_traits&lt;size_type&gt;::const_max - !!prefix_space), + static_cast&lt;size_type&gt;(buf.pcount()) ); mk_str(res, res_beg, res_size, w, oss.fill(), fl, prefix_space, (specs.pad_scheme_ &amp; format_item_t::centered) !=0 ); } </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Sun, 26 Jan 2014 18:50:39 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4636#comment:12 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4636#comment:12</guid> <description> <p> Hi, </p> <p> Whether this issue is fixed in latest version of boost library 1.55?.When we apply above mentioned patch is that any thing compromised?. I am facing this issue at run time only. Does the above patch leads to any data loss? Because we are making application level changes to support Unicode format(to support all languages). </p> <p> i.e wcout&lt;&lt;boost::wformat("My name is %s") %username </p> </description> <category>Ticket</category> </item> <item> <author>adam.f.badura@…</author> <pubDate>Sun, 07 Jun 2015 18:09:05 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4636#comment:13 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4636#comment:13</guid> <description> <p> The problem still exists in Boost 1.58. </p> </description> <category>Ticket</category> </item> <item> <author>adam.f.badura@…</author> <pubDate>Sun, 07 Jun 2015 18:09:58 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4636#comment:13 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4636#comment:13</guid> <description> <p> The problem still exists in Boost 1.58. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Sun, 09 Apr 2017 15:41:18 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4636#comment:14 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4636#comment:14</guid> <description> <p> The problem still exists in Boost 1.63 </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Sun, 09 Apr 2017 15:41:20 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4636#comment:14 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4636#comment:14</guid> <description> <p> The problem still exists in Boost 1.63 </p> </description> <category>Ticket</category> </item> <item> <dc:creator>James E. King, III</dc:creator> <pubDate>Wed, 11 Oct 2017 18:56:14 GMT</pubDate> <title>owner changed https://svn.boost.org/trac10/ticket/4636#comment:15 https://svn.boost.org/trac10/ticket/4636#comment:15 <ul> <li><strong>owner</strong> changed from <span class="trac-author">Samuel Krempp</span> to <span class="trac-author">James E. King, III</span> </li> </ul> <p> I am looking into this. </p> <p> Event without /RTCsuc I am able to see the same thing using a numeric_cast as follows: </p> <pre class="wiki"> size_type res_size = (std::min)( numeric_cast&lt;size_type&gt;(specs.truncate_) - !!prefix_space, buf.pcount() ); </pre><p> Which results in the error: </p> <p> class boost::numeric::positive_overflow: bad numeric conversion: positive overflow </p> <p> Inspecting it in the debugger I see: </p> <pre class="wiki">- specs {argN_=0 res_="" appendix_="00" ...} const boost::io::detail::format_item&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt; &amp; argN_ 0 int + res_ "" std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt; + appendix_ "00" std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt; + fmtstate_ {width_=0 precision_=6 fill_=32 ' ' ...} boost::io::detail::stream_format_state&lt;char,std::char_traits&lt;char&gt; &gt; truncate_ 9223372036854775807 __int64 pad_scheme_ 0 unsigned int </pre><p> The offending test is in format_test1.cpp: </p> <pre class="wiki"> if(str( format("%%##%%##%%1 %1%00") % "Escaped OK" ) != "%##%##%1 Escaped OK00") </pre><p> At first glance I would say that the value of truncate_ is wrong here, and figuring that out will end up being the root cause. </p> Ticket James E. King, III Wed, 11 Oct 2017 21:32:49 GMT <link>https://svn.boost.org/trac10/ticket/4636#comment:16 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4636#comment:16</guid> <description> <p> format_item::truncate_ uses a std::streamsize which is an unsigned long long type, and it is initialized to a max() value for that type if the format specification doesn't have explicit truncation defined. The code that can make a string (mk_str) has a maximum size of std::string::size_type which is an unsigned long. This impedance mismatch is really the core of the issue here. The code is relying on a clamping mechanism that ensures a cast of the maximum stream size is a useful value when truncated to 32-bits. Therefore it looks like the code in the patch is partially correct; there are other instances that need to be addressed so I will work on this. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>James E. King, III</dc:creator> <pubDate>Thu, 12 Oct 2017 01:01:33 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4636#comment:17 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4636#comment:17</guid> <description> <p> PR is out: <a class="ext-link" href="https://github.com/boostorg/format/pull/17"><span class="icon">​</span>https://github.com/boostorg/format/pull/17</a> </p> </description> <category>Ticket</category> </item> <item> <dc:creator>James E. King, III</dc:creator> <pubDate>Thu, 12 Oct 2017 01:02:07 GMT</pubDate> <title>keywords, status, milestone changed https://svn.boost.org/trac10/ticket/4636#comment:18 https://svn.boost.org/trac10/ticket/4636#comment:18 <ul> <li><strong>keywords</strong> RTC MSVC10 added; format Run-Time Check Failure removed </li> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> <li><strong>milestone</strong> <span class="trac-field-old">To Be Determined</span> → <span class="trac-field-new">Boost 1.66.0</span> </li> </ul> Ticket James E. King, III Thu, 12 Oct 2017 16:19:40 GMT <link>https://svn.boost.org/trac10/ticket/4636#comment:19 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4636#comment:19</guid> <description> <p> Merged into develop, will resolve this when develop merges into master. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>James E. King, III</dc:creator> <pubDate>Fri, 20 Oct 2017 16:20:51 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/4636#comment:20 https://svn.boost.org/trac10/ticket/4636#comment:20 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> Ticket