Bug 11838 (boost796799) - boost::lexical_cast segfault
Summary: boost::lexical_cast segfault
Status: RESOLVED INVALID
Alias: boost796799
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 3.3
: P2 normal
Target Milestone: 3.4.0
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2003-08-06 19:50 UTC by Stephan T. Lavavej
Modified: 2005-07-23 22:49 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
3.3 preprocessed source (124.70 KB, application/octet-stream)
2003-08-06 22:16 UTC, Stephan T. Lavavej
Details
3.4 preprocessed source (126.15 KB, application/octet-stream)
2003-08-06 22:16 UTC, Stephan T. Lavavej
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Stephan T. Lavavej 2003-08-06 19:50:00 UTC
crash.cc:
----
#include <boost/lexical_cast.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

int main() {
    boost::lexical_cast<int>(
        boost::gregorian::date(1969, boost::gregorian::Jul, 21).month()
    );
}
----

[08/06/2003 Wed 12:39.25 PM stl@nuwen ~/temp]
> g++ --version
g++ (GCC) 3.3
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


[08/06/2003 Wed 12:39.27 PM stl@nuwen ~/temp]
> g++ -Wall -W crash.cc -o crash

[08/06/2003 Wed 12:39.42 PM stl@nuwen ~/temp]
> crash
Segmentation fault

[08/06/2003 Wed 12:39.54 PM stl@nuwen ~/temp]
>

3.3 and 3.4-20030806 behave identically. I haven't tried any other versions.
MinGW gcc 3.3 on WinXP also produces a segfaulting executable.
The problem does not appear to be on Boost's end; I have been told that 
MSVC.NET 2003 compiles this correctly.

I would submit preprocessed source, but it's about a megabyte. I've tried to 
reduce the test case as much as possible, but it seems that anything more 
eliminates the segfault.

The workaround is to lexical_cast<whatever, int> instead of 
lexical_cast<whatever>. I originally encountered this problem when using 
lexical_cast<string> but I have changed it to lexical_cast<int> as above to 
demonstrate that this is not related to some quirk of string.

Stepping through the program's execution with gdb:
----
(gdb)
0x080497ef in 
boost::date_time::gregorian_calendar_base<boost::date_time::year_month_day_base
<boost::gregorian::greg_year, boost::gregorian::greg_month, 
boost::gregorian::greg_day>, unsigned long>::from_day_number(unsigned long) 
(dayNumber=2440424) at gregorian_calendar.ipp:125
125         return ymd_type(year,month,day);
(gdb)
boost::date_time::date<boost::gregorian::date, 
boost::gregorian::gregorian_calendar, boost::date_time::date_duration<long> 
>::month() const (this=0xbfffdbbc) at date.hpp:81
81            return ymd.month;
(gdb)

Program received signal SIGSEGV, Segmentation fault.
0x08049d56 in std::ios_base::getloc() const (this=0xbfffdb04) at ios_base.h:614
614         getloc() const { return _M_ios_locale; }
(gdb)
----

Sometimes the segfault occurs elsewhere:
----
Program received signal SIGSEGV, Segmentation fault.
0x080498fa in operator<< <char, std::char_traits<char> > (os=@0xbfffe928, 
m=@0xbfffea20) at greg_facet.hpp:58
58          std::locale locale = os.getloc();
----
Comment 1 Andrew Pinski 2003-08-06 20:44:56 UTC
Wee need the preprocessed file as not every one has boost install?
Can you provide both the 3.3 one and the 3.4 one. Use gzip or bzip2 if you want the size 
to go down.
Comment 2 Stephan T. Lavavej 2003-08-06 22:16:24 UTC
Created attachment 4573 [details]
3.3 preprocessed source
Comment 3 Stephan T. Lavavej 2003-08-06 22:16:47 UTC
Created attachment 4574 [details]
3.4 preprocessed source
Comment 4 Andrew Pinski 2003-08-07 00:34:25 UTC
From looking at the code:
  template <class charT, class traits>
  inline
  std::basic_ostream<charT, traits>&
  operator<<(std::basic_ostream<charT, traits>& os, const greg_month& m)
  {
    typedef boost::date_time::ostream_month_formatter<greg_base_facet, charT> 
greg_month_formatter;
    std::locale locale = os.getloc();
    if (std::has_facet<greg_base_facet>(locale)) {
      const greg_base_facet& f = std::use_facet<greg_base_facet>(locale);
      greg_month_formatter::format_month(m, os, f);

    }
    else {
      os << std::setw(2) << std::setfill('0') << m;   //this line
    }

    return os;
  }

The problem there is stack overflow that is causing the seg fault but that is because the 
line marked this line is calling the same function over and over.  I do not know if this is a 
boost problem or libstdc++ problem (becuase I do not like locals at all and I do not know 
them).
Comment 5 Andrew Pinski 2003-08-07 01:59:35 UTC
Marking this as invalid to mark it as ...
Comment 6 Andrew Pinski 2003-08-07 01:59:49 UTC
Unconfirmed.
Comment 7 Andrew Pinski 2003-08-28 16:03:55 UTC
Can a libstdc++ export look at this one because I think boost's code is not doing what they think 
it should do?
Comment 8 Stephan T. Lavavej 2003-08-28 20:16:05 UTC
Is this related?

http://lists.boost.org/MailArchives/boost/msg51572.php

It looks like this may be Boost's fault, not gcc's. But I don't know enough 
about Boost internals or locales to really know.
Comment 9 Andrew Pinski 2003-08-28 20:23:17 UTC
It is the same bug, as I looked at why the seg fault happening before but I still do not know if it is 
boost's bug or ours, GCC.
Comment 10 Stephan T. Lavavej 2003-09-13 21:39:13 UTC
It turns out that this is boost's bug. See:
http://lists.boost.org/MailArchives/boost/msg52323.php

This bug can be closed (as INVALID?). Thanks.
Comment 11 Andrew Pinski 2003-09-14 00:38:51 UTC
Not GCC bug but rather a boost bug.