]> The Tcpdump Group git mirrors - libpcap/blob - diag-control.h
Make sure no read routine process more than INT_MAX packets.
[libpcap] / diag-control.h
1 /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
2 /*
3 * Copyright (c) 1993, 1994, 1995, 1996, 1997
4 * The Regents of the University of California. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the Computer Systems
17 * Engineering Group at Lawrence Berkeley Laboratory.
18 * 4. Neither the name of the University nor of the Laboratory may be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #ifndef _diag_control_h
36 #define _diag_control_h
37
38 #include "pcap/compiler-tests.h"
39
40 #ifndef _MSC_VER
41 /*
42 * Clang and GCC both support this way of putting pragmas into #defines.
43 * We don't use it unless we have a compiler that supports it; the
44 * warning-suppressing pragmas differ between Clang and GCC, so we test
45 * for both of those separately.
46 */
47 #define PCAP_DO_PRAGMA(x) _Pragma (#x)
48 #endif
49
50 /*
51 * Suppress "enum value not explicitly handled in switch" warnings.
52 * We may have to build on multiple different Windows SDKs, so we
53 * may not be able to include all enum values in a switch, as they
54 * won't necessarily be defined on all the SDKs, and, unlike
55 * #defines, there's no easy way to test whether a given enum has
56 * a given value. It *could* be done by the configure script or
57 * CMake tests.
58 */
59 #if defined(_MSC_VER)
60 #define DIAG_OFF_ENUM_SWITCH \
61 __pragma(warning(push)) \
62 __pragma(warning(disable:4061))
63 #define DIAG_ON_ENUM_SWITCH \
64 __pragma(warning(pop))
65 #else
66 #define DIAG_OFF_ENUM_SWITCH
67 #define DIAG_ON_ENUM_SWITCH
68 #endif
69
70 /*
71 * Suppress "switch statement has only a default case" warnings.
72 * There's a switch in bpf_filter.c that only has additional
73 * cases on Linux.
74 */
75 #if defined(_MSC_VER)
76 #define DIAG_OFF_DEFAULT_ONLY_SWITCH \
77 __pragma(warning(push)) \
78 __pragma(warning(disable:4065))
79 #define DIAG_ON_DEFAULT_ONLY_SWITCH \
80 __pragma(warning(pop))
81 #else
82 #define DIAG_OFF_DEFAULT_ONLY_SWITCH
83 #define DIAG_ON_DEFAULT_ONLY_SWITCH
84 #endif
85
86 /*
87 * Suppress Flex, narrowing, and deprecation warnings.
88 */
89 #if defined(_MSC_VER)
90 /*
91 * This is Microsoft Visual Studio; we can use __pragma(warning(disable:XXXX))
92 * and __pragma(warning(push/pop)).
93 *
94 * Suppress signed-vs-unsigned comparison, narrowing, and unreachable
95 * code warnings.
96 */
97 #define DIAG_OFF_FLEX \
98 __pragma(warning(push)) \
99 __pragma(warning(disable:4127)) \
100 __pragma(warning(disable:4242)) \
101 __pragma(warning(disable:4244)) \
102 __pragma(warning(disable:4702))
103 #define DIAG_ON_FLEX \
104 __pragma(warning(pop))
105
106 /*
107 * Suppress narrowing warnings.
108 */
109 #define DIAG_OFF_NARROWING \
110 __pragma(warning(push)) \
111 __pragma(warning(disable:4242)) \
112 __pragma(warning(disable:4311))
113 #define DIAG_ON_NARROWING \
114 __pragma(warning(pop))
115
116 /*
117 * Suppress deprecation warnings.
118 */
119 #define DIAG_OFF_DEPRECATION \
120 __pragma(warning(push)) \
121 __pragma(warning(disable:4996))
122 #define DIAG_ON_DEPRECATION \
123 __pragma(warning(pop))
124 #define DIAG_OFF_FORMAT_TRUNCATION
125 #define DIAG_ON_FORMAT_TRUNCATION
126 #elif PCAP_IS_AT_LEAST_CLANG_VERSION(2,8)
127 /*
128 * This is Clang 2.8 or later; we can use "clang diagnostic
129 * ignored -Wxxx" and "clang diagnostic push/pop".
130 *
131 * Suppress -Wdocumentation warnings; GCC doesn't support -Wdocumentation,
132 * at least according to the GCC 7.3 documentation. Apparently, Flex
133 * generates code that upsets at least some versions of Clang's
134 * -Wdocumentation.
135 */
136 #define DIAG_OFF_FLEX \
137 PCAP_DO_PRAGMA(clang diagnostic push) \
138 PCAP_DO_PRAGMA(clang diagnostic ignored "-Wsign-compare") \
139 PCAP_DO_PRAGMA(clang diagnostic ignored "-Wdocumentation") \
140 PCAP_DO_PRAGMA(clang diagnostic ignored "-Wshorten-64-to-32") \
141 PCAP_DO_PRAGMA(clang diagnostic ignored "-Wmissing-noreturn") \
142 PCAP_DO_PRAGMA(clang diagnostic ignored "-Wunused-parameter") \
143 PCAP_DO_PRAGMA(clang diagnostic ignored "-Wunreachable-code")
144 #define DIAG_ON_FLEX \
145 PCAP_DO_PRAGMA(clang diagnostic pop)
146
147 /*
148 * Suppress the only narrowing warnings you get from Clang.
149 */
150 #define DIAG_OFF_NARROWING \
151 PCAP_DO_PRAGMA(clang diagnostic push) \
152 PCAP_DO_PRAGMA(clang diagnostic ignored "-Wshorten-64-to-32")
153
154 #define DIAG_ON_NARROWING \
155 PCAP_DO_PRAGMA(clang diagnostic pop)
156
157 /*
158 * Suppress deprecation warnings.
159 */
160 #define DIAG_OFF_DEPRECATION \
161 PCAP_DO_PRAGMA(clang diagnostic push) \
162 PCAP_DO_PRAGMA(clang diagnostic ignored "-Wdeprecated-declarations")
163 #define DIAG_ON_DEPRECATION \
164 PCAP_DO_PRAGMA(clang diagnostic pop)
165 #define DIAG_OFF_FORMAT_TRUNCATION
166 #define DIAG_ON_FORMAT_TRUNCATION
167 #elif PCAP_IS_AT_LEAST_GNUC_VERSION(4,6)
168 /*
169 * This is GCC 4.6 or later, or a compiler claiming to be that.
170 * We can use "GCC diagnostic ignored -Wxxx" (introduced in 4.2)
171 * and "GCC diagnostic push/pop" (introduced in 4.6).
172 */
173 #define DIAG_OFF_FLEX \
174 PCAP_DO_PRAGMA(GCC diagnostic push) \
175 PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wsign-compare") \
176 PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wunused-parameter") \
177 PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wunreachable-code")
178 #define DIAG_ON_FLEX \
179 PCAP_DO_PRAGMA(GCC diagnostic pop)
180
181 /*
182 * GCC currently doesn't issue any narrowing warnings.
183 */
184 #define DIAG_OFF_NARROWING
185 #define DIAG_ON_NARROWING
186
187 /*
188 * Suppress deprecation warnings.
189 */
190 #define DIAG_OFF_DEPRECATION \
191 PCAP_DO_PRAGMA(GCC diagnostic push) \
192 PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wdeprecated-declarations")
193 #define DIAG_ON_DEPRECATION \
194 PCAP_DO_PRAGMA(GCC diagnostic pop)
195
196 /*
197 * Suppress format-truncation= warnings.
198 * GCC 7.1 had introduced this warning option. Earlier versions (at least
199 * one particular copy of GCC 4.6.4) treat the request as a warning.
200 */
201 #if PCAP_IS_AT_LEAST_GNUC_VERSION(7,1)
202 #define DIAG_OFF_FORMAT_TRUNCATION \
203 PCAP_DO_PRAGMA(GCC diagnostic push) \
204 PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wformat-truncation=")
205 #define DIAG_ON_FORMAT_TRUNCATION \
206 PCAP_DO_PRAGMA(GCC diagnostic pop)
207 #else
208 #define DIAG_OFF_FORMAT_TRUNCATION
209 #define DIAG_ON_FORMAT_TRUNCATION
210 #endif
211 #else
212 /*
213 * Neither Visual Studio, nor Clang 2.8 or later, nor GCC 4.6 or later
214 * or a compiler claiming to be that; there's nothing we know of that
215 * we can do.
216 */
217 #define DIAG_OFF_FLEX
218 #define DIAG_ON_FLEX
219 #define DIAG_OFF_NARROWING
220 #define DIAG_ON_NARROWING
221 #define DIAG_OFF_DEPRECATION
222 #define DIAG_ON_DEPRECATION
223 #define DIAG_OFF_FORMAT_TRUNCATION
224 #define DIAG_ON_FORMAT_TRUNCATION
225 #endif
226
227 #ifdef YYBYACC
228 /*
229 * Berkeley YACC.
230 *
231 * It generates a global declaration of yylval, or the appropriately
232 * prefixed version of yylval, in grammar.h, *even though it's been
233 * told to generate a pure parser, meaning it doesn't have any global
234 * variables*. Bison doesn't do this.
235 *
236 * That causes a warning due to the local declaration in the parser
237 * shadowing the global declaration.
238 *
239 * So, if the compiler warns about that, we turn off -Wshadow warnings.
240 *
241 * In addition, the generated code may have functions with unreachable
242 * code, so suppress warnings about those.
243 */
244 #if defined(_MSC_VER)
245 /*
246 * This is Microsoft Visual Studio; we can use
247 * __pragma(warning(disable:XXXX)).
248 */
249 #define DIAG_OFF_BISON_BYACC \
250 __pragma(warning(disable:4702))
251 #elif PCAP_IS_AT_LEAST_CLANG_VERSION(2,8)
252 /*
253 * This is Clang 2.8 or later; we can use "clang diagnostic
254 * ignored -Wxxx".
255 */
256 #define DIAG_OFF_BISON_BYACC \
257 PCAP_DO_PRAGMA(clang diagnostic ignored "-Wshadow") \
258 PCAP_DO_PRAGMA(clang diagnostic ignored "-Wunreachable-code")
259 #elif PCAP_IS_AT_LEAST_GNUC_VERSION(4,6)
260 /*
261 * This is GCC 4.6 or later, or a compiler claiming to be that.
262 * We can use "GCC diagnostic ignored -Wxxx" (introduced in 4.2,
263 * but it may not actually work very well prior to 4.6).
264 */
265 #define DIAG_OFF_BISON_BYACC \
266 PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wshadow") \
267 PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wunreachable-code")
268 #else
269 /*
270 * Neither Clang 2.8 or later nor GCC 4.6 or later or a compiler
271 * claiming to be that; there's nothing we know of that we can do.
272 */
273 #define DIAG_OFF_BISON_BYACC
274 #endif
275 #else
276 /*
277 * Bison.
278 *
279 * The generated code may have functions with unreachable code and
280 * switches with only a default case, so suppress warnings about those.
281 */
282 #if defined(_MSC_VER)
283 /*
284 * This is Microsoft Visual Studio; we can use
285 * __pragma(warning(disable:XXXX)).
286 *
287 * Suppress some /Wall warnings.
288 */
289 #define DIAG_OFF_BISON_BYACC \
290 __pragma(warning(disable:4065)) \
291 __pragma(warning(disable:4127)) \
292 __pragma(warning(disable:4242)) \
293 __pragma(warning(disable:4244)) \
294 __pragma(warning(disable:4702))
295 #elif PCAP_IS_AT_LEAST_CLANG_VERSION(2,8)
296 /*
297 * This is Clang 2.8 or later; we can use "clang diagnostic
298 * ignored -Wxxx".
299 */
300 #define DIAG_OFF_BISON_BYACC \
301 PCAP_DO_PRAGMA(clang diagnostic ignored "-Wunreachable-code")
302 #elif PCAP_IS_AT_LEAST_GNUC_VERSION(4,6)
303 /*
304 * This is GCC 4.6 or later, or a compiler claiming to be that.
305 * We can use "GCC diagnostic ignored -Wxxx" (introduced in 4.2,
306 * but it may not actually work very well prior to 4.6).
307 */
308 #define DIAG_OFF_BISON_BYACC \
309 PCAP_DO_PRAGMA(GCC diagnostic ignored "-Wunreachable-code")
310 #else
311 /*
312 * Neither Clang 2.8 or later nor GCC 4.6 or later or a compiler
313 * claiming to be that; there's nothing we know of that we can do.
314 */
315 #define DIAG_OFF_BISON_BYACC
316 #endif
317 #endif
318
319 /*
320 * GCC needs this on AIX for longjmp().
321 */
322 #if PCAP_IS_AT_LEAST_GNUC_VERSION(5,1)
323 /*
324 * Beware that the effect of this builtin is more than just squelching the
325 * warning! GCC trusts it enough for the process to segfault if the control
326 * flow reaches the builtin (an infinite empty loop in the same context would
327 * squelch the warning and ruin the process too, albeit in a different way).
328 * So please remember to use this very carefully.
329 */
330 #define PCAP_UNREACHABLE __builtin_unreachable();
331 #else
332 #define PCAP_UNREACHABLE
333 #endif
334
335 #endif /* _diag_control_h */