public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: Jonathan Wakely <jwakely@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:05:00 -0000	[thread overview]
Message-ID: <20150929152515.GJ1847@tucnak.redhat.com> (raw)
In-Reply-To: <20150929151541.GZ12094@redhat.com>

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

	Jakub

  reply	other threads:[~2015-09-29 15:25 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 [this message]
2015-09-29 16:25   ` Jonathan Wakely
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=20150929152515.GJ1847@tucnak.redhat.com \
    --to=jakub@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jwakely@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).