diff --git a/stl/inc/chrono b/stl/inc/chrono index 2a86c9aded5..d6558177e2f 100644 --- a/stl/inc/chrono +++ b/stl/inc/chrono @@ -1725,9 +1725,15 @@ namespace chrono { _EXPORT_STD class _NODISCARD nonexistent_local_time : public runtime_error { public: +#if _HAS_EXCEPTIONS template nonexistent_local_time(const local_time<_Duration>& _Tp, const local_info& _Info) : runtime_error(_Make_string(_Tp, _Info)) {} +#else // ^^^ _HAS_EXCEPTIONS / !_HAS_EXCEPTIONS vvv + template + nonexistent_local_time(const local_time<_Duration>&, const local_info&) + : runtime_error("nonexistent local time") {} +#endif // ^^^ !_HAS_EXCEPTIONS ^^^ private: template @@ -1741,9 +1747,14 @@ namespace chrono { _EXPORT_STD class _NODISCARD ambiguous_local_time : public runtime_error { public: +#if _HAS_EXCEPTIONS template ambiguous_local_time(const local_time<_Duration>& _Tp, const local_info& _Info) : runtime_error(_Make_string(_Tp, _Info)) {} +#else // ^^^ _HAS_EXCEPTIONS / !_HAS_EXCEPTIONS vvv + template + ambiguous_local_time(const local_time<_Duration>&, const local_info&) : runtime_error("ambiguous local time") {} +#endif // ^^^ !_HAS_EXCEPTIONS ^^^ private: template diff --git a/stl/inc/system_error b/stl/inc/system_error index bcf2ca0d88f..4a263e93ee5 100644 --- a/stl/inc/system_error +++ b/stl/inc/system_error @@ -470,10 +470,16 @@ private: } protected: +#if _HAS_EXCEPTIONS _System_error(error_code _Errcode) : runtime_error(_Errcode.message()), _Mycode(_Errcode) {} _System_error(error_code _Errcode, const string& _Message) : runtime_error(_Makestr(_Errcode, _Message)), _Mycode(_Errcode) {} +#else // ^^^ _HAS_EXCEPTIONS / !_HAS_EXCEPTIONS vvv + _System_error(error_code _Errcode) : runtime_error("system error"), _Mycode(_Errcode) {} + + _System_error(error_code _Errcode, const string&) : runtime_error("system error"), _Mycode(_Errcode) {} +#endif // ^^^ !_HAS_EXCEPTIONS ^^^ error_code _Mycode; // the stored error code }; diff --git a/tests/std/test.lst b/tests/std/test.lst index 292e4255c97..969295ae89c 100644 --- a/tests/std/test.lst +++ b/tests/std/test.lst @@ -257,6 +257,7 @@ tests\GH_004930_char_traits_user_specialization tests\GH_005090_stl_hardening tests\GH_005204_regex_collating_ranges tests\GH_005244_regex_escape_sequences +tests\GH_005276_system_error_heap_use_after_free tests\GH_005315_destructor_tombstones tests\LWG2381_num_get_floating_point tests\LWG2597_complex_branch_cut diff --git a/tests/std/tests/GH_005276_system_error_heap_use_after_free/env.lst b/tests/std/tests/GH_005276_system_error_heap_use_after_free/env.lst new file mode 100644 index 00000000000..ebe0f92582a --- /dev/null +++ b/tests/std/tests/GH_005276_system_error_heap_use_after_free/env.lst @@ -0,0 +1,6 @@ +# Copyright (c) Microsoft Corporation. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +RUNALL_INCLUDE ..\usual_20_matrix.lst +RUNALL_CROSSLIST +* PM_CL="/D_HAS_EXCEPTIONS=0" diff --git a/tests/std/tests/GH_005276_system_error_heap_use_after_free/test.cpp b/tests/std/tests/GH_005276_system_error_heap_use_after_free/test.cpp new file mode 100644 index 00000000000..a1444a99678 --- /dev/null +++ b/tests/std/tests/GH_005276_system_error_heap_use_after_free/test.cpp @@ -0,0 +1,49 @@ +// Copyright (c) Microsoft Corporation. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#include +#include +#include +#include +#include +using namespace std; +using namespace chrono; + +// GH-5276 : heap-use-after-free for _HAS_EXCEPTIONS=0 +int main() { + const string str{"abc"}; + const error_code ec{2, system_category()}; + { + system_error syserr1{ec}; + assert(syserr1.what() == "system error"sv); + } + { + system_error syserr2{ec, str}; + assert(syserr2.what() == "system error"sv); + } + { + system_error syserr3{ec, "meow"}; + assert(syserr3.what() == "system error"sv); + } + { + system_error syserr4{2, system_category()}; + assert(syserr4.what() == "system error"sv); + } + { + system_error syserr5{2, system_category(), str}; + assert(syserr5.what() == "system error"sv); + } + { + system_error syserr6{2, system_category(), "meow"}; + assert(syserr6.what() == "system error"sv); + } + + { + ambiguous_local_time alt{local_seconds{}, local_info{}}; + assert(alt.what() == "ambiguous local time"sv); + } + { + nonexistent_local_time nlt{local_seconds{}, local_info{}}; + assert(nlt.what() == "nonexistent local time"sv); + } +}