Opened 13 years ago

Closed 5 years ago

#3606 closed Bugs (fixed)

One potential bug, and lots of warnings in DateTime

Reported by: John Maddock Owned by: James E. King, III
Milestone: Boost 1.67.0 Component: date_time
Version: Boost 1.41.0 Severity: Problem
Keywords: Cc:

Description

Boost.DateTime 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 DateTime, so the effect is not limited to just DateTime itself.

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.

As to the potential bug, consider this warning:

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<date_type>::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<date_type>' being compiled
        with
        [
            date_type=boost::gregorian::date
        ]

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:

   int i = 0xf0001;
   boost::gregorian::greg_day d(i);

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 DateTime headers, or in user code), the construction can actually succeed :-(

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:

template <class Integer> greg_day(Integer day_of_month);

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.

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!

Cheers, John.

Attachments (2)

msvc.log​ (135.4 KB ) - added by John Maddock 13 years ago.
gcc.zip​ (9.6 KB ) - added by John Maddock 13 years ago.

Download all attachments as: .zip

Change History (10)

by John Maddock, 13 years ago

Attachment: msvc.log​ added

by John Maddock, 13 years ago

Attachment: gcc.zip​ added

comment:1 by Paul A. Bristow, 11 years ago

I've just hit this warning too, after writing as little as

date d(day_clock::local_day()); std::cout << d << std::endl;

warning C4244: 'argument' : conversion from 'int' to 'unsigned short', possible loss of data

Since this warning C4244 is normally telling you something that you should worry about, I am reluctant to disable it.

As John Maddock observes, this is not a trivial or purely cosmetic issue, but I feel it should be fixed somehow.

comment:2 by az_sw_dude, 10 years ago

Status: new → assigned
Version: Boost 1.40.0

The initial issue here:

int i = 0xf0001; boost::gregorian::greg_day d(i)

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.

As an example:

 //integer_behavior.cpp
 // g++-4.6 -Wall -Wextra -pedantic integer_behavior.cpp

 //no warnings, no errors...

 #include <iostream>

 int main()
 {
  unsigned short i = 0;
  int j = 0xf0001;
  i = j;
  std::cout << i << std::endl; //0
  return 0;
 }

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.

comment:3 by James E. King, III, 5 years ago

Owner: changed from az_sw_dude to James E. King, III
Status: assigned → new

comment:4 by James E. King, III, 5 years ago

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.

I am going to use this issue to resolve all the other /W4 related warnings in the build of Boost.DateTime.

comment:5 by James E. King, III, 5 years ago

Status: new → assigned

comment:7 by James E. King, III, 5 years ago

Milestone: Boost 1.42.0 → Boost 1.67.0
Version: → Boost 1.41.0

comment:8 by James E. King, III, 5 years ago

Resolution: → fixed
Status: assigned → closed

Fix merged to master; resolved.

Note: See TracTickets for help on using tickets.