summaryrefslogtreecommitdiff
path: root/trap.h
blob: 63985840c463fd66817e02270550aa825db41231 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// trap.h - written and placed in public domain by Jeffrey Walton.
//          Copyright assigned to Crypto++ project

#ifndef CRYPTOPP_TRAP_H
#define CRYPTOPP_TRAP_H

#include "config.h"

// CRYPTOPP_POSIX_ASSERT unconditionally disables the library assert and yields
//   to Posix assert. CRYPTOPP_POSIX_ASSERT can be set in config.h. if you want
//   to disable asserts, then define NDEBUG or _NDEBUG when building the library.

// Needed for NDEBUG and CRYPTOPP_POSIX_ASSERT
#include <cassert>

#if defined(CRYPTOPP_DEBUG)
#  include <iostream>
#  include <sstream>
#  if defined(CRYPTOPP_WIN32_AVAILABLE)
#    pragma push_macro("WIN32_LEAN_AND_MEAN")
#    pragma push_macro("_WIN32_WINNT")
#    pragma push_macro("NOMINMAX")
#    define WIN32_LEAN_AND_MEAN
#    define _WIN32_WINNT 0x0400
#    define NOMINMAX
#    include <Windows.h>
#  elif defined(CRYPTOPP_BSD_AVAILABLE) || defined(CRYPTOPP_UNIX_AVAILABLE)
#    include <signal.h>
#  endif
#endif // CRYPTOPP_DEBUG

// ************** run-time assertion ***************

// See test.cpp and DebugTrapHandler for code to install a null signal handler
// for SIGTRAP on BSD, Linux and Unix. The handler installs itself during
// initialization of the test program.

#if defined(CRYPTOPP_DEBUG) && (defined(CRYPTOPP_BSD_AVAILABLE) || defined(CRYPTOPP_UNIX_AVAILABLE))
#  define CRYPTOPP_ASSERT(exp) {                                  \
    if (!(exp)) {                                                 \
      std::ostringstream oss;                                     \
      oss << "Assertion failed: " << (char*)(__FILE__) << "("     \
          << (int)(__LINE__) << "): " << (char*)(__func__)        \
          << std::endl;                                           \
      std::cerr << oss.str();                                     \
      raise(SIGTRAP);                                             \
    }                                                             \
  }
#elif defined(CRYPTOPP_DEBUG) && defined(CRYPTOPP_WIN32_AVAILABLE)
#  define CRYPTOPP_ASSERT(exp) {                                  \
    if (!(exp)) {                                                 \
      std::ostringstream oss;                                     \
      oss << "Assertion failed: " << (char*)(__FILE__) << "("     \
          << (int)(__LINE__) << "): " << (char*)(__FUNCTION__)    \
          << std::endl;                                           \
      DebugBreak();                                               \
      std::cerr << oss.str();                                     \
    }                                                             \
  }
// Fallback to original behavior for NDEBUG (and non-Windows/non-Unix builds)
#else
#  define CRYPTOPP_ASSERT(exp) assert(exp)
#endif // DEBUG and Unix or Windows

#if defined(CRYPTOPP_DEBUG) && defined(CRYPTOPP_WIN32_AVAILABLE)
#  pragma pop_macro("WIN32_LEAN_AND_MEAN")
#  pragma pop_macro("_WIN32_WINNT")
#  pragma pop_macro("NOMINMAX")
#endif

#endif // CRYPTOPP_TRAP_H