Bug 2020 - fstream initialization seg-faults if global locale is missing required facets
Summary: fstream initialization seg-faults if global locale is missing required facets
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: unknown
: P3 normal
Target Milestone: ---
Assignee: Benjamin Kosnik
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2001-02-18 16:06 UTC by bumgard
Modified: 2020-01-23 12:41 UTC (History)
2 users (show)

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


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description bumgard 2001-02-18 16:06:02 UTC
Creation of an fstream instance produces a segmentation
fault if the global locale does not contain a ctype facet
matching the template parameters of the filebuf 
instantiation.

A bad_cast exception is thrown by a call to use_facet<> 
in the basic_filebuf constructor, and this causes the
as yet, incomplete fstream instance to be destroyed.

The actual fault appears to occur because the ios_base 
destructor calls ios_base::_M_call_callbacks before the
_M_callbacks field has been initialized. This happens
because basic_ios<>, as a virtual base class, is constructed
before basic_ostream's constructor has called its init()
method. This constructor is never executed because the
bad_cast exception is produced while constructing the
filebuf that would be supplied as its streambuf argument.

This problem might also exist for other stream types.

Release:
gcc-3_0-branch

Environment:
Red Hat Linux 2.2.16

How-To-Repeat:
Instantiate a basic_ofstream using a character type other
than char or wchar_t, and construct an instance of that
stream without changing the original global locale.
Comment 1 bumgard 2001-02-18 16:06:02 UTC
Fix:
Not sure. May need to do go ahead and do 
initialization in default constructor.
Comment 2 Benjamin Kosnik 2001-03-12 15:13:06 UTC
From: benjamin kosnik <bkoz@nabi.net>
To: gcc-gnats@gcc.gnu.org, bumgard@roguewave.com, nobody@gcc.gnu.org
Cc:  
Subject: Re: libstdc++/2020
Date: Mon, 12 Mar 2001 15:13:06 -0800

 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view&pr=2020&database=gcc
 
 here's the testcase:
 
 // libstdc++/2020
 
 #include <fstream>
 
 class gnu_char_type
 {
   unsigned long character;
 public:
   // operator ==
   bool
   operator==(const gnu_char_type& __lhs) 
   { return character == __lhs.character; }
 
   // operator <
   bool
   operator<(const gnu_char_type& __lhs) 
   { return character < __lhs.character; }
 
   // to_char_type
   gnu_char_type(const unsigned long& __l) : character(__l) { } 
 
   // to_int_type
   operator unsigned long() const { return character; }
 };
 
 void test01()
 {
   typedef std::basic_filebuf<gnu_char_type> gnu_filebuf;
   gnu_filebuf obj;
 }
 
 int main()
 {
   test01();
   return 0;
 }

Comment 3 Benjamin Kosnik 2001-03-14 12:48:53 UTC
From: benjamin kosnik <bkoz@nabi.net>
To: gcc-gnats@gcc.gnu.org, bumgard@roguewave.com, nobody@gcc.gnu.org
Cc:  
Subject: Re: libstdc++/2020
Date: Wed, 14 Mar 2001 12:48:53 -0800

 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view&pr=2020&database=gcc
 
 2001-03-14  Benjamin Kosnik  <bkoz@redhat.com>
 
 	libstdc++/2020
 	* include/bits/std_streambuf.h: Remove cached locale facets.
 	(basic_streambuf::_M_buf_fctype): Remove.
 	(basic_streambuf::~basic_streambuf): Remove here.
 	(basic_streambuf::basic_streambuf): Same.
 	(basic_streambuf::imbue): Same.
 	* include/bits/fstream.tcc (filebuf::imbue): Remove _M_buf_fctype.
 	* include/bits/std_fstream.h (basic_filebuf::_M_fcvt): Remove.
 	(basic_filebuf::~basic_filebuf()): Remove here.
 	* include/bits/fstream.tcc (basic_filebuf::basic_filebuf): Same.
 	(basic_filebuf::imbue): Same.
 	* include/bits/localefwd.h (_Count_ones): Remove.
 	(locale::_S_num_categories): Just use 6, since this doesn't
 	actually change, ever.
 	* include/bits/locale_facets.tcc (has_facet): Simplify.
 	(use_facet): Same.
 	* testsuite/27_io/filebuf.cc (test06): Add tests.
 	
 
 Fixed.
 
 Please read the commit message here:
 
 http://gcc.gnu.org/ml/gcc-patches/2001-03/msg00977.html
 
 
 This is one more step on getting codecvt working in filebufs, more
 necessary.
 
 Which of your bugs are still outstanding?
 
 -benjamin
Comment 4 Benjamin Kosnik 2001-03-14 13:35:52 UTC
Responsible-Changed-From-To: unassigned->bkoz
Responsible-Changed-Why: Fixed
Comment 5 Benjamin Kosnik 2001-03-14 13:35:52 UTC
State-Changed-From-To: open->closed
State-Changed-Why: Fixed
Comment 6 Benjamin Kosnik 2001-03-14 21:35:52 UTC
From: bkoz@gcc.gnu.org
To: bkoz@gcc.gnu.org, bumgard@roguewave.com, gcc-gnats@gcc.gnu.org,
  nobody@gcc.gnu.org
Cc:  
Subject: Re: libstdc++/2020
Date: 14 Mar 2001 21:35:52 -0000

 Synopsis: fstream initialization seg-faults if global locale is missing required facets
 
 Responsible-Changed-From-To: unassigned->bkoz
 Responsible-Changed-By: bkoz
 Responsible-Changed-When: Wed Mar 14 13:35:52 2001
 Responsible-Changed-Why:
     Fixed
 State-Changed-From-To: open->closed
 State-Changed-By: bkoz
 State-Changed-When: Wed Mar 14 13:35:52 2001
 State-Changed-Why:
     Fixed
 
 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view&pr=2020&database=gcc
Comment 7 GCC Commits 2020-01-23 12:41:07 UTC Comment hidden (obsolete)