Skip to content

Commit 66ee94f

Browse files
committed
Fix std::put_time() crash on invalid format specifier
1 parent ecbc1ef commit 66ee94f

File tree

2 files changed

+76
-5
lines changed

2 files changed

+76
-5
lines changed

stl/inc/xloctime

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,17 @@ protected:
651651
__CLR_OR_THIS_CALL ~time_get_byname() noexcept override {}
652652
};
653653

654+
constexpr bool _Is_valid_fmt_specifier(const char _Specifier) {
655+
constexpr char _Valid_specifiers[] = "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ";
656+
for (const char _Valid_specifier : _Valid_specifiers) {
657+
if (_Valid_specifier == _Specifier) {
658+
return true;
659+
}
660+
}
661+
662+
return false;
663+
}
664+
654665
_EXPORT_STD extern "C++" template <class _Elem, class _OutIt = ostreambuf_iterator<_Elem, char_traits<_Elem>>>
655666
class time_put : public locale::facet { // facet for converting encoded times to text
656667
public:
@@ -687,7 +698,19 @@ public:
687698
_Specifier = _Ctype_fac.narrow(*_Fmtfirst);
688699
}
689700

690-
_Dest = do_put(_Dest, _Iosbase, _Fill, _Pt, _Specifier, _Modifier); // convert a single field
701+
if (_Specifier == '%' && _Modifier == '\0') {
702+
// if the specifier is percent and no modifier is set, just append it
703+
*_Dest++ = _Percent;
704+
} else if (!_Is_valid_fmt_specifier(_Specifier)) {
705+
// no valid specifier, directly copy as literal elements
706+
*_Dest++ = _Percent;
707+
if (_Modifier != '\0') {
708+
*_Dest++ = _Modifier;
709+
}
710+
*_Dest++ = _Specifier;
711+
} else {
712+
_Dest = do_put(_Dest, _Iosbase, _Fill, _Pt, _Specifier, _Modifier); // convert a single field
713+
}
691714
}
692715
}
693716

@@ -811,7 +834,19 @@ public:
811834
_Specifier = _Ctype_fac.narrow(*_Fmtfirst);
812835
}
813836

814-
_Dest = do_put(_Dest, _Iosbase, _Fill, _Pt, _Specifier, _Modifier); // convert a single field
837+
if (_Specifier == '%' && _Modifier == '\0') {
838+
// if the specifier is percent and no modifier is set, just append it
839+
*_Dest++ = _Percent;
840+
} else if (!_Is_valid_fmt_specifier(_Specifier)) {
841+
// no valid specifier, directly copy as literal elements
842+
*_Dest++ = _Percent;
843+
*_Dest++ = _Raw;
844+
if (_Modifier != '\0') {
845+
*_Dest++ = *_Fmtfirst;
846+
}
847+
} else {
848+
_Dest = do_put(_Dest, _Iosbase, _Fill, _Pt, _Specifier, _Modifier); // convert a single field
849+
}
815850
}
816851
}
817852

@@ -943,7 +978,19 @@ public:
943978
_Specifier = _Ctype_fac.narrow(*_Fmtfirst);
944979
}
945980

946-
_Dest = do_put(_Dest, _Iosbase, _Fill, _Pt, _Specifier, _Modifier); // convert a single field
981+
if (_Specifier == '%' && _Modifier == '\0') {
982+
// if the specifier is percent and no modifier is set, just append it
983+
*_Dest++ = _Percent;
984+
} else if (!_Is_valid_fmt_specifier(
985+
_Specifier)) { // no valid specifier, directly copy as literal elements
986+
*_Dest++ = _Percent;
987+
if (_Modifier != '\0') {
988+
*_Dest++ = _Modifier;
989+
}
990+
*_Dest++ = _Specifier;
991+
} else {
992+
_Dest = do_put(_Dest, _Iosbase, _Fill, _Pt, _Specifier, _Modifier); // convert a single field
993+
}
947994
}
948995
}
949996

tests/std/tests/Dev11_0836436_get_time/test.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ void test_invalid_argument();
110110
void test_buffer_resizing();
111111
void test_gh_2618();
112112
void test_gh_2848();
113+
void test_gh_4820();
113114

114115
int main() {
115116
assert(read_hour("12 AM") == 0);
@@ -157,6 +158,7 @@ int main() {
157158
test_buffer_resizing();
158159
test_gh_2618();
159160
test_gh_2848();
161+
test_gh_4820();
160162
}
161163

162164
typedef istreambuf_iterator<char> Iter;
@@ -792,16 +794,17 @@ void test_invalid_argument() {
792794
time_t t = time(nullptr);
793795
tm currentTime;
794796
localtime_s(&currentTime, &t);
797+
currentTime.tm_hour = 25; // set invalid hour
795798

796799
{
797800
wstringstream wss;
798-
wss << put_time(&currentTime, L"%Y-%m-%d-%H-%M-%s");
801+
wss << put_time(&currentTime, L"%Y-%m-%d-%H-%M");
799802
assert(wss.rdstate() == ios_base::badbit);
800803
}
801804

802805
{
803806
stringstream ss;
804-
ss << put_time(&currentTime, "%Y-%m-%d-%H-%M-%s");
807+
ss << put_time(&currentTime, "%Y-%m-%d-%H-%M");
805808
assert(ss.rdstate() == ios_base::badbit);
806809
}
807810
#endif // _M_CEE_PURE
@@ -905,3 +908,24 @@ void test_gh_2848() {
905908
assert(err == (ios_base::eofbit | ios_base::failbit));
906909
}
907910
}
911+
912+
void test_gh_4820() {
913+
// GH-4820 <iomanip>: std::put_time should copy unknown conversion specifiers instead of crash
914+
time_t t = time(nullptr);
915+
tm currentTime;
916+
localtime_s(&currentTime, &t);
917+
918+
{
919+
wstringstream wss;
920+
wss << put_time(&currentTime, L"%Ei%!%E%J%P");
921+
assert(wss.rdstate() == ios_base::goodbit);
922+
assert(wss.str() == L"%Ei%!%E%J%P");
923+
}
924+
925+
{
926+
stringstream ss;
927+
ss << put_time(&currentTime, "%Ei%!%E%J%P");
928+
assert(ss.rdstate() == ios_base::goodbit);
929+
assert(ss.str() == "%Ei%!%E%J%P");
930+
}
931+
}

0 commit comments

Comments
 (0)