Reactos
1//
2// terminate.cpp
3//
4// Copyright (c) Microsoft Corporation. All rights reserved.
5//
6// The terminate handler
7//
8#include <corecrt_internal.h>
9#include <corecrt_terminate.h>
10
11
12
13static terminate_handler __cdecl get_terminate_or_default(
14 __acrt_ptd const* const ptd
15 ) throw()
16{
17 return ptd->_terminate ? ptd->_terminate : &abort;
18}
19
20extern "C" terminate_handler __cdecl _get_terminate()
21{
22 return get_terminate_or_default(__acrt_getptd());
23}
24
25extern "C" terminate_handler __cdecl set_terminate(
26 terminate_handler const new_handler
27 ) throw()
28{
29 __acrt_ptd* const ptd = __acrt_getptd();
30
31 terminate_handler const old_handler = get_terminate_or_default(ptd);
32
33 ptd->_terminate = new_handler;
34
35 return old_handler;
36}
37
38extern "C" void __cdecl terminate() throw()
39{
40 terminate_handler const handler = __acrt_getptd()->_terminate;
41 if (handler)
42 {
43 // Note: We cannot allow any exceptions to propagate from a user-
44 // registered terminate handler, so if any structured exception escapes
45 // the user handler we abort.
46 __try
47 {
48 handler();
49 }
50 __except(EXCEPTION_EXECUTE_HANDLER)
51 {
52 ; // Deliberately do nothing
53 }
54 __endtry
55 }
56
57 // If the terminate handler returned, faulted, or otherwise failed to end
58 // execution, we will do it:
59 abort();
60}