User account creation filtered due to spam.

Bug 12352 - Exception safety problems in src/
Summary: Exception safety problems in src/
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 3.4.0
: P2 normal
Target Milestone: 3.3.3
Assignee: Paolo Carlini
Depends on:
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

Test case (484 bytes, text/plain)
2003-09-21 11:06 UTC, Pétur Runólfsson
Test case (483 bytes, text/plain)
2003-09-21 16:58 UTC, Pétur Runólfsson

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/
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

      	_M_caches = new const facet*[_M_facets_size];
	for (size_t __i = 0; __i < _M_facets_size; ++__i)
	  _M_caches[__i] = 0;
	delete [] _M_caches;

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... 

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 
Comment 5 CVS Commits 2003-09-26 00:22:06 UTC
Subject: Bug 12352

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	2003-09-26 00:22:01

Modified files:
	libstdc++-v3   : ChangeLog 
	libstdc++-v3/include/bits: locale_classes.h 
Added files:

Log message:
	2003-09-25  Paolo Carlini  <>
	PR libstdc++/12352
	* src/ (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/ New, from the PR.
	* src/ (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.


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

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	2003-09-30 07:28:52

Modified files:
	libstdc++-v3   : ChangeLog 

Log message:
	2003-09-30  Paolo Carlini  <>
	PR libstdc++/12352 (cont)
	* src/ (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.


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:	2004-01-19 00:12:55

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

Log message:
	2004-01-19  Paolo Carlini  <>
	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&)):
	* src/ (locale::locale(const char*)): Likewise.
	* src/ (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):