Opened 11 years ago

Closed 9 years ago

Last modified 9 years ago

#5614 closed Bugs (invalid)

date bug in release (boost 1.46)

Reported by: ludviglarsson@… Owned by: az_sw_dude
Milestone: To Be Determined Component: date_time
Version: Boost 1.46.1 Severity: Problem
Keywords: Cc:

Description

The bug is present only in Release (WinXP + Boost 1.46) not in Debug.

The bug is produced as follows:

#include "boost/date_time/gregorian/gregorian.hpp"
using namespace boost::gregorian;

void func()
{
 date todaysdate(day_clock::local_day());
 printf("%i-%02i-%02i\n",todaysdate.year(), todaysdate.month(), todaysdate.day());
}

This prints gibberish values or more precisely, the lowest 16 bits are correct but the highest 16 bits contain random data.

The date itself seems to work perfectly well, I can compare dates, increment and so on, it is only the year(), month() etc. functions that seems broken.

HTH & keep up the good work!!

Valmond

Change History (3)

comment:1 by anonymous, 11 years ago

Component: Nonedate_time
Owner: set to az_sw_dude

comment:2 by Andrey Semashev, 9 years ago

Resolution: invalid
Status: newclosed

Your use of printf is incorrect. year(), month() and day() methods return class types which have conversion operators to unsigned short. This results in incorrect arguments being passed to printf, which makes the output incorrect.

A good compiler would warn you about such inconsistency, and gcc 4.8 actually does that:

ymd_test.cpp:14:81: warning: format ‘%i’ expects argument of type ‘int’, but argument 2 has type ‘boost::date_time::date<boost::gregorian::date, boost::gregorian::gregorian_calendar, boost::gregorian::date_duration>::year_type {aka boost::gregorian::greg_year}’ [-Wformat=]            
  printf("%i-%02i-%02i\n",todaysdate.year(), todaysdate.month(), todaysdate.day());                                                                                                                                                                                                          
                                                                                 ^                                                                                                                                                                                                           
ymd_test.cpp:14:81: warning: format ‘%i’ expects argument of type ‘int’, but argument 3 has type ‘boost::date_time::date<boost::gregorian::date, boost::gregorian::gregorian_calendar, boost::gregorian::date_duration>::month_type {aka boost::gregorian::greg_month}’ [-Wformat=]          
ymd_test.cpp:14:81: warning: format ‘%i’ expects argument of type ‘int’, but argument 4 has type ‘boost::date_time::date<boost::gregorian::date, boost::gregorian::gregorian_calendar, boost::gregorian::date_duration>::day_type {aka boost::gregorian::greg_day}’ [-Wformat=]              }}}

comment:3 by anonymous, 9 years ago

Except that neither Qt nor MSVC 2010 detects this.

The solution would have been to always truncate the numbers but, hey, no problem. On the other hand, few people uses Boost any more (it's all in 0x11 now) for new projects and as this ticket have been open for years, older projects will have done workarounds and as I said, it doesn't impact new projects.

Cheers

Note: See TracTickets for help on using tickets.