1 #ifndef SNMP_ASSERT_H
2 #define SNMP_ASSERT_H
3 
4 #ifdef NETSNMP_USE_ASSERT
5 #   include <assert.h>
6 #else
7 #   include <net-snmp/library/snmp_logging.h>
8 #endif
9 
10 
11 /*
12  * MACROs don't need extern "C"
13  */
14 
15 /*
16  * define __STRING for systems (*cough* sun *cough*) that don't have it
17  */
18 #ifndef __STRING
19 #  if defined(__STDC__) || defined(_MSC_VER)
20 #    define __STRING(x) #x
21 #  else
22 #    define __STRING(x) "x"
23 #  endif /* __STDC__ */
24 #endif /* __STRING */
25 
26 /*
27  * always use assert if requested
28  */
29 #ifdef NETSNMP_USE_ASSERT
30 /*   void netsnmp_assert( int );*/
31 #   define netsnmp_assert(x)  assert( x )
32 #   define netsnmp_assert_or_return(x, y)  assert( x )
33 #   define netsnmp_assert_or_msgreturn(x, y, z)  assert( x )
34 #else
35 /*
36  *  if asserts weren't requested, just log, unless NETSNMP_NO_DEBUGGING specified
37  */
38 #   ifndef NETSNMP_NO_DEBUGGING
39 #      ifdef  NETSNMP_FUNCTION
40 #          define NETSNMP_FUNC_FMT " %s()\n"
41 #          define NETSNMP_FUNC_PARAM NETSNMP_FUNCTION
42 #      else
43 #          define NETSNMP_FUNC_FMT "%c"
44 #          define NETSNMP_FUNC_PARAM '\n'
45 #      endif
46 #
47 #      define netsnmp_assert(x)  do { \
48               if ( x ) \
49                  ; \
50               else \
51                  snmp_log(LOG_ERR, \
52                           "netsnmp_assert %s failed %s:%d" NETSNMP_FUNC_FMT, \
53                           __STRING(x),__FILE__,__LINE__, \
54                           NETSNMP_FUNC_PARAM); \
55            }while(0)
56 #      define netsnmp_assert_or_return(x, y)  do {        \
57               if ( x ) \
58                  ; \
59               else { \
60                  snmp_log(LOG_ERR, \
61                           "netsnmp_assert %s failed %s:%d" NETSNMP_FUNC_FMT, \
62                           __STRING(x),__FILE__,__LINE__, \
63                           NETSNMP_FUNC_PARAM); \
64                  return y; \
65               } \
66            }while(0)
67 #      define netsnmp_assert_or_msgreturn(x, y, z)  do {       \
68               if ( x ) \
69                  ; \
70               else { \
71                  snmp_log(LOG_ERR, \
72                           "netsnmp_assert %s failed %s:%d" NETSNMP_FUNC_FMT, \
73                           __STRING(x),__FILE__,__LINE__, \
74                           NETSNMP_FUNC_PARAM); \
75                  snmp_log(LOG_ERR, y); \
76                  return z; \
77               } \
78            }while(0)
79 #   else /* NO DEBUGGING */
80 #      define netsnmp_assert(x)
81 #      define netsnmp_assert_or_return(x, y)  do {        \
82                  if ( x ) \
83                     ; \
84                  else { \
85                     return y; \
86                  } \
87               }while(0)
88 #      define netsnmp_assert_or_msgreturn(x, y, z)  do {       \
89                  if ( x ) \
90                     ; \
91                  else { \
92                     return z; \
93                  } \
94               }while(0)
95 #   endif /* NO DEBUGGING */
96 #endif /* not NETSNMP_USE_ASSERT */
97 
98 
99 #define netsnmp_static_assert(x) \
100     do { switch(0) { case (x): case 0: ; } } while(0)
101 
102 
103 /*
104  *  EXPERIMENTAL macros. May be removed without warning in future
105  * releases. Use at your own risk
106  *
107  * The series of uppercase letters at or near the end of these macros give
108  * an indication of what they do. The letters used are:
109  *
110  *   L  : log a message
111  *   RN : return NULL
112  *   RE : return a specific hardcoded error appropriate for the condition
113  *   RV : return user specified value
114  *
115  */
116 #define netsnmp_malloc_check_LRN(ptr)           \
117     netsnmp_assert_or_return( (ptr) != NULL, NULL)
118 #define netsnmp_malloc_check_LRE(ptr)           \
119     netsnmp_assert_or_return( (ptr) != NULL, SNMPERR_MALLOC)
120 #define netsnmp_malloc_check_LRV(ptr, val)                          \
121     netsnmp_assert_or_return( (ptr) != NULL, val)
122 
123 #define netsnmp_require_ptr_LRV( ptr, val ) \
124     netsnmp_assert_or_return( (ptr) != NULL, val)
125 
126 
127 #endif /* SNMP_ASSERT_H */
128