From fbc302a99204c2185a7c090918b055130095cf5f Mon Sep 17 00:00:00 2001 From: Hamid Reza Arzaghi Date: Wed, 9 Sep 2020 01:03:10 +0430 Subject: [PATCH 1/7] add _MaxLen parameter for _GetInt function --- stl/inc/xloctime | 64 +++++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/stl/inc/xloctime b/stl/inc/xloctime index baf58d94310..de80759aad2 100644 --- a/stl/inc/xloctime +++ b/stl/inc/xloctime @@ -212,18 +212,18 @@ protected: ios_base::iostate& _State, tm* _Pt) const { // get time of day from [_First, _Last) into _Pt const _Ctype& _Ctype_fac = _STD use_facet<_Ctype>(_Iosbase.getloc()); - _State |= _Getint(_First, _Last, 0, 23, _Pt->tm_hour, _Ctype_fac); + _State |= _Getint(_First, _Last, 0, 23, _Pt->tm_hour, _Ctype_fac, 2); if (_State != ios_base::goodbit || _Ctype_fac.narrow(*_First) != ':') { _State |= ios_base::failbit; // hour field is bad } else { - _State |= _Getint(++_First, _Last, 0, 59, _Pt->tm_min, _Ctype_fac); + _State |= _Getint(++_First, _Last, 0, 59, _Pt->tm_min, _Ctype_fac, 2); } if (_State != ios_base::goodbit || _Ctype_fac.narrow(*_First) != ':') { _State |= ios_base::failbit; // min field is bad } else { - _State |= _Getint(++_First, _Last, 0, 59, _Pt->tm_sec, _Ctype_fac); + _State |= _Getint(++_First, _Last, 0, 59, _Pt->tm_sec, _Ctype_fac, 2); } return _First; @@ -243,10 +243,10 @@ protected: _First = get_monthname(_First, _Last, _Iosbase, _State, _Pt); _Dorder = mdy; } else if (_Dorder == mdy) { // get month number - _State |= _Getint(_First, _Last, 1, 12, _Pt->tm_mon, _Ctype_fac); + _State |= _Getint(_First, _Last, 1, 12, _Pt->tm_mon, _Ctype_fac, 2); --_Pt->tm_mon; } else if (_Dorder == dmy) { - _State |= _Getint(_First, _Last, 1, 31, _Pt->tm_mday, _Ctype_fac); + _State |= _Getint(_First, _Last, 1, 31, _Pt->tm_mday, _Ctype_fac, 2); } else { // ymd or ydm _First = get_year(_First, _Last, _Iosbase, _State, _Pt); } @@ -278,10 +278,10 @@ protected: } } } else if (_Dorder == dmy || _Dorder == ymd) { // get month number - _State |= _Getint(_First, _Last, 1, 12, _Pt->tm_mon, _Ctype_fac); + _State |= _Getint(_First, _Last, 1, 12, _Pt->tm_mon, _Ctype_fac, 2); --_Pt->tm_mon; } else { - _State |= _Getint(_First, _Last, 1, 31, _Pt->tm_mday, _Ctype_fac); + _State |= _Getint(_First, _Last, 1, 31, _Pt->tm_mday, _Ctype_fac, 2); } } @@ -309,10 +309,10 @@ protected: _First = get_monthname(_First, _Last, _Iosbase, _State, _Pt); } } else if (_Dorder == ydm) { // get month number - _State |= _Getint(_First, _Last, 1, 12, _Pt->tm_mon, _Ctype_fac); + _State |= _Getint(_First, _Last, 1, 12, _Pt->tm_mon, _Ctype_fac, 2); --_Pt->tm_mon; } else if (_Dorder == ymd) { - _State |= _Getint(_First, _Last, 1, 31, _Pt->tm_mday, _Ctype_fac); + _State |= _Getint(_First, _Last, 1, 31, _Pt->tm_mday, _Ctype_fac, 2); } else { // mdy or dmy _First = get_year(_First, _Last, _Iosbase, _State, _Pt); } @@ -354,7 +354,7 @@ protected: const _Ctype& _Ctype_fac = _STD use_facet<_Ctype>(_Iosbase.getloc()); int _Ans = 0; - ios_base::iostate _Res = _Getint(_First, _Last, 0, 9999, _Ans, _Ctype_fac); + ios_base::iostate _Res = _Getint(_First, _Last, 0, 9999, _Ans, _Ctype_fac, 4); _State |= _Res; // pass on eofbit and failbit if (!(_Res & ios_base::failbit)) { @@ -394,7 +394,7 @@ protected: break; case 'C': - _State |= _Getint(_First, _Last, 0, 99, _Ans, _Ctype_fac); + _State |= _Getint(_First, _Last, 0, 99, _Ans, _Ctype_fac, 2); if (!(_State & ios_base::failbit)) { _Pt->tm_year = _Ans * 100 - 1900; // convert to century } @@ -403,7 +403,7 @@ protected: case 'd': case 'e': - _State |= _Getint(_First, _Last, 1, 31, _Pt->tm_mday, _Ctype_fac); + _State |= _Getint(_First, _Last, 1, 31, _Pt->tm_mday, _Ctype_fac, 2); break; case 'D': @@ -411,11 +411,11 @@ protected: break; case 'H': - _State |= _Getint(_First, _Last, 0, 23, _Pt->tm_hour, _Ctype_fac); + _State |= _Getint(_First, _Last, 0, 23, _Pt->tm_hour, _Ctype_fac, 2); break; case 'I': - _State |= _Getint(_First, _Last, 1, 12, _Ans, _Ctype_fac); + _State |= _Getint(_First, _Last, 1, 12, _Ans, _Ctype_fac, 2); if (!(_State & ios_base::failbit)) { _Pt->tm_hour = _Ans == 12 ? 0 : _Ans; } @@ -423,11 +423,11 @@ protected: break; case 'j': - _State |= _Getint(_First, _Last, 1, 366, _Pt->tm_yday, _Ctype_fac); + _State |= _Getint(_First, _Last, 1, 366, _Pt->tm_yday, _Ctype_fac, 3); break; case 'm': - _State |= _Getint(_First, _Last, 1, 12, _Ans, _Ctype_fac); + _State |= _Getint(_First, _Last, 1, 12, _Ans, _Ctype_fac, 2); if (!(_State & ios_base::failbit)) { _Pt->tm_mon = _Ans - 1; } @@ -435,7 +435,7 @@ protected: break; case 'M': - _State |= _Getint(_First, _Last, 0, 59, _Pt->tm_min, _Ctype_fac); + _State |= _Getint(_First, _Last, 0, 59, _Pt->tm_min, _Ctype_fac, 2); break; case 'n': @@ -462,7 +462,7 @@ protected: break; case 'S': - _State |= _Getint(_First, _Last, 0, 60, _Pt->tm_sec, _Ctype_fac); + _State |= _Getint(_First, _Last, 0, 60, _Pt->tm_sec, _Ctype_fac, 2); break; case 'T': @@ -471,15 +471,15 @@ protected: break; case 'U': - _State |= _Getint(_First, _Last, 0, 53, _Pt->tm_yday, _Ctype_fac); + _State |= _Getint(_First, _Last, 0, 53, _Pt->tm_yday, _Ctype_fac, 2); break; case 'w': - _State |= _Getint(_First, _Last, 0, 6, _Pt->tm_wday, _Ctype_fac); + _State |= _Getint(_First, _Last, 0, 6, _Pt->tm_wday, _Ctype_fac, 1); break; case 'W': - _State |= _Getint(_First, _Last, 0, 53, _Pt->tm_yday, _Ctype_fac); + _State |= _Getint(_First, _Last, 0, 53, _Pt->tm_yday, _Ctype_fac, 2); break; case 'x': @@ -487,7 +487,7 @@ protected: break; case 'y': - _State |= _Getint(_First, _Last, 0, 99, _Ans, _Ctype_fac); + _State |= _Getint(_First, _Last, 0, 99, _Ans, _Ctype_fac, 2); if (!(_State & ios_base::failbit)) { _Pt->tm_year = _Ans < 69 ? _Ans + 100 : _Ans; } @@ -538,7 +538,8 @@ protected: private: ios_base::iostate __CLRCALL_OR_CDECL _Getint(_InIt& _First, _InIt& _Last, int _Lo, int _Hi, int& _Val, - const _Ctype& _Ctype_fac) const { // get integer in range [_Lo, _Hi] from [_First, _Last) + const _Ctype& _Ctype_fac, + const int _MaxLen = -1) const { // get integer in range [_Lo, _Hi] from [_First, _Last) char _Ac[_MAX_INT_DIG]; char* _Ep; char* _Ptr = _Ac; @@ -554,26 +555,27 @@ private: } } - bool _Seendigit = false; + int _Seendigit = 0; for (; _First != _Last && _Ctype_fac.narrow(*_First) == '0'; ++_First) { // strip leading zeros - _Seendigit = true; + ++_Seendigit; } - if (_Seendigit) { - *_Ptr++ = '0'; // replace one or more with single zero + if (_Seendigit > 0) { + *_Ptr++ = '0'; // replace one or more with single zero + _Seendigit = 1; } - for (char* const _Pe = &_Ac[_MAX_INT_DIG - 1]; - _First != _Last && '0' <= (_Ch = _Ctype_fac.narrow(*_First)) && _Ch <= '9'; - _Seendigit = true, (void) ++_First) { // copy digits + for (char* const _Pe = &_Ac[_MAX_INT_DIG - 1]; _First != _Last && '0' <= (_Ch = _Ctype_fac.narrow(*_First)) + && _Ch <= '9' && (_MaxLen == -1 || _Seendigit < _MaxLen); + ++_Seendigit, (void) ++_First) { // copy digits *_Ptr = _Ch; if (_Ptr < _Pe) { ++_Ptr; // drop trailing digits if already too large } } - if (!_Seendigit) { + if (_Seendigit == 0) { _Ptr = _Ac; } From c7de211311fb4105298ad1f7b5d52abc8790e102 Mon Sep 17 00:00:00 2001 From: Hamid Reza Arzaghi Date: Wed, 9 Sep 2020 01:37:02 +0430 Subject: [PATCH 2/7] add tests to cover the issue --- .../std/tests/Dev11_0836436_get_time/test.cpp | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/tests/std/tests/Dev11_0836436_get_time/test.cpp b/tests/std/tests/Dev11_0836436_get_time/test.cpp index 79d2dde40a9..f463db4714b 100644 --- a/tests/std/tests/Dev11_0836436_get_time/test.cpp +++ b/tests/std/tests/Dev11_0836436_get_time/test.cpp @@ -242,6 +242,66 @@ void test_990695() { assert(t.tm_year == 114); } + { + istringstream iss("20200609"); + ios_base::iostate err = Bit; + tm t{}; + const string fmt("%Y%m%d"); + use_facet>(iss.getloc()) + .get(Iter(iss.rdbuf()), Iter(), iss, err, &t, fmt.c_str(), fmt.c_str() + fmt.size()); + assert(t.tm_mon == 5); + assert(t.tm_mday == 9); + assert(t.tm_year == 120); + } + + { + istringstream iss("20201213"); + ios_base::iostate err = Bit; + tm t{}; + const string fmt("%Y%m%d"); + use_facet>(iss.getloc()) + .get(Iter(iss.rdbuf()), Iter(), iss, err, &t, fmt.c_str(), fmt.c_str() + fmt.size()); + assert(t.tm_mon == 11); + assert(t.tm_mday == 13); + assert(t.tm_year == 120); + } + + { + istringstream iss("2020112"); + ios_base::iostate err = Bit; + tm t{}; + const string fmt("%Y%m%d"); + use_facet>(iss.getloc()) + .get(Iter(iss.rdbuf()), Iter(), iss, err, &t, fmt.c_str(), fmt.c_str() + fmt.size()); + assert(t.tm_mon == 10); + assert(t.tm_mday == 2); + assert(t.tm_year == 120); + } + + { + istringstream iss("2020061125"); + ios_base::iostate err = Bit; + tm t{}; + const string fmt("%Y%m%d"); + use_facet>(iss.getloc()) + .get(Iter(iss.rdbuf()), Iter(), iss, err, &t, fmt.c_str(), fmt.c_str() + fmt.size()); + assert(t.tm_mon == 5); + assert(t.tm_mday == 11); + assert(t.tm_year == 120); + } + + { + istringstream iss("2020120625119"); + ios_base::iostate err = Bit; + tm t{}; + const string fmt("%Y12%m25%d"); + use_facet>(iss.getloc()) + .get(Iter(iss.rdbuf()), Iter(), iss, err, &t, fmt.c_str(), fmt.c_str() + fmt.size()); + assert(t.tm_mon == 5); + assert(t.tm_mday == 11); + assert(t.tm_year == 120); + } + { istringstream iss("sep 31 2014"); ios_base::iostate err = Bit; @@ -290,6 +350,50 @@ void test_990695() { assert(t.tm_year == 105); } + { + istringstream iss("20200609"); + tm t = {}; + const string fmt("%Y%m%d"); + iss >> get_time(&t, fmt.c_str()); + assert(!iss.fail()); + assert(t.tm_mon == 5); + assert(t.tm_mday == 9); + assert(t.tm_year == 120); + } + + { + istringstream iss("2020112"); + tm t = {}; + const string fmt("%Y%m%d"); + iss >> get_time(&t, fmt.c_str()); + assert(!iss.fail()); + assert(t.tm_mon == 10); + assert(t.tm_mday == 2); + assert(t.tm_year == 120); + } + + { + istringstream iss("2020061125"); + tm t = {}; + const string fmt("%Y%m%d"); + iss >> get_time(&t, fmt.c_str()); + assert(!iss.fail()); + assert(t.tm_mon == 5); + assert(t.tm_mday == 11); + assert(t.tm_year == 120); + } + + { + istringstream iss("2020124"); + tm t = {}; + const string fmt("%Y%d%m"); + iss >> get_time(&t, fmt.c_str()); + assert(!iss.fail()); + assert(t.tm_mon == 3); + assert(t.tm_mday == 12); + assert(t.tm_year == 120); + } + { // This case should fail istringstream iss("2011-D-18"); From 7613ad9eed0d057d84b40dbc1671f141c47d843e Mon Sep 17 00:00:00 2001 From: Hamid Reza Arzaghi Date: Thu, 10 Sep 2020 15:43:16 +0430 Subject: [PATCH 3/7] revert the _GetInt function to its previous signature --- stl/inc/xloctime | 69 +++++++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 33 deletions(-) diff --git a/stl/inc/xloctime b/stl/inc/xloctime index de80759aad2..83f590674af 100644 --- a/stl/inc/xloctime +++ b/stl/inc/xloctime @@ -212,18 +212,18 @@ protected: ios_base::iostate& _State, tm* _Pt) const { // get time of day from [_First, _Last) into _Pt const _Ctype& _Ctype_fac = _STD use_facet<_Ctype>(_Iosbase.getloc()); - _State |= _Getint(_First, _Last, 0, 23, _Pt->tm_hour, _Ctype_fac, 2); + _State |= _Getint(_First, _Last, 0, 23, _Pt->tm_hour, _Ctype_fac); if (_State != ios_base::goodbit || _Ctype_fac.narrow(*_First) != ':') { _State |= ios_base::failbit; // hour field is bad } else { - _State |= _Getint(++_First, _Last, 0, 59, _Pt->tm_min, _Ctype_fac, 2); + _State |= _Getint(++_First, _Last, 0, 59, _Pt->tm_min, _Ctype_fac); } if (_State != ios_base::goodbit || _Ctype_fac.narrow(*_First) != ':') { _State |= ios_base::failbit; // min field is bad } else { - _State |= _Getint(++_First, _Last, 0, 59, _Pt->tm_sec, _Ctype_fac, 2); + _State |= _Getint(++_First, _Last, 0, 59, _Pt->tm_sec, _Ctype_fac); } return _First; @@ -243,10 +243,10 @@ protected: _First = get_monthname(_First, _Last, _Iosbase, _State, _Pt); _Dorder = mdy; } else if (_Dorder == mdy) { // get month number - _State |= _Getint(_First, _Last, 1, 12, _Pt->tm_mon, _Ctype_fac, 2); + _State |= _Getint(_First, _Last, 1, 12, _Pt->tm_mon, _Ctype_fac); --_Pt->tm_mon; } else if (_Dorder == dmy) { - _State |= _Getint(_First, _Last, 1, 31, _Pt->tm_mday, _Ctype_fac, 2); + _State |= _Getint(_First, _Last, 1, 31, _Pt->tm_mday, _Ctype_fac); } else { // ymd or ydm _First = get_year(_First, _Last, _Iosbase, _State, _Pt); } @@ -278,10 +278,10 @@ protected: } } } else if (_Dorder == dmy || _Dorder == ymd) { // get month number - _State |= _Getint(_First, _Last, 1, 12, _Pt->tm_mon, _Ctype_fac, 2); + _State |= _Getint(_First, _Last, 1, 12, _Pt->tm_mon, _Ctype_fac); --_Pt->tm_mon; } else { - _State |= _Getint(_First, _Last, 1, 31, _Pt->tm_mday, _Ctype_fac, 2); + _State |= _Getint(_First, _Last, 1, 31, _Pt->tm_mday, _Ctype_fac); } } @@ -309,10 +309,10 @@ protected: _First = get_monthname(_First, _Last, _Iosbase, _State, _Pt); } } else if (_Dorder == ydm) { // get month number - _State |= _Getint(_First, _Last, 1, 12, _Pt->tm_mon, _Ctype_fac, 2); + _State |= _Getint(_First, _Last, 1, 12, _Pt->tm_mon, _Ctype_fac); --_Pt->tm_mon; } else if (_Dorder == ymd) { - _State |= _Getint(_First, _Last, 1, 31, _Pt->tm_mday, _Ctype_fac, 2); + _State |= _Getint(_First, _Last, 1, 31, _Pt->tm_mday, _Ctype_fac); } else { // mdy or dmy _First = get_year(_First, _Last, _Iosbase, _State, _Pt); } @@ -354,7 +354,7 @@ protected: const _Ctype& _Ctype_fac = _STD use_facet<_Ctype>(_Iosbase.getloc()); int _Ans = 0; - ios_base::iostate _Res = _Getint(_First, _Last, 0, 9999, _Ans, _Ctype_fac, 4); + ios_base::iostate _Res = _Getint(_First, _Last, 0, 9999, _Ans, _Ctype_fac); _State |= _Res; // pass on eofbit and failbit if (!(_Res & ios_base::failbit)) { @@ -394,7 +394,7 @@ protected: break; case 'C': - _State |= _Getint(_First, _Last, 0, 99, _Ans, _Ctype_fac, 2); + _State |= _Getint(_First, _Last, 0, 99, _Ans, _Ctype_fac); if (!(_State & ios_base::failbit)) { _Pt->tm_year = _Ans * 100 - 1900; // convert to century } @@ -403,7 +403,7 @@ protected: case 'd': case 'e': - _State |= _Getint(_First, _Last, 1, 31, _Pt->tm_mday, _Ctype_fac, 2); + _State |= _Getint(_First, _Last, 1, 31, _Pt->tm_mday, _Ctype_fac); break; case 'D': @@ -411,11 +411,11 @@ protected: break; case 'H': - _State |= _Getint(_First, _Last, 0, 23, _Pt->tm_hour, _Ctype_fac, 2); + _State |= _Getint(_First, _Last, 0, 23, _Pt->tm_hour, _Ctype_fac); break; case 'I': - _State |= _Getint(_First, _Last, 1, 12, _Ans, _Ctype_fac, 2); + _State |= _Getint(_First, _Last, 1, 12, _Ans, _Ctype_fac); if (!(_State & ios_base::failbit)) { _Pt->tm_hour = _Ans == 12 ? 0 : _Ans; } @@ -423,11 +423,11 @@ protected: break; case 'j': - _State |= _Getint(_First, _Last, 1, 366, _Pt->tm_yday, _Ctype_fac, 3); + _State |= _Getint(_First, _Last, 1, 366, _Pt->tm_yday, _Ctype_fac); break; case 'm': - _State |= _Getint(_First, _Last, 1, 12, _Ans, _Ctype_fac, 2); + _State |= _Getint(_First, _Last, 1, 12, _Ans, _Ctype_fac); if (!(_State & ios_base::failbit)) { _Pt->tm_mon = _Ans - 1; } @@ -435,7 +435,7 @@ protected: break; case 'M': - _State |= _Getint(_First, _Last, 0, 59, _Pt->tm_min, _Ctype_fac, 2); + _State |= _Getint(_First, _Last, 0, 59, _Pt->tm_min, _Ctype_fac); break; case 'n': @@ -462,7 +462,7 @@ protected: break; case 'S': - _State |= _Getint(_First, _Last, 0, 60, _Pt->tm_sec, _Ctype_fac, 2); + _State |= _Getint(_First, _Last, 0, 60, _Pt->tm_sec, _Ctype_fac); break; case 'T': @@ -471,15 +471,15 @@ protected: break; case 'U': - _State |= _Getint(_First, _Last, 0, 53, _Pt->tm_yday, _Ctype_fac, 2); + _State |= _Getint(_First, _Last, 0, 53, _Pt->tm_yday, _Ctype_fac); break; case 'w': - _State |= _Getint(_First, _Last, 0, 6, _Pt->tm_wday, _Ctype_fac, 1); + _State |= _Getint(_First, _Last, 0, 6, _Pt->tm_wday, _Ctype_fac); break; case 'W': - _State |= _Getint(_First, _Last, 0, 53, _Pt->tm_yday, _Ctype_fac, 2); + _State |= _Getint(_First, _Last, 0, 53, _Pt->tm_yday, _Ctype_fac); break; case 'x': @@ -487,7 +487,7 @@ protected: break; case 'y': - _State |= _Getint(_First, _Last, 0, 99, _Ans, _Ctype_fac, 2); + _State |= _Getint(_First, _Last, 0, 99, _Ans, _Ctype_fac); if (!(_State & ios_base::failbit)) { _Pt->tm_year = _Ans < 69 ? _Ans + 100 : _Ans; } @@ -538,8 +538,11 @@ protected: private: ios_base::iostate __CLRCALL_OR_CDECL _Getint(_InIt& _First, _InIt& _Last, int _Lo, int _Hi, int& _Val, - const _Ctype& _Ctype_fac, - const int _MaxLen = -1) const { // get integer in range [_Lo, _Hi] from [_First, _Last) + const _Ctype& _Ctype_fac) const { // get integer in range [_Lo, _Hi] from [_First, _Last) + const int _NHiDigits = + (_Hi <= 9 ? 1 + : _Hi <= 99 ? 2 + : _Hi <= 999 ? 3 : _Hi <= 9999 ? 4 : static_cast(_STD log10(_STD abs(_Hi))) + 1); char _Ac[_MAX_INT_DIG]; char* _Ep; char* _Ptr = _Ac; @@ -555,27 +558,27 @@ private: } } - int _Seendigit = 0; + int _NSeendigit = 0; for (; _First != _Last && _Ctype_fac.narrow(*_First) == '0'; ++_First) { // strip leading zeros - ++_Seendigit; + ++_NSeendigit; } - if (_Seendigit > 0) { - *_Ptr++ = '0'; // replace one or more with single zero - _Seendigit = 1; + if (_NSeendigit > 0) { + *_Ptr++ = '0'; // replace one or more with single zero + _NSeendigit = 1; } - for (char* const _Pe = &_Ac[_MAX_INT_DIG - 1]; _First != _Last && '0' <= (_Ch = _Ctype_fac.narrow(*_First)) - && _Ch <= '9' && (_MaxLen == -1 || _Seendigit < _MaxLen); - ++_Seendigit, (void) ++_First) { // copy digits + for (char* const _Pe = &_Ac[_MAX_INT_DIG - 1]; + _First != _Last && '0' <= (_Ch = _Ctype_fac.narrow(*_First)) && _Ch <= '9' && _NSeendigit < _NHiDigits; + ++_NSeendigit, (void) ++_First) { // copy digits *_Ptr = _Ch; if (_Ptr < _Pe) { ++_Ptr; // drop trailing digits if already too large } } - if (_Seendigit == 0) { + if (_NSeendigit == 0) { _Ptr = _Ac; } From 9d77a3baef15c78ac954c9b6a021ad896bcbd5e6 Mon Sep 17 00:00:00 2001 From: Hamid Reza Arzaghi Date: Thu, 24 Sep 2020 09:35:21 +0330 Subject: [PATCH 4/7] add test cases --- .../std/tests/Dev11_0836436_get_time/test.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/std/tests/Dev11_0836436_get_time/test.cpp b/tests/std/tests/Dev11_0836436_get_time/test.cpp index f463db4714b..ed19c4f415c 100644 --- a/tests/std/tests/Dev11_0836436_get_time/test.cpp +++ b/tests/std/tests/Dev11_0836436_get_time/test.cpp @@ -394,6 +394,24 @@ void test_990695() { assert(t.tm_year == 120); } + { + istringstream iss("202000000000000923"); + tm t = {}; + const string fmt("%Y%m%d"); + iss >> get_time(&t, fmt.c_str()); + assert(iss.fail()); + } + + { + istringstream iss("202000000000000923"); + ios_base::iostate err = Bit; + tm t{}; + const string fmt("%Y%m%d"); + use_facet>(iss.getloc()) + .get(Iter(iss.rdbuf()), Iter(), iss, err, &t, fmt.c_str(), fmt.c_str() + fmt.size()); + assert(err == ios_base::failbit); + } + { // This case should fail istringstream iss("2011-D-18"); From 40f2339754f12af62f4077ce59993d5046ac2e23 Mon Sep 17 00:00:00 2001 From: Hamid Reza Arzaghi Date: Thu, 24 Sep 2020 09:37:30 +0330 Subject: [PATCH 5/7] resolve a bug and improve variables name --- stl/inc/xloctime | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/stl/inc/xloctime b/stl/inc/xloctime index 83f590674af..e3ce0dbf209 100644 --- a/stl/inc/xloctime +++ b/stl/inc/xloctime @@ -539,7 +539,7 @@ protected: private: ios_base::iostate __CLRCALL_OR_CDECL _Getint(_InIt& _First, _InIt& _Last, int _Lo, int _Hi, int& _Val, const _Ctype& _Ctype_fac) const { // get integer in range [_Lo, _Hi] from [_First, _Last) - const int _NHiDigits = + const int _Hi_digits = (_Hi <= 9 ? 1 : _Hi <= 99 ? 2 : _Hi <= 999 ? 3 : _Hi <= 9999 ? 4 : static_cast(_STD log10(_STD abs(_Hi))) + 1); @@ -558,27 +558,26 @@ private: } } - int _NSeendigit = 0; + int _Digits_seen = 0; for (; _First != _Last && _Ctype_fac.narrow(*_First) == '0'; ++_First) { // strip leading zeros - ++_NSeendigit; + ++_Digits_seen; } - if (_NSeendigit > 0) { - *_Ptr++ = '0'; // replace one or more with single zero - _NSeendigit = 1; + if (_Digits_seen > 0) { + *_Ptr++ = '0'; // replace one or more with single zero } for (char* const _Pe = &_Ac[_MAX_INT_DIG - 1]; - _First != _Last && '0' <= (_Ch = _Ctype_fac.narrow(*_First)) && _Ch <= '9' && _NSeendigit < _NHiDigits; - ++_NSeendigit, (void) ++_First) { // copy digits + _First != _Last && '0' <= (_Ch = _Ctype_fac.narrow(*_First)) && _Ch <= '9' && _Digits_seen < _Hi_digits; + ++_Digits_seen, (void) ++_First) { // copy digits *_Ptr = _Ch; if (_Ptr < _Pe) { ++_Ptr; // drop trailing digits if already too large } } - if (_NSeendigit == 0) { + if (_Digits_seen == 0) { _Ptr = _Ac; } From 74030dad9153052cee5d72ed606dfa3492fca5c3 Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Tue, 27 Oct 2020 00:11:10 -0700 Subject: [PATCH 6/7] Simplify _Hi_digits. --- stl/inc/xloctime | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/stl/inc/xloctime b/stl/inc/xloctime index 923d0920449..55cb9f0df2d 100644 --- a/stl/inc/xloctime +++ b/stl/inc/xloctime @@ -539,10 +539,8 @@ protected: private: ios_base::iostate __CLRCALL_OR_CDECL _Getint(_InIt& _First, _InIt& _Last, int _Lo, int _Hi, int& _Val, const _Ctype& _Ctype_fac) const { // get integer in range [_Lo, _Hi] from [_First, _Last) - const int _Hi_digits = - (_Hi <= 9 ? 1 - : _Hi <= 99 ? 2 - : _Hi <= 999 ? 3 : _Hi <= 9999 ? 4 : static_cast(_STD log10(_STD abs(_Hi))) + 1); + _STL_INTERNAL_CHECK(0 <= _Hi && _Hi <= 9999); + const int _Hi_digits = (_Hi <= 9 ? 1 : _Hi <= 99 ? 2 : _Hi <= 999 ? 3 : 4); char _Ac[_MAX_INT_DIG]; char* _Ep; char* _Ptr = _Ac; From a52b3c9dcabfc22685d71674c0a9ef437c21e5f6 Mon Sep 17 00:00:00 2001 From: Hamid Reza Arzaghi Date: Wed, 28 Oct 2020 16:45:17 +0330 Subject: [PATCH 7/7] add one more test --- .../std/tests/Dev11_0836436_get_time/test.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/std/tests/Dev11_0836436_get_time/test.cpp b/tests/std/tests/Dev11_0836436_get_time/test.cpp index 468692d7c10..2d704e204dd 100644 --- a/tests/std/tests/Dev11_0836436_get_time/test.cpp +++ b/tests/std/tests/Dev11_0836436_get_time/test.cpp @@ -302,6 +302,18 @@ void test_990695() { assert(t.tm_year == 120); } + { + istringstream iss("2020092Text"); + ios_base::iostate err = Bit; + tm t{}; + const string fmt("%Y%m%d"); + use_facet>(iss.getloc()) + .get(Iter(iss.rdbuf()), Iter(), iss, err, &t, fmt.c_str(), fmt.c_str() + fmt.size()); + assert(t.tm_mon == 8); + assert(t.tm_mday == 2); + assert(t.tm_year == 120); + } + { istringstream iss("sep 31 2014"); ios_base::iostate err = Bit; @@ -394,6 +406,17 @@ void test_990695() { assert(t.tm_year == 120); } + { + istringstream iss("2020104Text"); + tm t = {}; + const string fmt("%Y%d%m"); + iss >> get_time(&t, fmt.c_str()); + assert(!iss.fail()); + assert(t.tm_mon == 3); + assert(t.tm_mday == 10); + assert(t.tm_year == 120); + } + { istringstream iss("202000000000000923"); tm t = {};