This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[v3] Fix libstdc++/39802


Hi,

tested x86_64-linux multilib, committed to mainline (will go in 4.4.1 too).

Paolo.

////////////////////
2009-04-18  Paolo Carlini  <paolo.carlini@oracle.com>

	PR libstdc++/39802
	* include/bits/locale_facets.tcc (num_get<>::_M_extract_int
	(_InIter, _InIter, ios_base&, ios_base::iostate&, _ValueT&)):
	Always accept negative values, for unsigned types too.
	* testsuite/22_locale/num_get/get/char/39802.cc: New.
	* testsuite/22_locale/num_get/get/wchar_t/39802.cc: Likewise.
Index: include/bits/locale_facets.tcc
===================================================================
--- include/bits/locale_facets.tcc	(revision 146308)
+++ include/bits/locale_facets.tcc	(working copy)
@@ -379,8 +379,7 @@
 	if (!__testeof)
 	  {
 	    __c = *__beg;
-	    if (__gnu_cxx::__numeric_traits<_ValueT>::__is_signed)
-	      __negative = __c == __lit[__num_base::_S_iminus];
+	    __negative = __c == __lit[__num_base::_S_iminus];
 	    if ((__negative || __c == __lit[__num_base::_S_iplus])
 		&& !(__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
 		&& !(__c == __lc->_M_decimal_point))
@@ -449,7 +448,8 @@
 	  __found_grouping.reserve(32);
 	bool __testfail = false;
 	bool __testoverflow = false;
-	const __unsigned_type __max = __negative
+	const __unsigned_type __max =
+	  (__negative && __gnu_cxx::__numeric_traits<_ValueT>::__is_signed)
 	  ? -__gnu_cxx::__numeric_traits<_ValueT>::__min
 	  : __gnu_cxx::__numeric_traits<_ValueT>::__max;
 	const __unsigned_type __smax = __max / __base;
@@ -552,7 +552,8 @@
 	  }
 	else if (__testoverflow)
 	  {
-	    if (__negative)
+	    if (__negative
+		&& __gnu_cxx::__numeric_traits<_ValueT>::__is_signed)
 	      __v = __gnu_cxx::__numeric_traits<_ValueT>::__min;
 	    else
 	      __v = __gnu_cxx::__numeric_traits<_ValueT>::__max;
Index: testsuite/22_locale/num_get/get/wchar_t/39802.cc
===================================================================
--- testsuite/22_locale/num_get/get/wchar_t/39802.cc	(revision 0)
+++ testsuite/22_locale/num_get/get/wchar_t/39802.cc	(revision 0)
@@ -0,0 +1,77 @@
+// Copyright (C) 2009 Free Software Foundation
+//
+// 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/>.
+
+// 22.2.2.1.1  num_get members
+
+#include <locale>
+#include <sstream>
+#include <limits>
+#include <testsuite_hooks.h>
+
+// libstdc++/39802
+void test01()
+{
+  using namespace std;
+  typedef istreambuf_iterator<wchar_t> iterator_type;
+  
+  bool test __attribute__((unused)) = true;
+
+  wstringstream ss;
+  const num_get<wchar_t>& ng = use_facet<num_get<wchar_t> >(ss.getloc()); 
+  ios_base::iostate err;
+  iterator_type end;
+  const wstring empty;
+
+  unsigned long ul0 = 1;
+  const unsigned long ul1 = numeric_limits<unsigned long>::max();
+
+  ss << L"-0";
+  err = ios_base::goodbit;
+  end = ng.get(ss.rdbuf(), 0, ss, err, ul0);
+  VERIFY( err == ios_base::eofbit );
+  VERIFY( ul0 == 0 );
+
+  ss.clear();
+  ss.str(empty);
+  ss << L"-1";
+  err = ios_base::goodbit;
+  end = ng.get(ss.rdbuf(), 0, ss, err, ul0);
+  VERIFY( err == ios_base::eofbit );
+  VERIFY( ul0 == ul1 );
+
+  ss.clear();
+  ss.str(empty);
+  ss << L'-' << ul1;
+  err = ios_base::goodbit;
+  end = ng.get(ss.rdbuf(), 0, ss, err, ul0);
+  VERIFY( err == ios_base::eofbit );
+  VERIFY( ul0 == 1 );
+
+  ss.clear();
+  ss.str(empty);
+  ss << L'-' << ul1 << L'0';
+  err = ios_base::goodbit;
+  end = ng.get(ss.rdbuf(), 0, ss, err, ul0);
+  VERIFY( err == (ios_base::eofbit | ios_base::failbit) );
+  VERIFY( ul0 == ul1 );
+}
+
+int main()
+{
+  test01();
+  return 0;
+}
Index: testsuite/22_locale/num_get/get/char/39802.cc
===================================================================
--- testsuite/22_locale/num_get/get/char/39802.cc	(revision 0)
+++ testsuite/22_locale/num_get/get/char/39802.cc	(revision 0)
@@ -0,0 +1,77 @@
+// Copyright (C) 2009 Free Software Foundation
+//
+// 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/>.
+
+// 22.2.2.1.1  num_get members
+
+#include <locale>
+#include <sstream>
+#include <limits>
+#include <testsuite_hooks.h>
+
+// libstdc++/39802
+void test01()
+{
+  using namespace std;
+  typedef istreambuf_iterator<char> iterator_type;
+  
+  bool test __attribute__((unused)) = true;
+
+  stringstream ss;
+  const num_get<char>& ng = use_facet<num_get<char> >(ss.getloc()); 
+  ios_base::iostate err;
+  iterator_type end;
+  const string empty;
+
+  unsigned long ul0 = 1;
+  const unsigned long ul1 = numeric_limits<unsigned long>::max();
+
+  ss << "-0";
+  err = ios_base::goodbit;
+  end = ng.get(ss.rdbuf(), 0, ss, err, ul0);
+  VERIFY( err == ios_base::eofbit );
+  VERIFY( ul0 == 0 );
+
+  ss.clear();
+  ss.str(empty);
+  ss << "-1";
+  err = ios_base::goodbit;
+  end = ng.get(ss.rdbuf(), 0, ss, err, ul0);
+  VERIFY( err == ios_base::eofbit );
+  VERIFY( ul0 == ul1 );
+
+  ss.clear();
+  ss.str(empty);
+  ss << '-' << ul1;
+  err = ios_base::goodbit;
+  end = ng.get(ss.rdbuf(), 0, ss, err, ul0);
+  VERIFY( err == ios_base::eofbit );
+  VERIFY( ul0 == 1 );
+
+  ss.clear();
+  ss.str(empty);
+  ss << '-' << ul1 << '0';
+  err = ios_base::goodbit;
+  end = ng.get(ss.rdbuf(), 0, ss, err, ul0);
+  VERIFY( err == (ios_base::eofbit | ios_base::failbit) );
+  VERIFY( ul0 == ul1 );
+}
+
+int main()
+{
+  test01();
+  return 0;
+}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]