public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [patch] Leave errno unchanged by successful std::stoi etc
@ 2015-09-29 15:55 Jonathan Wakely
  2015-09-29 16:05 ` Jakub Jelinek
  0 siblings, 1 reply; 7+ messages in thread
From: Jonathan Wakely @ 2015-09-29 15:55 UTC (permalink / raw)
  To: libstdc++, gcc-patches

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

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.

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

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;
 
       if (__idx)
 	*__idx = __endptr - __str;
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/numeric_conversions/char/errno.cc b/libstdc++-v3/testsuite/21_strings/basic_string/numeric_conversions/char/errno.cc
new file mode 100644
index 0000000..4079744
--- /dev/null
+++ b/libstdc++-v3/testsuite/21_strings/basic_string/numeric_conversions/char/errno.cc
@@ -0,0 +1,36 @@
+// Copyright (C) 2015 Free Software Foundation, Inc.
+//
+// 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/>.
+
+// { dg-options "-std=gnu++11" }
+// { dg-require-string-conversions "" }
+
+#include <string>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+  errno = ERANGE;
+  std::stoi("42");
+  VERIFY( errno == ERANGE ); // errno should not be altered by successful call
+}
+
+int
+main()
+{
+  test01();
+}

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2015-10-01 11:24 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-09-29 15:55 [patch] Leave errno unchanged by successful std::stoi etc Jonathan Wakely
2015-09-29 16:05 ` Jakub Jelinek
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

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