diff --git a/stl/inc/xlocnum b/stl/inc/xlocnum index fca7454e0b..c44f552c1e 100644 --- a/stl/inc/xlocnum +++ b/stl/inc/xlocnum @@ -1313,8 +1313,16 @@ protected: char _Buf[2 * _Max_int_dig]; char _Fmt[6]; - return _Iput(_Dest, _Iosbase, _Fill, _Buf, - static_cast(_CSTD sprintf_s(_Buf, sizeof(_Buf), _Ifmt(_Fmt, "ld", _Iosbase.flags()), _Val))); + (void) _Ifmt(_Fmt, "ld", _Iosbase.flags()); + + const int _Count = _CSTD sprintf_s(_Buf, sizeof(_Buf), _Fmt, _Val); + + if (_Count < 0) { // It should be impossible for sprintf_s() to fail here. + _STL_INTERNAL_CHECK(false); // In STL tests, report an error. + return _Dest; // In production, return early to avoid static_cast of a negative _Count. + } + + return _Iput(_Dest, _Iosbase, _Fill, _Buf, static_cast(_Count)); } virtual _OutIt __CLR_OR_THIS_CALL do_put(_OutIt _Dest, ios_base& _Iosbase, _Elem _Fill, @@ -1322,8 +1330,16 @@ protected: char _Buf[2 * _Max_int_dig]; char _Fmt[6]; - return _Iput(_Dest, _Iosbase, _Fill, _Buf, - static_cast(_CSTD sprintf_s(_Buf, sizeof(_Buf), _Ifmt(_Fmt, "lu", _Iosbase.flags()), _Val))); + (void) _Ifmt(_Fmt, "lu", _Iosbase.flags()); + + const int _Count = _CSTD sprintf_s(_Buf, sizeof(_Buf), _Fmt, _Val); + + if (_Count < 0) { // It should be impossible for sprintf_s() to fail here. + _STL_INTERNAL_CHECK(false); // In STL tests, report an error. + return _Dest; // In production, return early to avoid static_cast of a negative _Count. + } + + return _Iput(_Dest, _Iosbase, _Fill, _Buf, static_cast(_Count)); } virtual _OutIt __CLR_OR_THIS_CALL do_put( @@ -1331,8 +1347,16 @@ protected: char _Buf[2 * _Max_int_dig]; char _Fmt[8]; - return _Iput(_Dest, _Iosbase, _Fill, _Buf, - static_cast(_CSTD sprintf_s(_Buf, sizeof(_Buf), _Ifmt(_Fmt, "Ld", _Iosbase.flags()), _Val))); + (void) _Ifmt(_Fmt, "Ld", _Iosbase.flags()); + + const int _Count = _CSTD sprintf_s(_Buf, sizeof(_Buf), _Fmt, _Val); + + if (_Count < 0) { // It should be impossible for sprintf_s() to fail here. + _STL_INTERNAL_CHECK(false); // In STL tests, report an error. + return _Dest; // In production, return early to avoid static_cast of a negative _Count. + } + + return _Iput(_Dest, _Iosbase, _Fill, _Buf, static_cast(_Count)); } virtual _OutIt __CLR_OR_THIS_CALL do_put(_OutIt _Dest, ios_base& _Iosbase, _Elem _Fill, @@ -1340,8 +1364,16 @@ protected: char _Buf[2 * _Max_int_dig]; char _Fmt[8]; - return _Iput(_Dest, _Iosbase, _Fill, _Buf, - static_cast(_CSTD sprintf_s(_Buf, sizeof(_Buf), _Ifmt(_Fmt, "Lu", _Iosbase.flags()), _Val))); + (void) _Ifmt(_Fmt, "Lu", _Iosbase.flags()); + + const int _Count = _CSTD sprintf_s(_Buf, sizeof(_Buf), _Fmt, _Val); + + if (_Count < 0) { // It should be impossible for sprintf_s() to fail here. + _STL_INTERNAL_CHECK(false); // In STL tests, report an error. + return _Dest; // In production, return early to avoid static_cast of a negative _Count. + } + + return _Iput(_Dest, _Iosbase, _Fill, _Buf, static_cast(_Count)); } virtual _OutIt __CLR_OR_THIS_CALL do_put( @@ -1365,10 +1397,17 @@ protected: const bool _Is_finite = (_STD isfinite) (_Val); const auto _Adjusted_flags = // TRANSITION, DevCom-10519861 _Is_finite ? _Iosbase.flags() : _Iosbase.flags() & ~ios_base::showpoint; - const auto _Ngen = static_cast(_CSTD sprintf_s( - &_Buf[0], _Buf.size(), _Ffmt(_Fmt, 0, _Adjusted_flags), static_cast(_Precision), _Val)); - return _Fput_v3(_Dest, _Iosbase, _Fill, _Buf.c_str(), _Ngen, _Is_finite); + (void) _Ffmt(_Fmt, 0, _Adjusted_flags); + + const int _Count = _CSTD sprintf_s(&_Buf[0], _Buf.size(), _Fmt, static_cast(_Precision), _Val); + + if (_Count < 0) { // It should be impossible for sprintf_s() to fail here. + _STL_INTERNAL_CHECK(false); // In STL tests, report an error. + return _Dest; // In production, return early to avoid static_cast of a negative _Count. + } + + return _Fput_v3(_Dest, _Iosbase, _Fill, _Buf.c_str(), static_cast(_Count), _Is_finite); } virtual _OutIt __CLR_OR_THIS_CALL do_put( @@ -1392,10 +1431,17 @@ protected: const bool _Is_finite = (_STD isfinite) (_Val); const auto _Adjusted_flags = // TRANSITION, DevCom-10519861 _Is_finite ? _Iosbase.flags() : _Iosbase.flags() & ~ios_base::showpoint; - const auto _Ngen = static_cast(_CSTD sprintf_s( - &_Buf[0], _Buf.size(), _Ffmt(_Fmt, 'L', _Adjusted_flags), static_cast(_Precision), _Val)); - return _Fput_v3(_Dest, _Iosbase, _Fill, _Buf.c_str(), _Ngen, _Is_finite); + (void) _Ffmt(_Fmt, 'L', _Adjusted_flags); + + const int _Count = _CSTD sprintf_s(&_Buf[0], _Buf.size(), _Fmt, static_cast(_Precision), _Val); + + if (_Count < 0) { // It should be impossible for sprintf_s() to fail here. + _STL_INTERNAL_CHECK(false); // In STL tests, report an error. + return _Dest; // In production, return early to avoid static_cast of a negative _Count. + } + + return _Fput_v3(_Dest, _Iosbase, _Fill, _Buf.c_str(), static_cast(_Count), _Is_finite); } #pragma warning(pop) @@ -1403,13 +1449,20 @@ protected: _OutIt _Dest, ios_base& _Iosbase, _Elem _Fill, const void* _Val) const { // put formatted void pointer to _Dest char _Buf[2 * _Max_int_dig]; - return _Iput( - _Dest, _Iosbase, _Fill, _Buf, static_cast(_CSTD sprintf_s(_Buf, sizeof(_Buf), "%p", _Val))); + const int _Count = _CSTD sprintf_s(_Buf, sizeof(_Buf), "%p", _Val); + + if (_Count < 0) { // It should be impossible for sprintf_s() to fail here. + _STL_INTERNAL_CHECK(false); // In STL tests, report an error. + return _Dest; // In production, return early to avoid static_cast of a negative _Count. + } + + return _Iput(_Dest, _Iosbase, _Fill, _Buf, static_cast(_Count)); } private: - char* __CLRCALL_OR_CDECL _Ffmt( - char* _Fmt, char _Spec, ios_base::fmtflags _Flags) const { // generate sprintf format for floating-point + // TRANSITION, ABI: This always returns _Fmt, which is now ignored. + char* __CLRCALL_OR_CDECL _Ffmt(char* _Fmt, char _Spec, ios_base::fmtflags _Flags) const { + // generate sprintf format for floating-point char* _Ptr = _Fmt; *_Ptr++ = '%'; @@ -1531,8 +1584,9 @@ private: return _Rep(_Dest, _Fill, _Fillcount); // put trailing fill } - char* __CLRCALL_OR_CDECL _Ifmt( - char* _Fmt, const char* _Spec, ios_base::fmtflags _Flags) const { // generate sprintf format for integer + // TRANSITION, ABI: This always returns _Fmt, which is now ignored. + char* __CLRCALL_OR_CDECL _Ifmt(char* _Fmt, const char* _Spec, ios_base::fmtflags _Flags) const { + // generate sprintf format for integer char* _Ptr = _Fmt; *_Ptr++ = '%';