This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
[Patch] Fix libstdc++/12791
- From: Paolo Carlini <pcarlini at suse dot de>
- To: libstdc++ <libstdc++ at gcc dot gnu dot org>
- Date: Tue, 02 Dec 2003 22:49:16 +0100
- Subject: [Patch] Fix libstdc++/12791
Hi,
it took me some time, but I find the result quite satisfying.
The new _M_extract_num is pretty general, only the initialization
of __mult is limited for the moment to __len = 1, 2, 4, the values
actually used in the library, otherwise (by just adding a pow() call
or something like that) the code is general and works ok up to the
the size of the int type.
This implies, among other things, that I didn't consider replacing
the division (__mult /= 10 -> __max *= 10, __min *= 10) neither I
have a special case for __len == 2 (the most common value).
Since we are now avoiding basic_string and atoi() the final code
is anyway pretty fast (besides not being affected by the iterator
bug, of course ;)
Please have a look: tomorrow (italian time) I mean to commit the
work (or variants of it ;)
Tested x86-linux, as usual.
Paolo.
///////////
2003-12-02 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.
diff -prN libstdc++-v3-orig/include/bits/locale_facets.tcc libstdc++-v3/include/bits/locale_facets.tcc
*** libstdc++-v3-orig/include/bits/locale_facets.tcc Tue Dec 2 11:14:59 2003
--- libstdc++-v3/include/bits/locale_facets.tcc Tue Dec 2 22:14:08 2003
*************** namespace std
*** 1679,1686 ****
break;
case 'Y':
// Year [1900). [tm_year]
! _M_extract_num(__beg, __end, __mem, 0,
! numeric_limits<int>::max(), 4,
__ctype, __err);
if (!__err)
__tm->tm_year = __mem - 1900;
--- 1679,1685 ----
break;
case 'Y':
// Year [1900). [tm_year]
! _M_extract_num(__beg, __end, __mem, 0, 9999, 4,
__ctype, __err);
if (!__err)
__tm->tm_year = __mem - 1900;
*************** namespace std
*** 1732,1754 ****
const ctype<_CharT>& __ctype,
ios_base::iostate& __err) const
{
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)
{
! const int __value = std::atoi(__digits.c_str());
! if (__min <= __value && __value <= __max)
! __member = __value;
else
! __testvalid = false;
}
else
- __testvalid = false;
- if (!__testvalid)
__err |= ios_base::failbit;
}
--- 1731,1759 ----
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;
! int __value = 0;
! for (; __beg != __end && __i < __len; ++__beg, ++__i)
{
! 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
! break;
}
+ if (__i == __len)
+ __member = __value;
else
__err |= ios_base::failbit;
}
*************** namespace std
*** 2031,2037 ****
// 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));
// NB: In IEE 1003.1-200x, and perhaps other locale models, it
// is possible that the format character will be longer than one
--- 2036,2043 ----
// 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));
// NB: In IEE 1003.1-200x, and perhaps other locale models, it
// is possible that the format character will be longer than one
diff -prN libstdc++-v3-orig/testsuite/22_locale/time_get/get_date/char/12791.cc libstdc++-v3/testsuite/22_locale/time_get/get_date/char/12791.cc
*** libstdc++-v3-orig/testsuite/22_locale/time_get/get_date/char/12791.cc Thu Jan 1 01:00:00 1970
--- libstdc++-v3/testsuite/22_locale/time_get/get_date/char/12791.cc Tue Dec 2 22:23:13 2003
***************
*** 0 ****
--- 1,65 ----
+ // 2003-12-02 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 -prN libstdc++-v3-orig/testsuite/22_locale/time_get/get_date/wchar_t/1.cc libstdc++-v3/testsuite/22_locale/time_get/get_date/wchar_t/1.cc
*** libstdc++-v3-orig/testsuite/22_locale/time_get/get_date/wchar_t/1.cc Tue Sep 23 22:02:34 2003
--- libstdc++-v3/testsuite/22_locale/time_get/get_date/wchar_t/1.cc Tue Dec 2 19:34:36 2003
*************** void test01()
*** 80,86 ****
VERIFY( time02.tm_mon == time_bday.tm_mon );
VERIFY( time02.tm_mday == time_bday.tm_mday );
VERIFY( errorstate == good );
! VERIFY( *is_it02 == ' ');
iss.str(L"04/04d/71 ");
iterator_type is_it03(iss);
--- 80,86 ----
VERIFY( time02.tm_mon == time_bday.tm_mon );
VERIFY( time02.tm_mday == time_bday.tm_mday );
VERIFY( errorstate == good );
! VERIFY( *is_it02 == L' ' );
iss.str(L"04/04d/71 ");
iterator_type is_it03(iss);
*************** void test01()
*** 92,98 ****
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');
}
int main()
--- 92,98 ----
VERIFY( time03.tm_mon == time_bday.tm_mon );
VERIFY( time03.tm_mday == time_bday.tm_mday );
VERIFY( errorstate == ios_base::failbit );
! VERIFY( *is_it03 == L'd' );
}
int main()
diff -prN libstdc++-v3-orig/testsuite/22_locale/time_get/get_date/wchar_t/12791.cc libstdc++-v3/testsuite/22_locale/time_get/get_date/wchar_t/12791.cc
*** libstdc++-v3-orig/testsuite/22_locale/time_get/get_date/wchar_t/12791.cc Thu Jan 1 01:00:00 1970
--- libstdc++-v3/testsuite/22_locale/time_get/get_date/wchar_t/12791.cc Tue Dec 2 22:23:23 2003
***************
*** 0 ****
--- 1,65 ----
+ // 2003-12-02 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 -prN libstdc++-v3-orig/testsuite/22_locale/time_get/get_monthname/wchar_t/1.cc libstdc++-v3/testsuite/22_locale/time_get/get_monthname/wchar_t/1.cc
*** libstdc++-v3-orig/testsuite/22_locale/time_get/get_monthname/wchar_t/1.cc Tue Sep 23 22:02:35 2003
--- libstdc++-v3/testsuite/22_locale/time_get/get_monthname/wchar_t/1.cc Tue Dec 2 19:34:36 2003
*************** void test01()
*** 81,87 ****
tim_get.get_monthname(is_it03, end, iss, errorstate, &time03);
VERIFY( time03.tm_mon == time_bday.tm_mon );
VERIFY( errorstate == good );
! VERIFY( *is_it03 == ' ');
iss.str(L"Aar");
iterator_type is_it04(iss);
--- 81,87 ----
tim_get.get_monthname(is_it03, end, iss, errorstate, &time03);
VERIFY( time03.tm_mon == time_bday.tm_mon );
VERIFY( errorstate == good );
! VERIFY( *is_it03 == L' ' );
iss.str(L"Aar");
iterator_type is_it04(iss);
*************** void test01()
*** 90,96 ****
errorstate = good;
tim_get.get_monthname(is_it04, end, iss, errorstate, &time04);
VERIFY( time04.tm_mon == 5 );
! VERIFY( *is_it04 == 'a');
VERIFY( errorstate == ios_base::failbit );
iss.str(L"December ");
--- 90,96 ----
errorstate = good;
tim_get.get_monthname(is_it04, end, iss, errorstate, &time04);
VERIFY( time04.tm_mon == 5 );
! VERIFY( *is_it04 == L'a' );
VERIFY( errorstate == ios_base::failbit );
iss.str(L"December ");
*************** void test01()
*** 100,106 ****
tim_get.get_monthname(is_it05, end, iss, errorstate, &time05);
VERIFY( time05.tm_mon == 11 );
VERIFY( errorstate == good );
! VERIFY( *is_it05 == ' ');
iss.str(L"Decelember ");
iterator_type is_it06(iss);
--- 100,106 ----
tim_get.get_monthname(is_it05, end, iss, errorstate, &time05);
VERIFY( time05.tm_mon == 11 );
VERIFY( errorstate == good );
! VERIFY( *is_it05 == L' ' );
iss.str(L"Decelember ");
iterator_type is_it06(iss);
*************** void test01()
*** 110,116 ****
tim_get.get_monthname(is_it06, end, iss, errorstate, &time06);
VERIFY( time06.tm_mon == 4 );
VERIFY( errorstate == ios_base::failbit );
! VERIFY( *is_it05 == 'l');
}
int main()
--- 110,116 ----
tim_get.get_monthname(is_it06, end, iss, errorstate, &time06);
VERIFY( time06.tm_mon == 4 );
VERIFY( errorstate == ios_base::failbit );
! VERIFY( *is_it05 == L'l' );
}
int main()
diff -prN libstdc++-v3-orig/testsuite/22_locale/time_get/get_time/wchar_t/1.cc libstdc++-v3/testsuite/22_locale/time_get/get_time/wchar_t/1.cc
*** libstdc++-v3-orig/testsuite/22_locale/time_get/get_time/wchar_t/1.cc Tue Sep 23 22:02:35 2003
--- libstdc++-v3/testsuite/22_locale/time_get/get_time/wchar_t/1.cc Tue Dec 2 19:34:36 2003
*************** void test01()
*** 94,100 ****
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( errorstate == ios_base::failbit );
// inspection of named locales, de_DE
--- 94,100 ----
errorstate = good;
tim_get.get_time(is_it04, end, iss, errorstate, &time04);
VERIFY( time01.tm_hour == time_bday.tm_hour );
! VERIFY( *is_it04 == L'a' );
VERIFY( errorstate == ios_base::failbit );
// inspection of named locales, de_DE
diff -prN libstdc++-v3-orig/testsuite/22_locale/time_get/get_weekday/wchar_t/1.cc libstdc++-v3/testsuite/22_locale/time_get/get_weekday/wchar_t/1.cc
*** libstdc++-v3-orig/testsuite/22_locale/time_get/get_weekday/wchar_t/1.cc Tue Sep 23 22:02:36 2003
--- libstdc++-v3/testsuite/22_locale/time_get/get_weekday/wchar_t/1.cc Tue Dec 2 19:34:36 2003
*************** void test01()
*** 85,91 ****
tim_get.get_weekday(is_it03, end, iss, errorstate, &time03);
VERIFY( time03.tm_wday == time_bday.tm_wday );
VERIFY( errorstate == good );
! VERIFY( *is_it03 == ' ');
iss.str(L"San");
iterator_type is_it04(iss);
--- 85,91 ----
tim_get.get_weekday(is_it03, end, iss, errorstate, &time03);
VERIFY( time03.tm_wday == time_bday.tm_wday );
VERIFY( errorstate == good );
! VERIFY( *is_it03 == L' ' );
iss.str(L"San");
iterator_type is_it04(iss);
*************** void test01()
*** 94,100 ****
errorstate = good;
tim_get.get_weekday(is_it04, end, iss, errorstate, &time04);
VERIFY( time04.tm_wday == 4 );
! VERIFY( *is_it04 == 'n');
VERIFY( errorstate == ios_base::failbit );
iss.str(L"Tuesday ");
--- 94,100 ----
errorstate = good;
tim_get.get_weekday(is_it04, end, iss, errorstate, &time04);
VERIFY( time04.tm_wday == 4 );
! VERIFY( *is_it04 == L'n' );
VERIFY( errorstate == ios_base::failbit );
iss.str(L"Tuesday ");
*************** void test01()
*** 104,110 ****
tim_get.get_weekday(is_it05, end, iss, errorstate, &time05);
VERIFY( time05.tm_wday == 2 );
VERIFY( errorstate == good );
! VERIFY( *is_it05 == ' ');
iss.str(L"Tuesducky "); // Kind of like Fryday, without the swirls.
iterator_type is_it06(iss);
--- 104,110 ----
tim_get.get_weekday(is_it05, end, iss, errorstate, &time05);
VERIFY( time05.tm_wday == 2 );
VERIFY( errorstate == good );
! VERIFY( *is_it05 == L' ' );
iss.str(L"Tuesducky "); // Kind of like Fryday, without the swirls.
iterator_type is_it06(iss);
*************** void test01()
*** 114,120 ****
tim_get.get_weekday(is_it06, end, iss, errorstate, &time06);
VERIFY( time06.tm_wday == 4 );
VERIFY( errorstate == ios_base::failbit );
! VERIFY( *is_it05 == 'u');
}
int main()
--- 114,120 ----
tim_get.get_weekday(is_it06, end, iss, errorstate, &time06);
VERIFY( time06.tm_wday == 4 );
VERIFY( errorstate == ios_base::failbit );
! VERIFY( *is_it05 == L'u' );
}
int main()
diff -prN libstdc++-v3-orig/testsuite/22_locale/time_get/get_year/wchar_t/1.cc libstdc++-v3/testsuite/22_locale/time_get/get_year/wchar_t/1.cc
*** libstdc++-v3-orig/testsuite/22_locale/time_get/get_year/wchar_t/1.cc Tue Sep 23 22:02:36 2003
--- libstdc++-v3/testsuite/22_locale/time_get/get_year/wchar_t/1.cc Tue Dec 2 19:34:36 2003
*************** void test01()
*** 76,82 ****
tim_get.get_year(is_it02, end, iss, errorstate, &time02);
VERIFY( time02.tm_year == time_bday.tm_year );
VERIFY( errorstate == good );
! VERIFY( *is_it02 == ' ');
iss.str(L"197d1 ");
iterator_type is_it03(iss);
--- 76,82 ----
tim_get.get_year(is_it02, end, iss, errorstate, &time02);
VERIFY( time02.tm_year == time_bday.tm_year );
VERIFY( errorstate == good );
! VERIFY( *is_it02 == L' ' );
iss.str(L"197d1 ");
iterator_type is_it03(iss);
*************** void test01()
*** 86,92 ****
tim_get.get_year(is_it03, end, iss, errorstate, &time03);
VERIFY( time03.tm_year == 3 );
VERIFY( errorstate == ios_base::failbit );
! VERIFY( *is_it03 == 'd');
iss.str(L"71d71");
iterator_type is_it04(iss);
--- 86,92 ----
tim_get.get_year(is_it03, end, iss, errorstate, &time03);
VERIFY( time03.tm_year == 3 );
VERIFY( errorstate == ios_base::failbit );
! VERIFY( *is_it03 == L'd' );
iss.str(L"71d71");
iterator_type is_it04(iss);
*************** void test01()
*** 95,101 ****
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');
iss.str(L"71");
iterator_type is_it05(iss);
--- 95,101 ----
tim_get.get_year(is_it04, end, iss, errorstate, &time04);
VERIFY( time04.tm_year == time_bday.tm_year );
VERIFY( errorstate == good );
! VERIFY( *is_it03 == L'd' );
iss.str(L"71");
iterator_type is_it05(iss);