From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [committed] libstdc++: Avoid bad_alloc exceptions when changing locales
Date: Tue, 10 Nov 2020 23:50:24 +0000 [thread overview]
Message-ID: <20201110235024.GA4798@redhat.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 547 bytes --]
For the --enable-clocale=generic configuration, the current code can
fail with a bad_alloc exception. This patch uses the nothrow version of
operator new and reports allocation failures by setting failbit in the
iostate variable.
* config/locale/generic/c_locale.cc (__set_C_locale()): New function
to set the "C" locale and return the name of the previous locale.
(__convert_to_v<float>, __convert_to_v<double>)
(__convert_to_v<long double>): Use __set_C_locale and set failbit on
error.
Tested sparc-sun-solaris2.11. Committed to trunk.
[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 3199 bytes --]
commit 5dfbc52264fc64db22e75f385be9efae3d0eba46
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Tue Nov 10 19:23:15 2020
libstdc++: Avoid bad_alloc exceptions when changing locales
For the --enable-clocale=generic configuration, the current code can
fail with a bad_alloc exception. This patch uses the nothrow version of
operator new and reports allocation failures by setting failbit in the
iostate variable.
* config/locale/generic/c_locale.cc (__set_C_locale()): New function
to set the "C" locale and return the name of the previous locale.
(__convert_to_v<float>, __convert_to_v<double>)
(__convert_to_v<long double>): Use __set_C_locale and set failbit on
error.
diff --git a/libstdc++-v3/config/locale/generic/c_locale.cc b/libstdc++-v3/config/locale/generic/c_locale.cc
index 61ead91ec1ed..648b8e182cc7 100644
--- a/libstdc++-v3/config/locale/generic/c_locale.cc
+++ b/libstdc++-v3/config/locale/generic/c_locale.cc
@@ -52,6 +52,22 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
~_Save_errno() { if (errno == 0) errno = _M_errno; }
int _M_errno;
};
+
+ // calls setlocale(LC_ALL, "C") and returns a string containing the old
+ // locale name. Caller must delete[] the string. Returns NULL on error.
+ const char*
+ __set_C_locale()
+ {
+ char* __old = setlocale(LC_ALL, 0);
+ const size_t __len = strlen(__old) + 1;
+ char* __sav = new(nothrow) char[__len];
+ if (__sav)
+ {
+ memcpy(__sav, __old, __len);
+ setlocale(LC_ALL, "C");
+ }
+ return __sav;
+ }
}
template<>
@@ -60,11 +76,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
const __c_locale&) throw()
{
// Assumes __s formatted for "C" locale.
- char* __old = setlocale(LC_ALL, 0);
- const size_t __len = strlen(__old) + 1;
- char* __sav = new char[__len];
- memcpy(__sav, __old, __len);
- setlocale(LC_ALL, "C");
+ const char* __sav = __set_C_locale();
+ if (!__sav)
+ {
+ __err = ios_base::failbit;
+ return;
+ }
char* __sanity;
bool __overflow = false;
@@ -125,11 +142,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
const __c_locale&) throw()
{
// Assumes __s formatted for "C" locale.
- char* __old = setlocale(LC_ALL, 0);
- const size_t __len = strlen(__old) + 1;
- char* __sav = new char[__len];
- memcpy(__sav, __old, __len);
- setlocale(LC_ALL, "C");
+ const char* __sav = __set_C_locale();
+ if (!__sav)
+ {
+ __err = ios_base::failbit;
+ return;
+ }
char* __sanity;
#if !__DBL_HAS_INFINITY__
@@ -170,11 +188,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
ios_base::iostate& __err, const __c_locale&) throw()
{
// Assumes __s formatted for "C" locale.
- char* __old = setlocale(LC_ALL, 0);
- const size_t __len = strlen(__old) + 1;
- char* __sav = new char[__len];
- memcpy(__sav, __old, __len);
- setlocale(LC_ALL, "C");
+ const char* __sav = __set_C_locale();
+ if (!__sav)
+ {
+ __err = ios_base::failbit;
+ return;
+ }
#if !__LDBL_HAS_INFINITY__
const _Save_errno __save_errno;
reply other threads:[~2020-11-10 23:50 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20201110235024.GA4798@redhat.com \
--to=jwakely@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=libstdc++@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).