ReactOS 0.4.16-dev-1104-ge0ef51b
report_runtime_error.cpp File Reference
#include <corecrt_internal.h>
#include <stdlib.h>
Include dependency graph for report_runtime_error.cpp:

Go to the source code of this file.

Macros

#define EOL   L"\r\n"
 
#define MSGTEXTPREFIX   L"Runtime Error!\n\nProgram: "
 
#define MAXLINELEN   60
 

Functions

static bool __cdecl issue_debug_notification (wchar_t const *const message) throw ()
 
void __cdecl _set_app_type (_crt_app_type const new_app_type)
 
_crt_app_type __cdecl _query_app_type ()
 
static bool __cdecl should_write_error_to_console () throw ()
 
static void write_string_to_console (wchar_t const *const wide_string) throw ()
 
void __cdecl __acrt_report_runtime_error (wchar_t const *const message)
 

Variables

static _crt_app_type __acrt_app_type = _crt_unknown_app
 

Macro Definition Documentation

◆ EOL

#define EOL   L"\r\n"

Definition at line 17 of file report_runtime_error.cpp.

◆ MAXLINELEN

#define MAXLINELEN   60

◆ MSGTEXTPREFIX

#define MSGTEXTPREFIX   L"Runtime Error!\n\nProgram: "

Function Documentation

◆ __acrt_report_runtime_error()

void __cdecl __acrt_report_runtime_error ( wchar_t const *const  message)

Definition at line 150 of file report_runtime_error.cpp.

151{
152 // Before we report the error via the normal path, report the error using
153 // the debug
155 {
156 return;
157 }
158
160 {
162 }
163 else
164 {
165 #define MSGTEXTPREFIX L"Runtime Error!\n\nProgram: "
166 static wchar_t outmsg[sizeof(MSGTEXTPREFIX) / sizeof(wchar_t) + _MAX_PATH + 2 + 500];
167 // runtime error msg + progname + 2 newline + runtime error text.
168 wchar_t* progname = &outmsg[sizeof(MSGTEXTPREFIX) / sizeof(wchar_t) - 1];
169 size_t progname_size = _countof(outmsg) - (progname - outmsg);
170 wchar_t* pch = progname;
171
172 _ERRCHECK(wcscpy_s(outmsg, _countof(outmsg), MSGTEXTPREFIX));
173
174 progname[MAX_PATH] = L'\0';
175 if (!GetModuleFileNameW(nullptr, progname, MAX_PATH))
176 {
177 _ERRCHECK(wcscpy_s(progname, progname_size, L"<program name unknown>"));
178 }
179
180 #define MAXLINELEN 60
181 if (wcslen(pch) + 1 > MAXLINELEN)
182 {
183 pch += wcslen(progname) + 1 - MAXLINELEN;
184 _ERRCHECK(wcsncpy_s(pch, progname_size - (pch - progname), L"...", 3));
185 }
186
187 _ERRCHECK(wcscat_s(outmsg, _countof(outmsg), L"\n\n"));
188 _ERRCHECK(wcscat_s(outmsg, _countof(outmsg), message));
189
190 // Okay to ignore return value here, this is just to display the message box.
191 // Only caller is abort() (so we shouldn't/can't handle IDABORT), so the process
192 // will end shortly.
194 outmsg,
195 L"Microsoft Visual C++ Runtime Library",
197 }
198}
static const char * progname
Definition: cjpeg.c:137
int __cdecl __acrt_show_wide_message_box(_In_opt_z_ wchar_t const *text, _In_opt_z_ wchar_t const *caption, _In_ unsigned type)
#define _ERRCHECK(e)
#define MAX_PATH
Definition: compat.h:34
DWORD WINAPI GetModuleFileNameW(HINSTANCE hModule, LPWSTR lpFilename, DWORD nSize)
Definition: loader.c:600
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define pch(ap)
Definition: match.c:418
#define _MAX_PATH
Definition: utility.h:77
#define wcscpy_s(d, l, s)
Definition: utility.h:201
#define wcsncpy_s(d, l, s, n)
Definition: utility.h:202
#define L(x)
Definition: ntvdm.h:50
static bool __cdecl should_write_error_to_console()
#define MSGTEXTPREFIX
static void write_string_to_console(wchar_t const *const wide_string)
#define MAXLINELEN
static bool __cdecl issue_debug_notification(wchar_t const *const message)
#define _countof(array)
Definition: sndvol32.h:70
Definition: tftpd.h:60
INT CDECL wcscat_s(wchar_t *dst, size_t elem, const wchar_t *src)
Definition: wcs.c:406
#define MB_SETFOREGROUND
Definition: winuser.h:825
#define MB_ICONHAND
Definition: winuser.h:799
#define MB_TASKMODAL
Definition: winuser.h:827
#define MB_OK
Definition: winuser.h:801

◆ _query_app_type()

_crt_app_type __cdecl _query_app_type ( void  )

Definition at line 85 of file report_runtime_error.cpp.

86{
87 return __acrt_app_type;
88}
static _crt_app_type __acrt_app_type

Referenced by __acrt_lowio_set_os_handle(), _free_osfhnd(), and common_assert().

◆ _set_app_type()

void __cdecl _set_app_type ( _crt_app_type const  new_app_type)

Definition at line 80 of file report_runtime_error.cpp.

81{
82 __acrt_app_type = new_app_type;
83}

◆ issue_debug_notification()

static bool __cdecl issue_debug_notification ( wchar_t const *const  message)
throw (
)
static

Definition at line 19 of file report_runtime_error.cpp.

20{
21 // This is referenced only in the Debug CRT build
23
24#ifdef _DEBUG
25 switch (_CrtDbgReportW(_CRT_ERROR, nullptr, 0, nullptr, L"%ls", message))
26 {
27 case 1:
29 return true;
30
31 case 0:
32 return true;
33 }
34#endif // _DEBUG
35
36 return false;
37}
#define _CRT_ERROR
Definition: crtdbg.h:21
#define _CrtDbgBreak()
Definition: crtdbg.h:103
EXTERN_C int __cdecl _CrtDbgReportW(int reportType, const wchar_t *filename, int linenumber, const wchar_t *moduleName, const wchar_t *format,...)
Definition: dbgrpt.cpp:483
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:325

Referenced by __acrt_report_runtime_error().

◆ should_write_error_to_console()

static bool __cdecl should_write_error_to_console ( )
throw (
)
static

Definition at line 92 of file report_runtime_error.cpp.

93{
94 int const error_mode = _set_error_mode(_REPORT_ERRMODE);
95
96 if (error_mode == _OUT_TO_STDERR)
97 {
98 return true;
99 }
100
101 if (error_mode == _OUT_TO_DEFAULT && __acrt_app_type == _crt_console_app)
102 {
103 return true;
104 }
105
106 return false;
107}
@ _crt_console_app
#define _REPORT_ERRMODE
Definition: stdlib.h:113
#define _OUT_TO_DEFAULT
Definition: stdlib.h:110
_Check_return_opt_ _CRTIMP int __cdecl _set_error_mode(_In_ int _Mode)
#define _OUT_TO_STDERR
Definition: stdlib.h:111

Referenced by __acrt_report_runtime_error().

◆ write_string_to_console()

static void write_string_to_console ( wchar_t const *const  wide_string)
throw (
)
static

Definition at line 111 of file report_runtime_error.cpp.

112{
114 if (handle == nullptr || handle == INVALID_HANDLE_VALUE)
115 {
116 return;
117 }
118
119 // We convert the wide string to a narrow string by truncating each character.
120 // Currently, the text for each runtime error consists only of ASCII, so this
121 // is acceptable. If the error text is ever localized, this would need to
122 // change.
123 size_t const narrow_buffer_count = 500;
124 char narrow_buffer[narrow_buffer_count];
125
126 char* const narrow_first = narrow_buffer;
127 char* const narrow_last = narrow_first + narrow_buffer_count;
128
129 // Note that this loop copies the null terminator if the loop terminates
130 // befoe running out of buffer space:
131 char* narrow_it = narrow_first;
132 wchar_t const* wide_it = wide_string;
133 do
134 {
135 *narrow_it = static_cast<char>(*wide_it);
136 }
137 while (++narrow_it != narrow_last && *wide_it++ != '\0');
138
139 // If we did run out of buffer space, this will null-terminate the text that
140 // we were able to copy:
141 *(narrow_last - 1) = '\0';
142
143 DWORD const bytes_to_write = static_cast<DWORD>(narrow_it - narrow_first - 1); // Account for null terminator
144 DWORD bytes_written = 0;
145 WriteFile(handle, narrow_buffer, bytes_to_write, &bytes_written, nullptr);
146}
HANDLE WINAPI GetStdHandle(IN DWORD nStdHandle)
Definition: console.c:203
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
unsigned long DWORD
Definition: ntddk_ex.h:95
#define STD_ERROR_HANDLE
Definition: winbase.h:295

Referenced by __acrt_report_runtime_error().

Variable Documentation

◆ __acrt_app_type

_crt_app_type __acrt_app_type = _crt_unknown_app
static