public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: Jakub Jelinek <jakub@redhat.com>
Cc: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: Re: [patch] Leave errno unchanged by successful std::stoi etc
Date: Tue, 29 Sep 2015 16:25:00 -0000	[thread overview]
Message-ID: <20150929161020.GA12094@redhat.com> (raw)
In-Reply-To: <20150929152515.GJ1847@tucnak.redhat.com>

[-- Attachment #1: Type: text/plain, Size: 2158 bytes --]

On 29/09/15 17:25 +0200, Jakub Jelinek wrote:
>On Tue, Sep 29, 2015 at 04:15:41PM +0100, Jonathan Wakely wrote:
>> We set errno=0 in __gnu_cxx::__stoa in order to reliably detect when
>> it gets set to ERANGE. This restores the previous value when the
>> conversion is successful.
>>
>> Tested powerpc64le-linux, committed to trunk.
>
>> commit 412f75dc37b1048e14996c9caafa46c00db8eb30
>> Author: Jonathan Wakely <jwakely@redhat.com>
>> Date:   Tue Sep 29 15:09:23 2015 +0100
>>
>>     Leave errno unchanged by successful std::stoi etc
>>
>>     	* include/ext/string_conversions.h (__stoa): Save and restore errno.
>>     	* testsuite/21_strings/basic_string/numeric_conversions/char/errno.cc:
>>     	New.
>>
>> diff --git a/libstdc++-v3/include/ext/string_conversions.h b/libstdc++-v3/include/ext/string_conversions.h
>> index f4648a8..58387a2 100644
>> --- a/libstdc++-v3/include/ext/string_conversions.h
>> +++ b/libstdc++-v3/include/ext/string_conversions.h
>> @@ -58,6 +58,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>>        _Ret __ret;
>>
>>        _CharT* __endptr;
>> +      const int __saved_errno = errno;
>>        errno = 0;
>>        const _TRet __tmp = __convf(__str, &__endptr, __base...);
>>
>> @@ -70,6 +71,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>>  	std::__throw_out_of_range(__name);
>>        else
>>  	__ret = __tmp;
>> +      errno = __saved_errno;
>
>That looks wrong to me, you only restore errno if you don't throw :(.
>If you throw, then errno might remain 0, which is IMHO undesirable.

My thinking was that a failed conversion that throws an exception
should be allowed to modify errno, and that the second case sets it to
ERANGE sometimes anyway.

But I suppose it would be better to consistently set it to non-zero
when an exception is thrown, or consistently restore the original
value in all cases.

>So, I'd say you want to restore it earlier, right after __convf, and
>immediately before that copy the current errno to some other temporary
>for the use in the condition?  Or restore errno = __saved_errno;
>in all the 3 spots instead of just one.

Or in a destructor so it happens however we exit the function, like
this ...



[-- Attachment #2: patch.txt --]
[-- Type: text/x-patch, Size: 867 bytes --]

diff --git a/libstdc++-v3/include/ext/string_conversions.h b/libstdc++-v3/include/ext/string_conversions.h
index 58387a2..3b62c9a 100644
--- a/libstdc++-v3/include/ext/string_conversions.h
+++ b/libstdc++-v3/include/ext/string_conversions.h
@@ -58,8 +58,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       _Ret __ret;
 
       _CharT* __endptr;
-      const int __saved_errno = errno;
-      errno = 0;
+
+      struct _Restore_errno {
+	  _Restore_errno() : _M_errno(errno) { errno = 0; }
+	  ~_Restore_errno() { errno = _M_errno; }
+	  int _M_errno;
+      } const __restore;
+
       const _TRet __tmp = __convf(__str, &__endptr, __base...);
 
       if (__endptr == __str)
@@ -71,7 +76,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	std::__throw_out_of_range(__name);
       else
 	__ret = __tmp;
-      errno = __saved_errno;
 
       if (__idx)
 	*__idx = __endptr - __str;

  reply	other threads:[~2015-09-29 16:10 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-29 15:55 Jonathan Wakely
2015-09-29 16:05 ` Jakub Jelinek
2015-09-29 16:25   ` Jonathan Wakely [this message]
2015-09-29 16:26     ` Jakub Jelinek
2015-09-29 17:28       ` Martin Sebor
2015-09-30 13:45         ` Jonathan Wakely
2015-10-01 11:24           ` Jonathan Wakely

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=20150929161020.GA12094@redhat.com \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jakub@redhat.com \
    --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).