ReactOS 0.4.16-dev-918-g97e7efc
assert.cpp File Reference
#include <corecrt_internal.h>
#include <corecrt_internal_stdio.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
Include dependency graph for assert.cpp:

Go to the source code of this file.

Macros

#define _ASSERT_OK
 
#define MAXLINELEN   64 /* max length for line in message box */
 
#define ASSERTBUFSZ   (MAXLINELEN * 9) /* 9 lines in message box */
 

Functions

static void __cdecl common_assert_to_stderr_direct (char const *, char const *, unsigned) throw ()
 
static void __cdecl common_assert_to_stderr_direct (wchar_t const *const expression, wchar_t const *const file_name, unsigned const line_number) throw ()
 
template<typename Character >
 __declspec (noreturn) static void __cdecl common_assert_to_stderr(Character const *const expression
 
Character const *const unsigned const line_number throw ()
 
template<typename Character >
static void __cdecl common_assert_to_message_box_build_string (Character *const assert_buffer, size_t const assert_buffer_count, Character const *const expression, Character const *const file_name, unsigned const line_number, void *const return_address) throw ()
 
template<typename Character >
static void __cdecl common_assert_to_message_box (Character const *const expression, Character const *const file_name, unsigned const line_number, void *const return_address) throw ()
 
template<typename Character >
static void __cdecl common_assert (Character const *const expression, Character const *const file_name, unsigned const line_number, void *const return_address) throw ()
 
void __cdecl _assert (char const *const expression, char const *const file_name, unsigned const line_number) throw ()
 
void __cdecl _wassert (wchar_t const *const expression, wchar_t const *const file_name, unsigned const line_number)
 

Variables

Character const *const file_name
 

Macro Definition Documentation

◆ _ASSERT_OK

#define _ASSERT_OK

Definition at line 18 of file assert.cpp.

◆ ASSERTBUFSZ

#define ASSERTBUFSZ   (MAXLINELEN * 9) /* 9 lines in message box */

Definition at line 23 of file assert.cpp.

◆ MAXLINELEN

#define MAXLINELEN   64 /* max length for line in message box */

Definition at line 22 of file assert.cpp.

Function Documentation

◆ __declspec()

template<typename Character >
__declspec ( noreturn  ) const

◆ _assert()

void __cdecl _assert ( char const *const  expression,
char const *const  file_name,
unsigned const  line_number 
)
throw (
)

Definition at line 428 of file assert.cpp.

433{
434 return common_assert(expression, file_name, line_number, _ReturnAddress());
435}
static void __cdecl common_assert(Character const *const expression, Character const *const file_name, unsigned const line_number, void *const return_address)
Definition: assert.cpp:403
Character const *const file_name
Definition: assert.cpp:166
int const char const *const int const line_number
Definition: debug_heap.cpp:499
#define _ReturnAddress()
Definition: intrin_arm.h:35

◆ _wassert()

void __cdecl _wassert ( wchar_t const *const  expression,
wchar_t const *const  file_name,
unsigned const  line_number 
)

Definition at line 437 of file assert.cpp.

442{
443 return common_assert(expression, file_name, line_number, _ReturnAddress());
444}

◆ common_assert()

template<typename Character >
static void __cdecl common_assert ( Character const *const  expression,
Character const *const  file_name,
unsigned const  line_number,
void *const  return_address 
)
throw (
)
static

Definition at line 403 of file assert.cpp.

409{
410 using traits = __crt_char_traits<Character>;
411
412 int const current_error_mode = _set_error_mode(_REPORT_ERRMODE);
413 if (current_error_mode == _OUT_TO_STDERR)
414 {
415 return common_assert_to_stderr(expression, file_name, line_number);
416 }
417
418 if (current_error_mode == _OUT_TO_DEFAULT && _query_app_type() == _crt_console_app)
419 {
420 return common_assert_to_stderr(expression, file_name, line_number);
421 }
422
423 return common_assert_to_message_box(expression, file_name, line_number, return_address);
424}
static void __cdecl common_assert_to_message_box(Character const *const expression, Character const *const file_name, unsigned const line_number, void *const return_address)
Definition: assert.cpp:350
@ _crt_console_app
_ACRTIMP _crt_app_type __cdecl _query_app_type(void)
#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 _assert(), and _wassert().

◆ common_assert_to_message_box()

template<typename Character >
static void __cdecl common_assert_to_message_box ( Character const *const  expression,
Character const *const  file_name,
unsigned const  line_number,
void *const  return_address 
)
throw (
)
static

Definition at line 350 of file assert.cpp.

356{
357 using traits = __crt_char_traits<Character>;
358
359 Character assert_buffer[ASSERTBUFSZ]{};
361 assert_buffer,
362 _countof(assert_buffer),
363 expression,
364 file_name,
366 return_address);
367
368 int const action = traits::show_message_box(
369 assert_buffer,
370 get_banner_text(Character()),
372
373 switch (action)
374 {
375 case IDABORT: // Abort the program:
376 {
377 raise(SIGABRT);
378
379 // We won't usually get here, but it's possible that a user-registered
380 // abort handler returns, so exit the program immediately. Note that
381 // even though we are "aborting," we do not call abort() because we do
382 // not want to invoke Watson (the user has already had an opportunity
383 // to debug the error and chose not to).
384 _exit(3);
385 }
386 case IDRETRY: // Break into the debugger then return control to caller
387 {
388 __debugbreak();
389 return;
390 }
391 case IDIGNORE: // Return control to caller
392 {
393 return;
394 }
395 default: // This should not happen; treat as fatal error:
396 {
397 abort();
398 }
399 }
400}
void _exit(int exitcode)
Definition: _exit.c:25
#define ASSERTBUFSZ
Definition: assert.cpp:23
static void __cdecl common_assert_to_message_box_build_string(Character *const assert_buffer, size_t const assert_buffer_count, Character const *const expression, Character const *const file_name, unsigned const line_number, void *const return_address)
Definition: assert.cpp:190
int __cdecl raise(int _SigNum)
Definition: signal.c:71
#define SIGABRT
Definition: signal.h:28
const WCHAR * action
Definition: action.c:7509
#define abort()
Definition: i386-dis.c:34
void __cdecl __debugbreak(void)
Definition: intrin_ppc.h:698
#define _countof(array)
Definition: sndvol32.h:70
#define MB_SETFOREGROUND
Definition: winuser.h:817
#define MB_ICONHAND
Definition: winuser.h:791
#define MB_TASKMODAL
Definition: winuser.h:819
#define IDIGNORE
Definition: winuser.h:837
#define MB_ABORTRETRYIGNORE
Definition: winuser.h:794
#define IDABORT
Definition: winuser.h:835
#define IDRETRY
Definition: winuser.h:836

Referenced by common_assert().

◆ common_assert_to_message_box_build_string()

template<typename Character >
static void __cdecl common_assert_to_message_box_build_string ( Character *const  assert_buffer,
size_t const  assert_buffer_count,
Character const *const  expression,
Character const *const  file_name,
unsigned const  line_number,
void *const  return_address 
)
throw (
)
static

Definition at line 190 of file assert.cpp.

198{
199 using traits = __crt_char_traits<Character>;
200
201 // Line 1: Box introduction line:
202 _ERRCHECK(traits::tcscpy_s(assert_buffer, assert_buffer_count, get_box_intro(Character())));
203 _ERRCHECK(traits::tcscat_s(assert_buffer, assert_buffer_count, get_double_newline(Character())));
204
205 // Line 2: Program line:
206 _ERRCHECK(traits::tcscat_s(assert_buffer, assert_buffer_count, get_program_intro(Character())));
207
208 Character program_name[_MAX_PATH + 1]{};
209
210 HMODULE asserting_module = nullptr;
212 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT | GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
213 static_cast<wchar_t const*>(return_address),
214 &asserting_module))
215 {
216 asserting_module = nullptr;
217 }
218
219 #ifdef CRTDLL
220 // If the assert came from within the CRT DLL, report it as having come
221 // from the EXE instead:
222 if (asserting_module == reinterpret_cast<HMODULE>(&__ImageBase))
223 {
224 asserting_module = nullptr;
225 }
226 #endif
227
228 if (!traits::get_module_file_name(asserting_module, program_name, static_cast<DWORD>(_countof(program_name))))
229 {
230 _ERRCHECK(traits::tcscpy_s(program_name, _countof(program_name), get_program_name_unknown_text(Character())));
231 }
232
233 Character* pchProg = program_name;
234 if (program_intro_count + traits::tcslen(program_name) + newline_length > MAXLINELEN)
235 {
236 pchProg += (program_intro_count + traits::tcslen(program_name) + newline_length) - MAXLINELEN;
237 // Only replace first (sizeof(Character) * dot_dot_dot_length) bytes to ellipsis:
239 pchProg,
240 sizeof(Character) * ((MAX_PATH + 1) - (pchProg - program_name)),
241 get_dot_dot_dot(Character()),
242 sizeof(Character) * dot_dot_dot_length
243 ));
244 }
245
246 _ERRCHECK(traits::tcscat_s(assert_buffer, assert_buffer_count, pchProg));
247 _ERRCHECK(traits::tcscat_s(assert_buffer, assert_buffer_count, get_newline(Character())));
248
249 // Line 3: File line
250 _ERRCHECK(traits::tcscat_s(assert_buffer, assert_buffer_count, get_file_intro(Character())));
251
252 if (file_intro_count + traits::tcslen(file_name) + newline_length > MAXLINELEN)
253 {
254 size_t const ffn = MAXLINELEN - file_intro_count - newline_length;
255
256 size_t p = 0;
257 size_t len = 0;
258 Character const* pch = file_name;
259 for (len = traits::tcslen(file_name), p = 1;
260 pch[len - p] != '\\' && pch[len - p] != '/' && p < len;
261 p++)
262 {
263 }
264
265 // Trim the path and file name so that they fit, using up to 2/3 of
266 // the maximum number of characters for the path and the remaining
267 // 1/3 for the file name:
268 if ((ffn - ffn / 3) < (len - p) && ffn / 3 > p)
269 {
270 // The path is too long. Use the first part of the path and the
271 // full file name:
272 _ERRCHECK(traits::tcsncat_s(assert_buffer, assert_buffer_count, pch, ffn - dot_dot_dot_length - p));
273 _ERRCHECK(traits::tcscat_s (assert_buffer, assert_buffer_count, get_dot_dot_dot(Character())));
274 _ERRCHECK(traits::tcscat_s (assert_buffer, assert_buffer_count, pch + len - p));
275 }
276 else if (ffn - ffn / 3 > len - p)
277 {
278 // The file name is too long. Use the full path and the first
279 // and last part of the file name, with a ... in between:
280 p = p / 2;
281 _ERRCHECK(traits::tcsncat_s(assert_buffer, assert_buffer_count, pch, ffn - dot_dot_dot_length - p));
282 _ERRCHECK(traits::tcscat_s (assert_buffer, assert_buffer_count, get_dot_dot_dot(Character())));
283 _ERRCHECK(traits::tcscat_s (assert_buffer, assert_buffer_count, pch + len - p));
284 }
285 else
286 {
287 // Both are too long. Use the first part of the path and the
288 // first and last part of the file name, with ...s in between:
289 _ERRCHECK(traits::tcsncat_s(assert_buffer, assert_buffer_count, pch, ffn - ffn / 3 - dot_dot_dot_length));
290 _ERRCHECK(traits::tcscat_s (assert_buffer, assert_buffer_count, get_dot_dot_dot(Character())));
291 _ERRCHECK(traits::tcsncat_s(assert_buffer, assert_buffer_count, pch + len - p, ffn / 6 - 1));
292 _ERRCHECK(traits::tcscat_s (assert_buffer, assert_buffer_count, get_dot_dot_dot(Character())));
293 _ERRCHECK(traits::tcscat_s (assert_buffer, assert_buffer_count, pch + len - (ffn / 3 - ffn / 6 - 2)));
294 }
295 }
296 else
297 {
298 // Plenty of room on the line; just append the full path and file name:
299 _ERRCHECK(traits::tcscat_s(assert_buffer, assert_buffer_count, file_name));
300 }
301
302 _ERRCHECK(traits::tcscat_s(assert_buffer, assert_buffer_count, get_newline(Character())));
303
304 // Line 4: Line Number line:
305 _ERRCHECK(traits::tcscat_s(assert_buffer, assert_buffer_count, get_line_intro(Character())));
306 _ERRCHECK(traits::itot_s(
308 assert_buffer + traits::tcslen(assert_buffer),
309 assert_buffer_count - traits::tcslen(assert_buffer),
310 10));
311 _ERRCHECK(traits::tcscat_s(assert_buffer, assert_buffer_count, get_double_newline(Character())));
312
313 // Line 5: Message line:
314 _ERRCHECK(traits::tcscat_s(assert_buffer, assert_buffer_count, get_expression_intro(Character())));
315
316 size_t const characters_used =
317 traits::tcslen(assert_buffer) +
318 2 * double_newline_length +
319 info_intro_length +
320 help_intro_count;
321
322 if (characters_used + traits::tcslen(expression) > assert_buffer_count)
323 {
324 size_t const characters_to_write = assert_buffer_count - (characters_used + dot_dot_dot_length);
325 _ERRCHECK(traits::tcsncat_s(
326 assert_buffer,
327 assert_buffer_count,
328 expression,
329 characters_to_write));
330 _ERRCHECK(traits::tcscat_s(assert_buffer, assert_buffer_count, get_dot_dot_dot(Character())));
331 }
332 else
333 {
334 _ERRCHECK(traits::tcscat_s(assert_buffer, assert_buffer_count, expression));
335 }
336
337 _ERRCHECK(traits::tcscat_s(assert_buffer, assert_buffer_count, get_double_newline(Character())));
338
339 // Info line:
340 _ERRCHECK(traits::tcscat_s(assert_buffer, assert_buffer_count, get_info_intro(Character())));
341 _ERRCHECK(traits::tcscat_s(assert_buffer, assert_buffer_count, get_double_newline(Character())));
342
343 // Help line:
344 _ERRCHECK(traits::tcscat_s(assert_buffer, assert_buffer_count, get_help_intro(Character())));
345}
#define MAXLINELEN
Definition: assert.cpp:22
#define _ERRCHECK(e)
#define __ImageBase
Definition: crt_handler.c:22
#define MAX_PATH
Definition: compat.h:34
BOOL WINAPI GetModuleHandleExW(IN DWORD dwFlags, IN LPCWSTR lpwModuleName OPTIONAL, OUT HMODULE *phModule)
Definition: loader.c:866
unsigned long DWORD
Definition: ntddk_ex.h:95
GLfloat GLfloat p
Definition: glext.h:8902
GLenum GLsizei len
Definition: glext.h:6722
static char * program_name
Definition: mkdosfs.c:519
#define pch(ap)
Definition: match.c:418
#define _MAX_PATH
Definition: utility.h:77
int CDECL memcpy_s(void *dest, size_t numberOfElements, const void *src, size_t count)
Definition: heap.c:800
#define const
Definition: zconf.h:233

Referenced by common_assert_to_message_box().

◆ common_assert_to_stderr_direct() [1/2]

static void __cdecl common_assert_to_stderr_direct ( char const ,
char const ,
unsigned   
)
throw (
)
static

Definition at line 123 of file assert.cpp.

124{
125 // No action for narrow strings
126}

Referenced by throw().

◆ common_assert_to_stderr_direct() [2/2]

static void __cdecl common_assert_to_stderr_direct ( wchar_t const *const  expression,
wchar_t const *const  file_name,
unsigned const  line_number 
)
throw (
)
static

Definition at line 128 of file assert.cpp.

133{
134 HANDLE const stderr_handle = GetStdHandle(STD_ERROR_HANDLE);
135#pragma warning(suppress:__WARNING_REDUNDANT_POINTER_TEST) // 28922
136 if (stderr_handle == INVALID_HANDLE_VALUE || stderr_handle == nullptr)
137 {
138 return;
139 }
140
141 if (GetFileType(stderr_handle) != FILE_TYPE_CHAR)
142 {
143 return;
144 }
145
146 wchar_t assert_buffer[ASSERTBUFSZ];
147#pragma warning(suppress:__WARNING_BANNED_API_USAGE) // 28719
148 if (swprintf(assert_buffer, _countof(assert_buffer), get_assert_format(wchar_t()), expression, file_name, line_number) < 0)
149 {
150 return;
151 }
152
153 DWORD const assert_buffer_length = static_cast<DWORD>(wcslen(assert_buffer));
154 DWORD characters_written = 0;
155 if (WriteConsoleW(stderr_handle, assert_buffer, assert_buffer_length, &characters_written, nullptr) == 0)
156 {
157 return;
158 }
159
160 abort();
161}
HANDLE WINAPI GetStdHandle(IN DWORD nStdHandle)
Definition: console.c:203
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleW(IN HANDLE hConsoleOutput, IN CONST VOID *lpBuffer, IN DWORD nNumberOfCharsToWrite, OUT LPDWORD lpNumberOfCharsWritten, LPVOID lpReserved)
Definition: readwrite.c:1447
DWORD WINAPI GetFileType(HANDLE hFile)
Definition: fileinfo.c:269
#define swprintf
Definition: precomp.h:40
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define STD_ERROR_HANDLE
Definition: winbase.h:295
#define FILE_TYPE_CHAR
Definition: winbase.h:286

◆ throw()

Character const *const unsigned const line_number throw ( )

Definition at line 168 of file assert.cpp.

169{
170 using traits = __crt_char_traits<Character>;
171
172 // Try to write directly to the console. This is only supported for wide
173 // character strings. If we have a narrow character string or the write
174 // fails, we fall back to call through stdio.
176
177 // If stderr does not yet have a buffer, set it to use single character
178 // buffering to avoid dynamic allocation of a stream buffer:
179 if (!__crt_stdio_stream(stderr).has_any_buffer())
180 {
181 setvbuf(stderr, nullptr, _IONBF, 0);
182 }
183
184 traits::ftprintf(stderr, get_assert_format(Character()), expression, file_name, line_number);
185 fflush(stderr);
186 abort();
187}
static void __cdecl common_assert_to_stderr_direct(char const *, char const *, unsigned)
Definition: assert.cpp:123
#define stderr
Definition: stdio.h:100
_Check_return_opt_ _CRTIMP int __cdecl fflush(_Inout_opt_ FILE *_File)
#define _IONBF
Definition: stdio.h:129
_Check_return_opt_ _CRTIMP int __cdecl setvbuf(_Inout_ FILE *_File, _Inout_updates_opt_z_(_Size) char *_Buf, _In_ int _Mode, _In_ size_t _Size)

Variable Documentation

◆ file_name