From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 5307C385380D; Thu, 17 Mar 2022 17:52:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5307C385380D MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-7690] libstdc++: Rewrite __moneypunct_cache::_M_cache [PR104966] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: 1d47c0512a265d4bb3ab9e56259fd1e4f4d42c75 X-Git-Newrev: 38ce4489635f2d65de965af3ec5d5c4adf7762d9 Message-Id: <20220317175200.5307C385380D@sourceware.org> Date: Thu, 17 Mar 2022 17:52:00 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 17 Mar 2022 17:52:00 -0000 https://gcc.gnu.org/g:38ce4489635f2d65de965af3ec5d5c4adf7762d9 commit r12-7690-g38ce4489635f2d65de965af3ec5d5c4adf7762d9 Author: Jonathan Wakely Date: Thu Mar 17 13:33:07 2022 +0000 libstdc++: Rewrite __moneypunct_cache::_M_cache [PR104966] GCC thinks the following can lead to a buffer overflow when __ns.size() equals zero: const basic_string<_CharT>& __ns = __mp.negative_sign(); _M_negative_sign_size = __ns.size(); __negative_sign = new _CharT[_M_negative_sign_size]; __ns.copy(__negative_sign, _M_negative_sign_size); This happens because operator new might be replaced with something that writes to this->_M_negative_sign_size and so the basic_string::copy call could use a non-zero size to write to a zero-length buffer. The solution suggested by Richi is to cache the size in a local variable so that the compiler knows it won't be changed between the allocation and the copy. This commit goes further and rewrites the whole function to use RAII and delay all modifications of *this until after all allocations have succeeded. The RAII helper type caches the size and copies the string and owns the memory until told to release it. libstdc++-v3/ChangeLog: PR middle-end/104966 * include/bits/locale_facets_nonio.tcc (__moneypunct_cache::_M_cache): Replace try-catch with RAII and make all string copies before any stores to *this. Diff: --- libstdc++-v3/include/bits/locale_facets_nonio.tcc | 102 +++++++++++----------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/libstdc++-v3/include/bits/locale_facets_nonio.tcc b/libstdc++-v3/include/bits/locale_facets_nonio.tcc index 98442418f51..8c37a706db8 100644 --- a/libstdc++-v3/include/bits/locale_facets_nonio.tcc +++ b/libstdc++-v3/include/bits/locale_facets_nonio.tcc @@ -71,61 +71,61 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION const moneypunct<_CharT, _Intl>& __mp = use_facet >(__loc); + struct _Scoped_str + { + size_t _M_len; + _CharT* _M_str; + + explicit + _Scoped_str(const basic_string<_CharT>& __str) + : _M_len(__str.size()), _M_str(new _CharT[_M_len]) + { __str.copy(_M_str, _M_len); } + + ~_Scoped_str() { delete[] _M_str; } + + void + _M_release(const _CharT*& __p, size_t& __n) + { + __p = _M_str; + __n = _M_len; + _M_str = 0; + } + }; + + _Scoped_str __curr_symbol(__mp.curr_symbol()); + _Scoped_str __positive_sign(__mp.positive_sign()); + _Scoped_str __negative_sign(__mp.negative_sign()); + + const string& __g = __mp.grouping(); + const size_t __g_size = __g.size(); + char* const __grouping = new char[__g_size]; + __g.copy(__grouping, __g_size); + + // All allocations succeeded without throwing, OK to modify *this now. + + _M_grouping = __grouping; + _M_grouping_size = __g_size; + _M_use_grouping = (__g_size + && static_cast(__grouping[0]) > 0 + && (__grouping[0] + != __gnu_cxx::__numeric_traits::__max)); + _M_decimal_point = __mp.decimal_point(); _M_thousands_sep = __mp.thousands_sep(); + + __curr_symbol._M_release(_M_curr_symbol, _M_curr_symbol_size); + __positive_sign._M_release(_M_positive_sign, _M_positive_sign_size); + __negative_sign._M_release(_M_negative_sign, _M_negative_sign_size); + _M_frac_digits = __mp.frac_digits(); + _M_pos_format = __mp.pos_format(); + _M_neg_format = __mp.neg_format(); - char* __grouping = 0; - _CharT* __curr_symbol = 0; - _CharT* __positive_sign = 0; - _CharT* __negative_sign = 0; - __try - { - const string& __g = __mp.grouping(); - _M_grouping_size = __g.size(); - __grouping = new char[_M_grouping_size]; - __g.copy(__grouping, _M_grouping_size); - _M_use_grouping = (_M_grouping_size - && static_cast(__grouping[0]) > 0 - && (__grouping[0] - != __gnu_cxx::__numeric_traits::__max)); - - const basic_string<_CharT>& __cs = __mp.curr_symbol(); - _M_curr_symbol_size = __cs.size(); - __curr_symbol = new _CharT[_M_curr_symbol_size]; - __cs.copy(__curr_symbol, _M_curr_symbol_size); - - const basic_string<_CharT>& __ps = __mp.positive_sign(); - _M_positive_sign_size = __ps.size(); - __positive_sign = new _CharT[_M_positive_sign_size]; - __ps.copy(__positive_sign, _M_positive_sign_size); - - const basic_string<_CharT>& __ns = __mp.negative_sign(); - _M_negative_sign_size = __ns.size(); - __negative_sign = new _CharT[_M_negative_sign_size]; - __ns.copy(__negative_sign, _M_negative_sign_size); - - _M_pos_format = __mp.pos_format(); - _M_neg_format = __mp.neg_format(); - - const ctype<_CharT>& __ct = use_facet >(__loc); - __ct.widen(money_base::_S_atoms, - money_base::_S_atoms + money_base::_S_end, _M_atoms); - - _M_grouping = __grouping; - _M_curr_symbol = __curr_symbol; - _M_positive_sign = __positive_sign; - _M_negative_sign = __negative_sign; - _M_allocated = true; - } - __catch(...) - { - delete [] __grouping; - delete [] __curr_symbol; - delete [] __positive_sign; - delete [] __negative_sign; - __throw_exception_again; - } + const ctype<_CharT>& __ct = use_facet >(__loc); + __ct.widen(money_base::_S_atoms, + money_base::_S_atoms + money_base::_S_end, _M_atoms); + + _M_allocated = true; } _GLIBCXX_BEGIN_NAMESPACE_LDBL_OR_CXX11