Boost C++ Libraries: Ticket #3606: One potential bug, and lots of warnings in DateTime https://svn.boost.org/trac10/ticket/3606 <p> Boost.<a class="missing wiki">DateTime</a> when compiled at high warning levels emits rather a lot of warnings, at least one of which seems to indicate a genuine potential bug and/or trap for the unwary. In addition these warnings are seen by libraries like Boost.Thread which depend on <a class="missing wiki">DateTime</a>, so the effect is not limited to just <a class="missing wiki">DateTime</a> itself. </p> <p> I'm attaching a log of the build for msvc-9 (with /W4) and gcc-4.4.1 (with -Wall -Wextra -pedantic) showing the warnings produced. </p> <p> As to the potential bug, consider this warning: </p> <pre class="wiki">d:\data\boost\trunk\boost/date_time/date_clock_device.hpp(41) : warning C4244: 'argument' : conversion from 'int' to 'unsigned short', possible loss of data d:\data\boost\trunk\boost/date_time/date_clock_device.hpp(36) : while compiling class template member function 'boost::gregorian::date::ymd_type boost::date_time::day_clock&lt;date_type&gt;::local_day_ymd(void)' with [ date_type=boost::gregorian::date ] gregorian\testdate_input_facet.cpp(257) : see reference to class template instantiation 'boost::date_time::day_clock&lt;date_type&gt;' being compiled with [ date_type=boost::gregorian::date ] </pre><p> The problem here is that the integer argument representing the day/month/year gets truncated to a short in the constructor of greg_day/greg_month/greg_year. The problem here is manifold: first off I can't find it documented anywhere what type the argument to a day/month/year type should be (conceptually speaking) so it's hard to justify an explicit type cast in generic code like this. More importantly, the implicit cast taking place here can circumvent the error checking inside the day/month/year type, consider: </p> <pre class="wiki"> int i = 0xf0001; boost::gregorian::greg_day d(i); </pre><p> You might expect this to throw since the value i is out of range, but it doesn't because i gets truncated to the value 1 before the constructor is called. Thus if these types are accidentally constructed with a garbage value (whether within the <a class="missing wiki">DateTime</a> headers, or in user code), the construction can actually succeed :-( </p> <p> I think one potential solution is to decouple error-checking from storage type - for example if the constructor for greg_day was changed to a template: </p> <p> template &lt;class Integer&gt; greg_day(Integer day_of_month); </p> <p> and likewise for the underlying base type constructor, then we can error check the actual argument passed, and only then cast it to the storage type used. As a bonus, is there any reason why the storage type is a short? Could it be an unsigned char? The latter would decrease the size of a day/month/year type from 8 to 4 bytes. </p> <p> Anyhow let me know if you need help fixing the other warnings - I stopped when I saw this one, because it didn't look like the usual spurious warning! </p> <p> Cheers, John. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/3606 Trac 1.4.3 John Maddock Wed, 11 Nov 2009 10:03:22 GMT attachment set https://svn.boost.org/trac10/ticket/3606 https://svn.boost.org/trac10/ticket/3606 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">msvc.log</span> </li> </ul> Ticket John Maddock Wed, 11 Nov 2009 10:04:33 GMT attachment set https://svn.boost.org/trac10/ticket/3606 https://svn.boost.org/trac10/ticket/3606 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">gcc.zip</span> </li> </ul> Ticket Paul A. Bristow Wed, 18 Jan 2012 12:17:47 GMT <link>https://svn.boost.org/trac10/ticket/3606#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3606#comment:1</guid> <description> <p> I've just hit this warning too, after writing as little as </p> <blockquote> <p> date d(day_clock::local_day()); std::cout &lt;&lt; d &lt;&lt; std::endl; </p> </blockquote> <blockquote> <p> warning C4244: 'argument' : conversion from 'int' to 'unsigned short', possible loss of data </p> </blockquote> <p> Since this warning C4244 is normally telling you something that you should worry about, I am reluctant to disable it. </p> <p> As John Maddock observes, this is not a trivial or purely cosmetic issue, but I feel it should be fixed somehow. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>az_sw_dude</dc:creator> <pubDate>Sun, 01 Jul 2012 17:45:42 GMT</pubDate> <title>status changed; version deleted https://svn.boost.org/trac10/ticket/3606#comment:2 https://svn.boost.org/trac10/ticket/3606#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> <li><strong>version</strong> <span class="trac-field-deleted">Boost 1.40.0</span> </li> </ul> <p> The initial issue here: </p> <p> int i = 0xf0001; boost::gregorian::greg_day d(i) </p> <p> I'm afraid that this is an issue with C++ itself -- it will automatically convert integer types that it really shouldn't. In this case greg_day takes and unsigned short simply truncating the value -- so in fact the value turns out to be within the valid range. In fact if 0xf0000 had been supplied an exception would have been thrown. So, at some level there is an argument that sorry folks, this is C++ if you deal with integer types you're going to have to know what you're doing. </p> <p> As an example: </p> <pre class="wiki"> //integer_behavior.cpp // g++-4.6 -Wall -Wextra -pedantic integer_behavior.cpp //no warnings, no errors... #include &lt;iostream&gt; int main() { unsigned short i = 0; int j = 0xf0001; i = j; std::cout &lt;&lt; i &lt;&lt; std::endl; //0 return 0; } </pre><p> Still, I take the point that there are possibly a few places where this could be cleaned up -- the template constructor suggestion isn't a bad one since VC6 doesn't need to be supported any longer. So I'll accept the defect for now and see how to eventually deal with it -- in the grand scheme of things it's likely a low priority. </p> Ticket James E. King, III Wed, 27 Dec 2017 13:44:18 GMT owner, status changed https://svn.boost.org/trac10/ticket/3606#comment:3 https://svn.boost.org/trac10/ticket/3606#comment:3 <ul> <li><strong>owner</strong> changed from <span class="trac-author">az_sw_dude</span> to <span class="trac-author">James E. King, III</span> </li> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">new</span> </li> </ul> Ticket James E. King, III Wed, 27 Dec 2017 15:11:56 GMT <link>https://svn.boost.org/trac10/ticket/3606#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3606#comment:4</guid> <description> <p> In this case C4244 is appropriate and called out to the author who wrote bad code. Use boost::gregorian::greg_day::value_type instead of int. </p> <p> I am going to use this issue to resolve all the other /W4 related warnings in the build of Boost.<a class="missing wiki">DateTime</a>. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>James E. King, III</dc:creator> <pubDate>Wed, 27 Dec 2017 19:20:56 GMT</pubDate> <title>status changed https://svn.boost.org/trac10/ticket/3606#comment:5 https://svn.boost.org/trac10/ticket/3606#comment:5 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> Ticket James E. King, III Thu, 11 Jan 2018 20:28:17 GMT <link>https://svn.boost.org/trac10/ticket/3606#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3606#comment:6</guid> <description> <p> <a class="ext-link" href="https://github.com/boostorg/date_time/pull/62"><span class="icon">​</span>https://github.com/boostorg/date_time/pull/62</a> </p> </description> <category>Ticket</category> </item> <item> <dc:creator>James E. King, III</dc:creator> <pubDate>Thu, 18 Jan 2018 14:11:29 GMT</pubDate> <title>milestone changed; version set https://svn.boost.org/trac10/ticket/3606#comment:7 https://svn.boost.org/trac10/ticket/3606#comment:7 <ul> <li><strong>version</strong> → <span class="trac-field-new">Boost 1.41.0</span> </li> <li><strong>milestone</strong> <span class="trac-field-old">Boost 1.42.0</span> → <span class="trac-field-new">Boost 1.67.0</span> </li> </ul> Ticket James E. King, III Sun, 28 Jan 2018 16:23:19 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/3606#comment:8 https://svn.boost.org/trac10/ticket/3606#comment:8 <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> <p> Fix merged to master; resolved. </p> Ticket