Commit af90c8c9 by Paolo Carlini Committed by Paolo Carlini

re PR libstdc++/40712 (locale(const locale&, const char*, locale::category) can…

re PR libstdc++/40712 (locale(const locale&, const char*, locale::category) can create broken locale)

2009-07-18  Paolo Carlini  <paolo.carlini@oracle.com>

	PR libstdc++/40712
	* config/locale/gnu/numeric_members.cc (numpunct<>::
	_M_initialize_numpunct): Dynamically allocate _M_data->_M_grouping
	and copy the langinfo data into it.
	(numpunct<>::~numpunct): Free the allocated memory.
	* config/locale/gnu/monetary_members.cc (moneypunct<>::
	_M_initialize_moneypunct): Dynamically allocate _M_data->_M_grouping,
	_M_data->_M_positive_sign, _M_data->_M_negative_sign,
	_M_data->_M_curr_symbol.
	(moneypunct<>::~moneypunct): Free the allocated memory.
	* testsuite/22_locale/moneypunct/40712.cc: New.

	* include/bits/locale_facets.tcc (__numpunct_cache<>::_M_cache):
	Do not leak memory if new throws.
	* include/bits/locale_facets_nonio.tcc
	(__moneypunct_cache<>::_M_cache): Likewise.

From-SVN: r149782
parent 827f4079
2009-07-18 Paolo Carlini <paolo.carlini@oracle.com>
PR libstdc++/40712
* config/locale/gnu/numeric_members.cc (numpunct<>::
_M_initialize_numpunct): Dynamically allocate _M_data->_M_grouping
and copy the langinfo data into it.
(numpunct<>::~numpunct): Free the allocated memory.
* config/locale/gnu/monetary_members.cc (moneypunct<>::
_M_initialize_moneypunct): Dynamically allocate _M_data->_M_grouping,
_M_data->_M_positive_sign, _M_data->_M_negative_sign,
_M_data->_M_curr_symbol.
(moneypunct<>::~moneypunct): Free the allocated memory.
* testsuite/22_locale/moneypunct/40712.cc: New.
* include/bits/locale_facets.tcc (__numpunct_cache<>::_M_cache):
Do not leak memory if new throws.
* include/bits/locale_facets_nonio.tcc
(__moneypunct_cache<>::_M_cache): Likewise.
2009-07-17 Joseph Myers <joseph@codesourcery.com>
PR other/40784
......
......@@ -76,8 +76,29 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
}
else
{
_M_data->_M_grouping = __nl_langinfo_l(GROUPING, __cloc);
_M_data->_M_grouping_size = strlen(_M_data->_M_grouping);
const char* __src = __nl_langinfo_l(GROUPING, __cloc);
const size_t __len = strlen(__src);
if (__len)
{
__try
{
char* __dst = new char[__len + 1];
memcpy(__dst, __src, __len + 1);
_M_data->_M_grouping = __dst;
}
__catch(...)
{
delete _M_data;
_M_data = 0;
__throw_exception_again;
}
}
else
{
_M_data->_M_grouping = "";
_M_data->_M_use_grouping = false;
}
_M_data->_M_grouping_size = __len;
}
}
......@@ -92,7 +113,11 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
template<>
numpunct<char>::~numpunct()
{ delete _M_data; }
{
if (_M_data->_M_grouping_size)
delete [] _M_data->_M_grouping;
delete _M_data;
}
#ifdef _GLIBCXX_USE_WCHAR_T
template<>
......@@ -143,8 +168,29 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
}
else
{
_M_data->_M_grouping = __nl_langinfo_l(GROUPING, __cloc);
_M_data->_M_grouping_size = strlen(_M_data->_M_grouping);
const char* __src = __nl_langinfo_l(GROUPING, __cloc);
const size_t __len = strlen(__src);
if (__len)
{
__try
{
char* __dst = new char[__len + 1];
memcpy(__dst, __src, __len + 1);
_M_data->_M_grouping = __dst;
}
__catch(...)
{
delete _M_data;
_M_data = 0;
__throw_exception_again;
}
}
else
{
_M_data->_M_grouping = "";
_M_data->_M_use_grouping = false;
}
_M_data->_M_grouping_size = __len;
}
}
......@@ -159,7 +205,11 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
template<>
numpunct<wchar_t>::~numpunct()
{ delete _M_data; }
{
if (_M_data->_M_grouping_size)
delete [] _M_data->_M_grouping;
delete _M_data;
}
#endif
_GLIBCXX_END_NAMESPACE
......@@ -81,33 +81,48 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
_M_grouping_size = __np.grouping().size();
char* __grouping = new char[_M_grouping_size];
__np.grouping().copy(__grouping, _M_grouping_size);
_M_grouping = __grouping;
_M_use_grouping = (_M_grouping_size
&& static_cast<signed char>(_M_grouping[0]) > 0
&& (_M_grouping[0]
!= __gnu_cxx::__numeric_traits<char>::__max));
_M_truename_size = __np.truename().size();
_CharT* __truename = new _CharT[_M_truename_size];
__np.truename().copy(__truename, _M_truename_size);
_M_truename = __truename;
_M_falsename_size = __np.falsename().size();
_CharT* __falsename = new _CharT[_M_falsename_size];
__np.falsename().copy(__falsename, _M_falsename_size);
_M_falsename = __falsename;
_M_decimal_point = __np.decimal_point();
_M_thousands_sep = __np.thousands_sep();
const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__loc);
__ct.widen(__num_base::_S_atoms_out,
__num_base::_S_atoms_out + __num_base::_S_oend, _M_atoms_out);
__ct.widen(__num_base::_S_atoms_in,
__num_base::_S_atoms_in + __num_base::_S_iend, _M_atoms_in);
char* __grouping = 0;
_CharT* __truename = 0;
_CharT* __falsename = 0;
__try
{
_M_grouping_size = __np.grouping().size();
__grouping = new char[_M_grouping_size];
__np.grouping().copy(__grouping, _M_grouping_size);
_M_grouping = __grouping;
_M_use_grouping = (_M_grouping_size
&& static_cast<signed char>(_M_grouping[0]) > 0
&& (_M_grouping[0]
!= __gnu_cxx::__numeric_traits<char>::__max));
_M_truename_size = __np.truename().size();
__truename = new _CharT[_M_truename_size];
__np.truename().copy(__truename, _M_truename_size);
_M_truename = __truename;
_M_falsename_size = __np.falsename().size();
__falsename = new _CharT[_M_falsename_size];
__np.falsename().copy(__falsename, _M_falsename_size);
_M_falsename = __falsename;
_M_decimal_point = __np.decimal_point();
_M_thousands_sep = __np.thousands_sep();
const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__loc);
__ct.widen(__num_base::_S_atoms_out,
__num_base::_S_atoms_out
+ __num_base::_S_oend, _M_atoms_out);
__ct.widen(__num_base::_S_atoms_in,
__num_base::_S_atoms_in
+ __num_base::_S_iend, _M_atoms_in);
}
__catch(...)
{
delete [] __grouping;
delete [] __truename;
delete [] __falsename;
__throw_exception_again;
}
}
// Used by both numeric and monetary facets.
......
......@@ -71,40 +71,55 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
const moneypunct<_CharT, _Intl>& __mp =
use_facet<moneypunct<_CharT, _Intl> >(__loc);
_M_grouping_size = __mp.grouping().size();
char* __grouping = new char[_M_grouping_size];
__mp.grouping().copy(__grouping, _M_grouping_size);
_M_grouping = __grouping;
_M_use_grouping = (_M_grouping_size
&& static_cast<signed char>(_M_grouping[0]) > 0
&& (_M_grouping[0]
!= __gnu_cxx::__numeric_traits<char>::__max));
_M_decimal_point = __mp.decimal_point();
_M_thousands_sep = __mp.thousands_sep();
_M_frac_digits = __mp.frac_digits();
_M_curr_symbol_size = __mp.curr_symbol().size();
_CharT* __curr_symbol = new _CharT[_M_curr_symbol_size];
__mp.curr_symbol().copy(__curr_symbol, _M_curr_symbol_size);
_M_curr_symbol = __curr_symbol;
_M_positive_sign_size = __mp.positive_sign().size();
_CharT* __positive_sign = new _CharT[_M_positive_sign_size];
__mp.positive_sign().copy(__positive_sign, _M_positive_sign_size);
_M_positive_sign = __positive_sign;
_M_negative_sign_size = __mp.negative_sign().size();
_CharT* __negative_sign = new _CharT[_M_negative_sign_size];
__mp.negative_sign().copy(__negative_sign, _M_negative_sign_size);
_M_negative_sign = __negative_sign;
_M_pos_format = __mp.pos_format();
_M_neg_format = __mp.neg_format();
const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__loc);
__ct.widen(money_base::_S_atoms,
money_base::_S_atoms + money_base::_S_end, _M_atoms);
char* __grouping = 0;
_CharT* __curr_symbol = 0;
_CharT* __positive_sign = 0;
_CharT* __negative_sign = 0;
__try
{
_M_grouping_size = __mp.grouping().size();
__grouping = new char[_M_grouping_size];
__mp.grouping().copy(__grouping, _M_grouping_size);
_M_grouping = __grouping;
_M_use_grouping = (_M_grouping_size
&& static_cast<signed char>(_M_grouping[0]) > 0
&& (_M_grouping[0]
!= __gnu_cxx::__numeric_traits<char>::__max));
_M_curr_symbol_size = __mp.curr_symbol().size();
__curr_symbol = new _CharT[_M_curr_symbol_size];
__mp.curr_symbol().copy(__curr_symbol, _M_curr_symbol_size);
_M_curr_symbol = __curr_symbol;
_M_positive_sign_size = __mp.positive_sign().size();
__positive_sign = new _CharT[_M_positive_sign_size];
__mp.positive_sign().copy(__positive_sign, _M_positive_sign_size);
_M_positive_sign = __positive_sign;
_M_negative_sign_size = __mp.negative_sign().size();
__negative_sign = new _CharT[_M_negative_sign_size];
__mp.negative_sign().copy(__negative_sign, _M_negative_sign_size);
_M_negative_sign = __negative_sign;
_M_pos_format = __mp.pos_format();
_M_neg_format = __mp.neg_format();
const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__loc);
__ct.widen(money_base::_S_atoms,
money_base::_S_atoms + money_base::_S_end, _M_atoms);
}
__catch(...)
{
delete [] __grouping;
delete [] __curr_symbol;
delete [] __positive_sign;
delete [] __negative_sign;
__throw_exception_again;
}
}
_GLIBCXX_BEGIN_LDBL_NAMESPACE
......
// { dg-require-namedlocale "" }
// 2009-07-18 Paolo Carlini <paolo.carlini@oracle.com>
// Copyright (C) 2009 Free Software Foundation
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// 22.2.6.3 Template class moneypunct
#include <locale>
// libstdc++/40712
void test01()
{
bool test __attribute__((unused)) = true;
using namespace std;
locale loc(locale("C"), "en_US", locale::monetary);
use_facet<moneypunct<char> >(loc).grouping();
}
int main()
{
test01();
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment