Bug 12352 - Exception safety problems in src/localename.cc
Exception safety problems in src/localename.cc
Status: RESOLVED FIXED
Product: gcc
Classification: Unclassified
Component: libstdc++
3.4.0
: P2 normal
: 3.3.3
Assigned To: Paolo Carlini
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2003-09-21 11:05 UTC by Pétur Runólfsson
Modified: 2004-01-19 00:14 UTC (History)
1 user (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu
Known to work:
Known to fail:
Last reconfirmed: 2003-09-21 20:51:12


Attachments
Test case (1.43 KB, text/plain)
2003-09-21 11:06 UTC, Pétur Runólfsson
Details
Test case (1.52 KB, text/plain)
2003-09-21 16:58 UTC, Pétur Runólfsson
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Pétur Runólfsson 2003-09-21 11:05:26 UTC
There are numerous exception safety problems in the file src/localename.cc
in the functions

locale::_Impl::_Impl(const _Impl& __imp, size_t __refs)
locale::_Impl::_Impl(const char* __s, size_t __refs) 
void locale::_Impl::_M_replace_categories(const _Impl* __imp, category __cat)
void locale::_Impl::_M_install_facet(const locale::id* __idp, const facet* __fp)

For example, the construct

    try 
      {
      	_M_caches = new const facet*[_M_facets_size];
	for (size_t __i = 0; __i < _M_facets_size; ++__i)
	  _M_caches[__i] = 0;
      }
    catch(...)
      {
	delete [] _M_caches;
	__throw_exception_again;
      }

appears in both constructors. There are several problems with this code.
1) _M_caches is uninitialized before entering the try block, and the
only function that can throw is the call to operator new, so _M_caches is
always uninitialized when the delete[] statement is executed.
2) Resources allocated before entering the try block are leaked.
Comment 1 Pétur Runólfsson 2003-09-21 11:06:27 UTC
Created attachment 4790 [details]
Test case
Comment 2 Paolo Carlini 2003-09-21 14:08:55 UTC
Hi Pétur. Indeed, your PR seems correct, but I cannot effectively make use of
the testcase: it never fails at runtime whichever value of iters (in the range
1-1000). Sorry if I'm missing something naive... 

Thanks,
Paolo.
Comment 3 Pétur Runólfsson 2003-09-21 16:58:25 UTC
Created attachment 4792 [details]
Test case

> it never fails at runtime

Possibly because the code to make operator new fail was commented out:
	/*
	if (!times_to_fail--)
		return 0;
	*/
Sorry about the confusion :-(
Comment 4 Andrew Pinski 2003-09-21 20:51:11 UTC
Moving it to confirmed based on Paolo Carlini's analysis and that Pétur gave a correct test case this 
time.
Comment 5 CVS Commits 2003-09-26 00:22:06 UTC
Subject: Bug 12352

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	paolo@gcc.gnu.org	2003-09-26 00:22:01

Modified files:
	libstdc++-v3   : ChangeLog 
	libstdc++-v3/src: localename.cc 
	libstdc++-v3/include/bits: locale_classes.h 
Added files:
	libstdc++-v3/testsuite/22_locale/locale/cons: 12352.cc 

Log message:
	2003-09-25  Paolo Carlini  <pcarlini@unitus.it>
	
	PR libstdc++/12352
	* src/localename.cc (locale::_Impl::_Impl(const _Impl&,
	size_t)): Don't leak if memory allocations for _M_facets,
	_M_caches, and _M_names fail.
	(locale::_Impl::_Impl(const char*, size_t)): Ditto.
	(locale::_Impl::_M_replace_categories(const _Impl*,
	category)): Ditto.
	(locale::_Impl::_M_install_facet(const locale::id*,
	const facet*)): Ditto.
	* include/bits/locale_classes.h (locale::locale(const locale&,
	_Facet*)): Don't leak memory.
	* testsuite/22_locale/locale/cons/12352.cc: New, from the PR.
	
	* src/localename.cc (locale::_Impl::_Impl(facet**, size_t,
	bool)): Qualify with std:: strcpy, tweak.
	* include/bits/locale_classes.h
	(locale::_Impl::_M_check_same_name): Qualify strcmp.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/ChangeLog.diff?cvsroot=gcc&r1=1.1969&r2=1.1970
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/src/localename.cc.diff?cvsroot=gcc&r1=1.43&r2=1.44
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/include/bits/locale_classes.h.diff?cvsroot=gcc&r1=1.10&r2=1.11
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/testsuite/22_locale/locale/cons/12352.cc.diff?cvsroot=gcc&r1=NONE&r2=1.1

Comment 6 Paolo Carlini 2003-09-26 00:27:53 UTC
Fixed.
Comment 7 CVS Commits 2003-09-30 07:28:59 UTC
Subject: Bug 12352

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	paolo@gcc.gnu.org	2003-09-30 07:28:52

Modified files:
	libstdc++-v3   : ChangeLog 
	libstdc++-v3/src: localename.cc 

Log message:
	2003-09-30  Paolo Carlini  <pcarlini@unitus.it>
	
	PR libstdc++/12352 (cont)
	* src/localename.cc (locale::_Impl::_Impl(const char*, size_t)):
	Don't leak __cloc; don't leak if any of the _M_init_facet(...)
	calls fail.
	(locale::_Impl::_Impl(const _Impl&, size_t)): Tweak.
	(locale::_Impl::~_Impl): Don't do anything if !_M_facets,
	!_M_caches, !_M_names.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/ChangeLog.diff?cvsroot=gcc&r1=1.1976&r2=1.1977
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/src/localename.cc.diff?cvsroot=gcc&r1=1.44&r2=1.45

Comment 8 CVS Commits 2004-01-19 00:13:00 UTC
Subject: Bug 12352

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	gcc-3_3-branch
Changes by:	paolo@gcc.gnu.org	2004-01-19 00:12:55

Modified files:
	libstdc++-v3   : ChangeLog 
	libstdc++-v3/include/bits: locale_classes.h locale_facets.tcc 
	libstdc++-v3/src: locale.cc localename.cc 

Log message:
	2004-01-19  Paolo Carlini  <pcarlini@suse.de>
	
	PR libstdc++/12352
	PR libstdc++/12438
	* include/bits/locale_classes.h (locale::locale(const locale&,
	_Facet*)): Don't leak memory on exception.
	* include/bits/locale_facets.tcc (locale::combine(const locale&)):
	Likewise.
	* src/locale.cc (locale::locale(const char*)): Likewise.
	* src/localename.cc (locale::_Impl::~_Impl(),
	locale::_Impl::_Impl(const _Impl&, size_t),
	locale::_Impl::_Impl(const char*, size_t)): Likewise.
	(locale::_Impl::_M_replace_categories(const _Impl*, category):
	Tweak.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.1464.2.165&r2=1.1464.2.166
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/include/bits/locale_classes.h.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.1.2.3&r2=1.1.2.4
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/include/bits/locale_facets.tcc.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.82.4.14&r2=1.82.4.15
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/src/locale.cc.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.72.2.4&r2=1.72.2.5
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/src/localename.cc.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.33.2.4&r2=1.33.2.5