summaryrefslogtreecommitdiff
path: root/libstdc++-v3
diff options
context:
space:
mode:
authorPaolo Carlini <pcarlini@suse.de>2003-12-03 09:18:28 +0000
committerPaolo Carlini <paolo@gcc.gnu.org>2003-12-03 09:18:28 +0000
commit3259561c4c46afb921e593baeebdc0a955063473 (patch)
tree6b2133520095c88dcebbddb70318a43ff390faa1 /libstdc++-v3
parentc6a25d3a3dff96bf920d34b31faba6fdb5ff9323 (diff)
downloadgcc-3259561c4c46afb921e593baeebdc0a955063473.tar.gz
re PR libstdc++/12791 (_M_extract_num returns a wrong __beg in case of error)
2003-12-03 Paolo Carlini <pcarlini@suse.de> PR libstdc++/12791 * include/bits/locale_facets.tcc (time_get::_M_extract_num): Rewrite, stop the parsing as soon as a digit cannot possibly lead to a final number within the bounds; otherwise, simplify, avoiding __ctype.is() and atoi(). * testsuite/22_locale/time_get/get_date/char/12791.cc: New. * testsuite/22_locale/time_get/get_date/wchar_t/12791.cc: New. * include/bits/locale_facets.tcc (time_get::_M_extract_via_format): Minor tweak: a 4-digit integer cannot be bigger than 9999. * testsuite/22_locale/time_get/get_date/wchar_t/1.cc: Use type-correct wchar_t string literals. * testsuite/22_locale/time_get/get_monthname/wchar_t/1.cc: Ditto. * testsuite/22_locale/time_get/get_time/wchar_t/1.cc: Ditto. * testsuite/22_locale/time_get/get_weekday/wchar_t/1.cc: Ditto. * testsuite/22_locale/time_get/get_year/wchar_t/1.cc: Ditto. From-SVN: r74220
Diffstat (limited to 'libstdc++-v3')
-rw-r--r--libstdc++-v3/ChangeLog20
-rw-r--r--libstdc++-v3/include/bits/locale_facets.tcc36
-rw-r--r--libstdc++-v3/testsuite/22_locale/time_get/get_date/char/12791.cc65
-rw-r--r--libstdc++-v3/testsuite/22_locale/time_get/get_date/wchar_t/1.cc4
-rw-r--r--libstdc++-v3/testsuite/22_locale/time_get/get_date/wchar_t/12791.cc65
-rw-r--r--libstdc++-v3/testsuite/22_locale/time_get/get_monthname/wchar_t/1.cc8
-rw-r--r--libstdc++-v3/testsuite/22_locale/time_get/get_time/wchar_t/1.cc2
-rw-r--r--libstdc++-v3/testsuite/22_locale/time_get/get_year/wchar_t/1.cc6
8 files changed, 181 insertions, 25 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog
index 63eed9dbdaf..1a45fef10c3 100644
--- a/libstdc++-v3/ChangeLog
+++ b/libstdc++-v3/ChangeLog
@@ -1,3 +1,23 @@
+2003-12-03 Paolo Carlini <pcarlini@suse.de>
+
+ PR libstdc++/12791
+ * include/bits/locale_facets.tcc (time_get::_M_extract_num):
+ Rewrite, stop the parsing as soon as a digit cannot possibly
+ lead to a final number within the bounds; otherwise, simplify,
+ avoiding __ctype.is() and atoi().
+ * testsuite/22_locale/time_get/get_date/char/12791.cc: New.
+ * testsuite/22_locale/time_get/get_date/wchar_t/12791.cc: New.
+
+ * include/bits/locale_facets.tcc (time_get::_M_extract_via_format):
+ Minor tweak: a 4-digit integer cannot be bigger than 9999.
+
+ * testsuite/22_locale/time_get/get_date/wchar_t/1.cc: Use
+ type-correct wchar_t string literals.
+ * testsuite/22_locale/time_get/get_monthname/wchar_t/1.cc: Ditto.
+ * testsuite/22_locale/time_get/get_time/wchar_t/1.cc: Ditto.
+ * testsuite/22_locale/time_get/get_weekday/wchar_t/1.cc: Ditto.
+ * testsuite/22_locale/time_get/get_year/wchar_t/1.cc: Ditto.
+
2003-12-02 Paolo Carlini <pcarlini@suse.de>
* include/bits/locale_facets.tcc (time_get::do_get_year):
diff --git a/libstdc++-v3/include/bits/locale_facets.tcc b/libstdc++-v3/include/bits/locale_facets.tcc
index 47696ac7c76..e921585fe9c 100644
--- a/libstdc++-v3/include/bits/locale_facets.tcc
+++ b/libstdc++-v3/include/bits/locale_facets.tcc
@@ -1679,8 +1679,7 @@ namespace std
break;
case 'Y':
// Year [1900). [tm_year]
- _M_extract_num(__beg, __end, __mem, 0,
- numeric_limits<int>::max(), 4,
+ _M_extract_num(__beg, __end, __mem, 0, 9999, 4,
__ctype, __err);
if (!__err)
__tm->tm_year = __mem - 1900;
@@ -1732,23 +1731,29 @@ namespace std
const ctype<_CharT>& __ctype,
ios_base::iostate& __err) const
{
+ // As-is works for __len = 1, 2, 4, the values actually used.
+ int __mult = __len == 2 ? 10 : (__len == 4 ? 1000 : 1);
+
+ ++__min;
size_t __i = 0;
- string __digits;
- bool __testvalid = true;
- for (; __beg != __end && __i < __len
- && __ctype.is(ctype_base::digit, *__beg); ++__beg, ++__i)
- __digits += __ctype.narrow(*__beg, 0);
- if (__i == __len)
+ int __value = 0;
+ for (; __beg != __end && __i < __len; ++__beg, ++__i)
{
- const int __value = std::atoi(__digits.c_str());
- if (__min <= __value && __value <= __max)
- __member = __value;
+ const char __c = __ctype.narrow(*__beg, '*');
+ if (__c >= '0' && __c <= '9')
+ {
+ __value = __value * 10 + (__c - '0');
+ const int __valuec = __value * __mult;
+ if (__valuec > __max || __valuec + __mult < __min)
+ break;
+ __mult /= 10;
+ }
else
- __testvalid = false;
+ break;
}
+ if (__i == __len)
+ __member = __value;
else
- __testvalid = false;
- if (!__testvalid)
__err |= ios_base::failbit;
}
@@ -2031,7 +2036,8 @@ namespace std
// NB: This size is arbitrary. Should this be a data member,
// initialized at construction?
const size_t __maxlen = 64;
- char_type* __res = static_cast<char_type*>(__builtin_alloca(sizeof(char_type) * __maxlen));
+ char_type* __res = static_cast<char_type*>(__builtin_alloca(sizeof(char_type)
+ * __maxlen));
// NB: In IEE 1003.1-200x, and perhaps other locale models, it
// is possible that the format character will be longer than one
diff --git a/libstdc++-v3/testsuite/22_locale/time_get/get_date/char/12791.cc b/libstdc++-v3/testsuite/22_locale/time_get/get_date/char/12791.cc
new file mode 100644
index 00000000000..643bbca81d4
--- /dev/null
+++ b/libstdc++-v3/testsuite/22_locale/time_get/get_date/char/12791.cc
@@ -0,0 +1,65 @@
+// 2003-12-03 Paolo Carlini <pcarlini@suse.de>
+
+// Copyright (C) 2003 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 2, 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 COPYING. If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// 22.2.5.1.1 time_get members
+
+#include <locale>
+#include <sstream>
+#include <testsuite_hooks.h>
+
+// libstdc++/12791
+void test01()
+{
+ using namespace std;
+ bool test __attribute__((unused)) = true;
+
+ typedef istreambuf_iterator<char> iterator_type;
+
+ // create an ostream-derived object, cache the time_get facet
+ iterator_type end;
+
+ istringstream iss;
+ const time_get<char>& tim_get = use_facet<time_get<char> >(iss.getloc());
+
+ const ios_base::iostate good = ios_base::goodbit;
+ ios_base::iostate errorstate = good;
+
+ iss.str("60/04/71");
+ iterator_type is_it01(iss);
+ tm time01;
+ errorstate = good;
+ tim_get.get_date(is_it01, end, iss, errorstate, &time01);
+ VERIFY( errorstate == ios_base::failbit );
+ VERIFY( *is_it01 == '6' );
+
+ iss.str("04/38/71");
+ iterator_type is_it02(iss);
+ tm time02;
+ errorstate = good;
+ tim_get.get_date(is_it02, end, iss, errorstate, &time02);
+ VERIFY( errorstate == ios_base::failbit );
+ VERIFY( *is_it02 == '8' );
+}
+
+int main()
+{
+ test01();
+ return 0;
+}
diff --git a/libstdc++-v3/testsuite/22_locale/time_get/get_date/wchar_t/1.cc b/libstdc++-v3/testsuite/22_locale/time_get/get_date/wchar_t/1.cc
index 4affdf1796a..e4b67e0778d 100644
--- a/libstdc++-v3/testsuite/22_locale/time_get/get_date/wchar_t/1.cc
+++ b/libstdc++-v3/testsuite/22_locale/time_get/get_date/wchar_t/1.cc
@@ -80,7 +80,7 @@ void test01()
VERIFY( time02.tm_mon == time_bday.tm_mon );
VERIFY( time02.tm_mday == time_bday.tm_mday );
VERIFY( errorstate == good );
- VERIFY( *is_it02 == ' ');
+ VERIFY( *is_it02 == L' ' );
iss.str(L"04/04d/71 ");
iterator_type is_it03(iss);
@@ -92,7 +92,7 @@ void test01()
VERIFY( time03.tm_mon == time_bday.tm_mon );
VERIFY( time03.tm_mday == time_bday.tm_mday );
VERIFY( errorstate == ios_base::failbit );
- VERIFY( *is_it03 == 'd');
+ VERIFY( *is_it03 == L'd' );
}
int main()
diff --git a/libstdc++-v3/testsuite/22_locale/time_get/get_date/wchar_t/12791.cc b/libstdc++-v3/testsuite/22_locale/time_get/get_date/wchar_t/12791.cc
new file mode 100644
index 00000000000..d06fca8d34e
--- /dev/null
+++ b/libstdc++-v3/testsuite/22_locale/time_get/get_date/wchar_t/12791.cc
@@ -0,0 +1,65 @@
+// 2003-12-03 Paolo Carlini <pcarlini@suse.de>
+
+// Copyright (C) 2003 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 2, 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 COPYING. If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// 22.2.5.1.1 time_get members
+
+#include <locale>
+#include <sstream>
+#include <testsuite_hooks.h>
+
+// libstdc++/12791
+void test01()
+{
+ using namespace std;
+ bool test __attribute__((unused)) = true;
+
+ typedef istreambuf_iterator<wchar_t> iterator_type;
+
+ // create an ostream-derived object, cache the time_get facet
+ iterator_type end;
+
+ wistringstream iss;
+ const time_get<wchar_t>& tim_get = use_facet<time_get<wchar_t> >(iss.getloc());
+
+ const ios_base::iostate good = ios_base::goodbit;
+ ios_base::iostate errorstate = good;
+
+ iss.str(L"60/04/71");
+ iterator_type is_it01(iss);
+ tm time01;
+ errorstate = good;
+ tim_get.get_date(is_it01, end, iss, errorstate, &time01);
+ VERIFY( errorstate == ios_base::failbit );
+ VERIFY( *is_it01 == L'6' );
+
+ iss.str(L"04/38/71");
+ iterator_type is_it02(iss);
+ tm time02;
+ errorstate = good;
+ tim_get.get_date(is_it02, end, iss, errorstate, &time02);
+ VERIFY( errorstate == ios_base::failbit );
+ VERIFY( *is_it02 == L'8' );
+}
+
+int main()
+{
+ test01();
+ return 0;
+}
diff --git a/libstdc++-v3/testsuite/22_locale/time_get/get_monthname/wchar_t/1.cc b/libstdc++-v3/testsuite/22_locale/time_get/get_monthname/wchar_t/1.cc
index 47f5c48188a..17a8075f16d 100644
--- a/libstdc++-v3/testsuite/22_locale/time_get/get_monthname/wchar_t/1.cc
+++ b/libstdc++-v3/testsuite/22_locale/time_get/get_monthname/wchar_t/1.cc
@@ -81,7 +81,7 @@ void test01()
tim_get.get_monthname(is_it03, end, iss, errorstate, &time03);
VERIFY( time03.tm_mon == time_bday.tm_mon );
VERIFY( errorstate == good );
- VERIFY( *is_it03 == ' ');
+ VERIFY( *is_it03 == L' ' );
iss.str(L"Aar");
iterator_type is_it04(iss);
@@ -90,7 +90,7 @@ void test01()
errorstate = good;
tim_get.get_monthname(is_it04, end, iss, errorstate, &time04);
VERIFY( time04.tm_mon == 5 );
- VERIFY( *is_it04 == 'a');
+ VERIFY( *is_it04 == L'a' );
VERIFY( errorstate == ios_base::failbit );
iss.str(L"December ");
@@ -100,7 +100,7 @@ void test01()
tim_get.get_monthname(is_it05, end, iss, errorstate, &time05);
VERIFY( time05.tm_mon == 11 );
VERIFY( errorstate == good );
- VERIFY( *is_it05 == ' ');
+ VERIFY( *is_it05 == L' ' );
iss.str(L"Decelember ");
iterator_type is_it06(iss);
@@ -110,7 +110,7 @@ void test01()
tim_get.get_monthname(is_it06, end, iss, errorstate, &time06);
VERIFY( time06.tm_mon == 4 );
VERIFY( errorstate == ios_base::failbit );
- VERIFY( *is_it05 == 'l');
+ VERIFY( *is_it05 == L'l' );
}
int main()
diff --git a/libstdc++-v3/testsuite/22_locale/time_get/get_time/wchar_t/1.cc b/libstdc++-v3/testsuite/22_locale/time_get/get_time/wchar_t/1.cc
index bc9b3a12734..f8d0bc6de60 100644
--- a/libstdc++-v3/testsuite/22_locale/time_get/get_time/wchar_t/1.cc
+++ b/libstdc++-v3/testsuite/22_locale/time_get/get_time/wchar_t/1.cc
@@ -94,7 +94,7 @@ void test01()
errorstate = good;
tim_get.get_time(is_it04, end, iss, errorstate, &time04);
VERIFY( time01.tm_hour == time_bday.tm_hour );
- VERIFY( *is_it04 == 'a');
+ VERIFY( *is_it04 == L'a' );
VERIFY( errorstate == ios_base::failbit );
// inspection of named locales, de_DE
diff --git a/libstdc++-v3/testsuite/22_locale/time_get/get_year/wchar_t/1.cc b/libstdc++-v3/testsuite/22_locale/time_get/get_year/wchar_t/1.cc
index 5aa77c9519f..578d18f7e04 100644
--- a/libstdc++-v3/testsuite/22_locale/time_get/get_year/wchar_t/1.cc
+++ b/libstdc++-v3/testsuite/22_locale/time_get/get_year/wchar_t/1.cc
@@ -76,7 +76,7 @@ void test01()
tim_get.get_year(is_it02, end, iss, errorstate, &time02);
VERIFY( time02.tm_year == time_bday.tm_year );
VERIFY( errorstate == good );
- VERIFY( *is_it02 == ' ');
+ VERIFY( *is_it02 == L' ' );
iss.str(L"197d1 ");
iterator_type is_it03(iss);
@@ -86,7 +86,7 @@ void test01()
tim_get.get_year(is_it03, end, iss, errorstate, &time03);
VERIFY( time03.tm_year == 3 );
VERIFY( errorstate == ios_base::failbit );
- VERIFY( *is_it03 == 'd');
+ VERIFY( *is_it03 == L'd' );
iss.str(L"71d71");
iterator_type is_it04(iss);
@@ -95,7 +95,7 @@ void test01()
tim_get.get_year(is_it04, end, iss, errorstate, &time04);
VERIFY( time04.tm_year == time_bday.tm_year );
VERIFY( errorstate == good );
- VERIFY( *is_it03 == 'd');
+ VERIFY( *is_it03 == L'd' );
iss.str(L"71");
iterator_type is_it05(iss);