Skip to content

Commit bb031e2

Browse files
Fix heap-use-after-free for _HAS_EXCEPTIONS=0 (#5406)
Co-authored-by: wolframw <[email protected]> Co-authored-by: Stephan T. Lavavej <[email protected]>
1 parent ed33e92 commit bb031e2

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-0
lines changed

stl/inc/chrono

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,9 +1725,15 @@ namespace chrono {
17251725

17261726
_EXPORT_STD class _NODISCARD nonexistent_local_time : public runtime_error {
17271727
public:
1728+
#if _HAS_EXCEPTIONS
17281729
template <class _Duration>
17291730
nonexistent_local_time(const local_time<_Duration>& _Tp, const local_info& _Info)
17301731
: runtime_error(_Make_string(_Tp, _Info)) {}
1732+
#else // ^^^ _HAS_EXCEPTIONS / !_HAS_EXCEPTIONS vvv
1733+
template <class _Duration>
1734+
nonexistent_local_time(const local_time<_Duration>&, const local_info&)
1735+
: runtime_error("nonexistent local time") {}
1736+
#endif // ^^^ !_HAS_EXCEPTIONS ^^^
17311737

17321738
private:
17331739
template <class _Duration>
@@ -1741,9 +1747,14 @@ namespace chrono {
17411747

17421748
_EXPORT_STD class _NODISCARD ambiguous_local_time : public runtime_error {
17431749
public:
1750+
#if _HAS_EXCEPTIONS
17441751
template <class _Duration>
17451752
ambiguous_local_time(const local_time<_Duration>& _Tp, const local_info& _Info)
17461753
: runtime_error(_Make_string(_Tp, _Info)) {}
1754+
#else // ^^^ _HAS_EXCEPTIONS / !_HAS_EXCEPTIONS vvv
1755+
template <class _Duration>
1756+
ambiguous_local_time(const local_time<_Duration>&, const local_info&) : runtime_error("ambiguous local time") {}
1757+
#endif // ^^^ !_HAS_EXCEPTIONS ^^^
17471758

17481759
private:
17491760
template <class _Duration>

stl/inc/system_error

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,10 +470,16 @@ private:
470470
}
471471

472472
protected:
473+
#if _HAS_EXCEPTIONS
473474
_System_error(error_code _Errcode) : runtime_error(_Errcode.message()), _Mycode(_Errcode) {}
474475

475476
_System_error(error_code _Errcode, const string& _Message)
476477
: runtime_error(_Makestr(_Errcode, _Message)), _Mycode(_Errcode) {}
478+
#else // ^^^ _HAS_EXCEPTIONS / !_HAS_EXCEPTIONS vvv
479+
_System_error(error_code _Errcode) : runtime_error("system error"), _Mycode(_Errcode) {}
480+
481+
_System_error(error_code _Errcode, const string&) : runtime_error("system error"), _Mycode(_Errcode) {}
482+
#endif // ^^^ !_HAS_EXCEPTIONS ^^^
477483

478484
error_code _Mycode; // the stored error code
479485
};

tests/std/test.lst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ tests\GH_005090_stl_hardening
258258
tests\GH_005204_regex_collating_ranges
259259
tests\GH_005236_collate_facet
260260
tests\GH_005244_regex_escape_sequences
261+
tests\GH_005276_system_error_heap_use_after_free
261262
tests\GH_005315_destructor_tombstones
262263
tests\LWG2381_num_get_floating_point
263264
tests\LWG2597_complex_branch_cut
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
RUNALL_INCLUDE ..\usual_20_matrix.lst
5+
RUNALL_CROSSLIST
6+
* PM_CL="/D_HAS_EXCEPTIONS=0"
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
#include <cassert>
5+
#include <chrono>
6+
#include <string>
7+
#include <string_view>
8+
#include <system_error>
9+
using namespace std;
10+
using namespace chrono;
11+
12+
// GH-5276 <system_error>: heap-use-after-free for _HAS_EXCEPTIONS=0
13+
int main() {
14+
const string str{"abc"};
15+
const error_code ec{2, system_category()};
16+
{
17+
system_error syserr1{ec};
18+
assert(syserr1.what() == "system error"sv);
19+
}
20+
{
21+
system_error syserr2{ec, str};
22+
assert(syserr2.what() == "system error"sv);
23+
}
24+
{
25+
system_error syserr3{ec, "meow"};
26+
assert(syserr3.what() == "system error"sv);
27+
}
28+
{
29+
system_error syserr4{2, system_category()};
30+
assert(syserr4.what() == "system error"sv);
31+
}
32+
{
33+
system_error syserr5{2, system_category(), str};
34+
assert(syserr5.what() == "system error"sv);
35+
}
36+
{
37+
system_error syserr6{2, system_category(), "meow"};
38+
assert(syserr6.what() == "system error"sv);
39+
}
40+
41+
{
42+
ambiguous_local_time alt{local_seconds{}, local_info{}};
43+
assert(alt.what() == "ambiguous local time"sv);
44+
}
45+
{
46+
nonexistent_local_time nlt{local_seconds{}, local_info{}};
47+
assert(nlt.what() == "nonexistent local time"sv);
48+
}
49+
}

0 commit comments

Comments
 (0)