Boost logo

Boost :

From: Stephan T. Lavavej (stl_at_[hidden])
Date: 2003-08-05 19:36:56


In working with my date/time code I've encountered another problem with
Boost. I can reproducibly get lexical_cast to segfault. It happens when a
greg_month is lexical_cast to a string. I don't observe this behavior for
years or days, just months. The segfault can be avoided by using
lexical_cast<string, int> instead of lexical_cast<string>, but this
shouldn't be necessary.

Test case (note that WinXP doesn't complain when programs segfault, hence
the Begin/End business):

crash.cc:

----
#include <boost/lexical_cast.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>
using namespace std;
using namespace boost;
using namespace boost::posix_time;
using namespace boost::gregorian;
int main() {
    const ptime t(date(1969, Jul, 21), time_duration(2, 56, 15));
    cout << "Begin!" << endl;
    cout << lexical_cast<string>(t.date().month()) << endl;
    cout << "End!" << endl;
}
----
WinXP, MinGW, gcc 3.3, Boost 1.30.0:
----
C:\Temp>gpp -Wall -W crash.cc -o crash.exe
C:\Temp>crash
Begin!
C:\Temp>
----
Red Hat 9, gcc 3.3, Boost 1.30.0:
----
[08/05/2003 Tue 05:26.26 PM stl_at_nuwen ~]
> gpp -Wall -W crash.cc -o crash
[08/05/2003 Tue 05:26.39 PM stl_at_nuwen ~]
> crash
Begin!
Segmentation fault
[08/05/2003 Tue 05:26.40 PM stl_at_nuwen ~]
>
----
fine.cc:
----
#include <boost/lexical_cast.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>
using namespace std;
using namespace boost;
using namespace boost::posix_time;
using namespace boost::gregorian;
int main() {
    const ptime t(date(1969, Jul, 21), time_duration(2, 56, 15));
    cout << "Begin!" << endl;
    cout << lexical_cast<string, int>(t.date().month()) << endl;
    cout << "End!" << endl;
}
----
WinXP:
----
C:\Temp>gpp -Wall -W fine.cc -o fine.exe
C:\Temp>fine
Begin!
7
End!
C:\Temp>
----
Red Hat:
----
[08/05/2003 Tue 05:29.03 PM stl_at_nuwen ~]
> gpp -Wall -W fine.cc -o fine
[08/05/2003 Tue 05:29.13 PM stl_at_nuwen ~]
> fine
Begin!
7
End!
[08/05/2003 Tue 05:29.14 PM stl_at_nuwen ~]
>
----
Stephan T. Lavavej
http://stl.caltech.edu

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk