2 * Wireshark's exceptions.
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * SPDX-License-Identifier: GPL-2.0-or-later
11 #ifndef __EXCEPTIONS_H__
12 #define __EXCEPTIONS_H__
16 /* Wireshark has only one exception group, to make these macros simple */
17 #define XCEPT_GROUP_WIRESHARK 1
20 Index is out of range.
21 An attempt was made to read past the end of a buffer.
22 This generally means that the capture was done with a "slice"
23 length or "snapshot" length less than the maximum packet size,
24 and a link-layer packet was cut short by that, so not all of the
25 data in the link-layer packet was available.
30 Index is beyond reported length (not cap_len)
31 An attempt was made to read past the logical end of a buffer. This
32 differs from a BoundsError in that the parent protocol established a
33 limit past which this dissector should not process in the buffer and that
35 This generally means that the packet is invalid, i.e. whatever
36 code constructed the packet and put it on the wire didn't put enough
37 data into it. It is therefore currently reported as a "Malformed
40 #define ReportedBoundsError 2
43 Index is beyond fragment length but not reported length.
44 This means that the packet wasn't reassembled.
46 #define FragmentBoundsError 3
49 During dfilter parsing
54 A bug was detected in a dissector.
56 DO NOT throw this with THROW(); that means that no details about
57 the dissector error will be reported. (Instead, the message will
58 blame you for not providing details.)
60 Instead, use the DISSECTOR_ASSERT(), etc. macros in epan/proto.h.
62 #define DissectorError 5
65 Index is out of range.
66 An attempt was made to read past the end of a buffer.
67 This error is specific to SCSI data transfers where for some CDBs
68 it is normal that the data PDU might be short.
69 I.e. ReportLuns initially called with allocation_length=8, just enough
70 to get the "size" of lun list back after which the initiator will
71 reissue the command with an allocation_length that is big enough.
73 #define ScsiBoundsError 6
76 Running out of memory.
77 A dissector tried to allocate memory but that failed.
79 #define OutOfMemoryError 7
82 The reassembly state machine was passed a bad fragment offset,
83 or other similar issues. We used to use DissectorError in these
84 cases, but they're not necessarily the dissector's fault - if the packet
85 contains a bad fragment offset, the dissector shouldn't have to figure
86 that out by itself since that's what the reassembly machine is for.
88 #define ReassemblyError 8
91 * Catch errors that, if you're calling a subdissector and catching
92 * exceptions from the subdissector, and possibly dissecting more
93 * stuff after the subdissector returns or fails, mean it makes
94 * sense to continue dissecting:
96 * BoundsError indicates a configuration problem (the capture was
97 * set up to throw away data, and it did); there's no point in
98 * trying to dissect any more data, as there's no more data to dissect.
100 * FragmentBoundsError indicates a configuration problem (reassembly
101 * wasn't enabled or couldn't be done); there's no point in trying
102 * to dissect any more data, as there's no more data to dissect.
104 * OutOfMemoryError indicates what its name suggests; there's no point
105 * in trying to dissect any more data, as you're probably not going to
106 * have any more memory to use when dissecting them.
108 * Other errors indicate that there's some sort of problem with
109 * the packet; you should continue dissecting data, as it might
110 * be OK, and, even if it's not, you should report its problem
113 #define CATCH_NONFATAL_ERRORS \
114 CATCH3(ReportedBoundsError, ScsiBoundsError, ReassemblyError)
117 * Catch all bounds-checking errors.
119 #define CATCH_BOUNDS_ERRORS \
120 CATCH4(BoundsError, FragmentBoundsError, ReportedBoundsError, \
124 * Catch all bounds-checking errors, and catch dissector bugs.
125 * Should only be used at the top level, so that dissector bugs
126 * go all the way to the top level and get reported immediately.
128 #define CATCH_BOUNDS_AND_DISSECTOR_ERRORS \
129 CATCH6(BoundsError, FragmentBoundsError, ReportedBoundsError, \
130 ScsiBoundsError, DissectorError, ReassemblyError)
142 * CATCH2(exception1, exception2) {
146 * CATCH3(exception1, exception2, exception3) {
150 * CATCH4(exception1, exception2, exception3, exception4) {
154 * CATCH5(exception1, exception2, exception3, exception4, exception5) {
158 * CATCH6(exception1, exception2, exception3, exception4, exception5, exception6) {
162 * CATCH_NONFATAL_ERRORS {
166 * CATCH_BOUNDS_ERRORS {
170 * CATCH_BOUNDS_AND_DISSECTOR_ERRORS {
184 * ********* Never use 'goto' or 'return' inside the TRY, CATCH*, or
185 * ********* FINALLY blocks. Execution must proceed through ENDTRY before
186 * ********* branching out.
188 * This is really something like:
196 * if (!caught && x == 1) {
200 * if (!caught && x == 2) {
204 * if (!caught && (x == 3 || x == 4)) {
208 * if (!caught && (x == 5 || x == 6 || x == 7)) {
210 * <CATCH3(5,6,7) code>
212 * if (!caught && x != 0) {
222 * All CATCH's must precede a CATCH_ALL.
223 * FINALLY must occur after any CATCH or CATCH_ALL.
224 * ENDTRY marks the end of the TRY code.
225 * TRY and ENDTRY are the mandatory parts of a TRY block.
226 * CATCH, CATCH_ALL, and FINALLY are all optional (although
227 * you'll probably use at least one, otherwise why "TRY"?)
229 * GET_MESSAGE returns string ptr to exception message
230 * when exception is thrown via THROW_MESSAGE()
232 * To throw/raise an exception.
235 * RETHROW rethrow the caught exception
237 * A cleanup callback is a function called in case an exception occurs
238 * and is not caught. It should be used to free any dynamically-allocated data.
239 * A pop or call_and_pop should occur at the same statement-nesting level
242 * CLEANUP_CB_PUSH(func, data)
244 * CLEANUP_CB_CALL_AND_POP
247 /* we do up to three passes through the bit of code after except_try_push(),
248 * and except_state is used to keep track of where we are.
250 #define EXCEPT_CAUGHT 1 /* exception has been caught, no need to rethrow at
253 #define EXCEPT_RETHROWN 2 /* the exception was rethrown from a CATCH
254 * block. Don't reenter the CATCH blocks, but do
255 * execute FINALLY and rethrow at ENDTRY */
257 #define EXCEPT_FINALLY 4 /* we've entered the FINALLY block - don't allow
258 * RETHROW, and don't reenter FINALLY if a
259 * different exception is thrown */
263 except_t *volatile exc; \
264 volatile int except_state = 0; \
265 static const except_id_t catch_spec[] = { \
266 { XCEPT_GROUP_WIRESHARK, XCEPT_CODE_ANY } }; \
267 except_try_push(catch_spec, 1, &exc); \
269 if(except_state & EXCEPT_CAUGHT) \
270 except_state |= EXCEPT_RETHROWN; \
271 except_state &= ~EXCEPT_CAUGHT; \
273 if (except_state == 0 && exc == 0) \
274 /* user's code goes here */
277 /* rethrow the exception if necessary */ \
278 if(!(except_state&EXCEPT_CAUGHT) && exc != 0) \
279 except_rethrow(exc); \
283 /* the (except_state |= EXCEPT_CAUGHT) in the below is a way of setting
284 * except_state before the user's code, without disrupting the user's code if
288 if (except_state == 0 && exc != 0 && \
289 exc->except_id.except_code == (x) && \
290 (except_state |= EXCEPT_CAUGHT)) \
291 /* user's code goes here */
293 #define CATCH2(x,y) \
294 if (except_state == 0 && exc != 0 && \
295 (exc->except_id.except_code == (x) || \
296 exc->except_id.except_code == (y)) && \
297 (except_state|=EXCEPT_CAUGHT)) \
298 /* user's code goes here */
300 #define CATCH3(x,y,z) \
301 if (except_state == 0 && exc != 0 && \
302 (exc->except_id.except_code == (x) || \
303 exc->except_id.except_code == (y) || \
304 exc->except_id.except_code == (z)) && \
305 (except_state|=EXCEPT_CAUGHT)) \
306 /* user's code goes here */
308 #define CATCH4(w,x,y,z) \
309 if (except_state == 0 && exc != 0 && \
310 (exc->except_id.except_code == (w) || \
311 exc->except_id.except_code == (x) || \
312 exc->except_id.except_code == (y) || \
313 exc->except_id.except_code == (z)) && \
314 (except_state|=EXCEPT_CAUGHT)) \
315 /* user's code goes here */
317 #define CATCH5(v,w,x,y,z) \
318 if (except_state == 0 && exc != 0 && \
319 (exc->except_id.except_code == (v) || \
320 exc->except_id.except_code == (w) || \
321 exc->except_id.except_code == (x) || \
322 exc->except_id.except_code == (y) || \
323 exc->except_id.except_code == (z)) && \
324 (except_state|=EXCEPT_CAUGHT)) \
325 /* user's code goes here */
327 #define CATCH6(u,v,w,x,y,z) \
328 if (except_state == 0 && exc != 0 && \
329 (exc->except_id.except_code == (u) || \
330 exc->except_id.except_code == (v) || \
331 exc->except_id.except_code == (w) || \
332 exc->except_id.except_code == (x) || \
333 exc->except_id.except_code == (y) || \
334 exc->except_id.except_code == (z)) && \
335 (except_state|=EXCEPT_CAUGHT)) \
336 /* user's code goes here */
339 if (except_state == 0 && exc != 0 && \
340 (except_state|=EXCEPT_CAUGHT)) \
341 /* user's code goes here */
344 if( !(except_state & EXCEPT_FINALLY) && (except_state|=EXCEPT_FINALLY)) \
345 /* user's code goes here */
348 except_throw(XCEPT_GROUP_WIRESHARK, (x), NULL)
350 #define THROW_ON(cond, x) G_STMT_START { \
352 except_throw(XCEPT_GROUP_WIRESHARK, (x), NULL); \
355 #define THROW_MESSAGE(x, y) \
356 except_throw(XCEPT_GROUP_WIRESHARK, (x), (y))
358 #define THROW_MESSAGE_ON(cond, x, y) G_STMT_START { \
360 except_throw(XCEPT_GROUP_WIRESHARK, (x), (y)); \
363 /* Throws a formatted message, its memory is cleared after catching it. */
364 #define THROW_FORMATTED(x, ...) \
365 except_throwf(XCEPT_GROUP_WIRESHARK, (x), __VA_ARGS__)
367 /* Like THROW_FORMATTED, but takes a va_list as an argument */
368 #define VTHROW_FORMATTED(x, format, args) \
369 except_vthrowf(XCEPT_GROUP_WIRESHARK, (x), format, args)
371 #define GET_MESSAGE except_message(exc)
375 /* check we're in a catch block */ \
376 g_assert(except_state == EXCEPT_CAUGHT); \
377 /* we can't use except_rethrow here, as that pops a catch block \
378 * off the stack, and we don't want to do that, because we want to \
379 * excecute the FINALLY {} block first. \
380 * except_throw doesn't provide an interface to rethrow an existing \
381 * exception; however, longjmping back to except_try_push() has the \
384 * Note also that THROW and RETHROW should provide much the same \
385 * functionality in terms of which blocks to enter, so any messing \
386 * about with except_state in here would indicate that THROW is \
387 * doing the wrong thing. \
389 longjmp(except_ch.except_jmp,1); \
392 #define EXCEPT_CODE except_code(exc)
394 /* Register cleanup functions in case an exception is thrown and not caught.
395 * From the Kazlib documentation, with modifications for use with the
396 * Wireshark-specific macros:
398 * CLEANUP_PUSH(func, arg)
400 * The call to CLEANUP_PUSH shall be matched with a call to
401 * CLEANUP_CALL_AND_POP or CLEANUP_POP which must occur in the same
402 * statement block at the same level of nesting. This requirement allows
403 * an implementation to provide a CLEANUP_PUSH macro which opens up a
404 * statement block and a CLEANUP_POP which closes the statement block.
405 * The space for the registered pointers can then be efficiently
406 * allocated from automatic storage.
408 * The CLEANUP_PUSH macro registers a cleanup handler that will be
409 * called if an exception subsequently occurs before the matching
410 * CLEANUP_[CALL_AND_]POP is executed, and is not intercepted and
411 * handled by a try-catch region that is nested between the two.
413 * The first argument to CLEANUP_PUSH is a pointer to the cleanup
414 * handler, a function that returns nothing and takes a single
415 * argument of type void*. The second argument is a void* value that
416 * is registered along with the handler. This value is what is passed
417 * to the registered handler, should it be called.
419 * Cleanup handlers are called in the reverse order of their nesting:
420 * inner handlers are called before outer handlers.
422 * The program shall not leave the cleanup region between
423 * the call to the macro CLEANUP_PUSH and the matching call to
424 * CLEANUP_[CALL_AND_]POP by means other than throwing an exception,
425 * or calling CLEANUP_[CALL_AND_]POP.
427 * Within the call to the cleanup handler, it is possible that new
428 * exceptions may happen. Such exceptions must be handled before the
429 * cleanup handler terminates. If the call to the cleanup handler is
430 * terminated by an exception, the behavior is undefined. The exception
431 * which triggered the cleanup is not yet caught; thus the program
432 * would be effectively trying to replace an exception with one that
433 * isn't in a well-defined state.
436 * CLEANUP_POP and CLEANUP_CALL_AND_POP
438 * A call to the CLEANUP_POP or CLEANUP_CALL_AND_POP macro shall match
439 * each call to CLEANUP_PUSH which shall be in the same statement block
440 * at the same nesting level. It shall match the most recent such a
441 * call that is not matched by a previous CLEANUP_[CALL_AND_]POP at
444 * These macros causes the registered cleanup handler to be removed. If
445 * CLEANUP_CALL_AND_POP is called, the cleanup handler is called.
446 * In that case, the registered context pointer is passed to the cleanup
447 * handler. If CLEANUP_POP is called, the cleanup handler is not called.
449 * The program shall not leave the region between the call to the
450 * macro CLEANUP_PUSH and the matching call to CLEANUP_[CALL_AND_]POP
451 * other than by throwing an exception, or by executing the
452 * CLEANUP_CALL_AND_POP.
457 #define CLEANUP_PUSH(f,a) except_cleanup_push((f),(a))
458 #define CLEANUP_POP except_cleanup_pop(0)
459 #define CLEANUP_CALL_AND_POP except_cleanup_pop(1)
461 /* Variants to allow nesting of except_cleanup_push w/o "shadowing" variables */
462 #define CLEANUP_PUSH_PFX(pfx,f,a) except_cleanup_push_pfx(pfx,(f),(a))
463 #define CLEANUP_POP_PFX(pfx) except_cleanup_pop_pfx(pfx,0)
464 #define CLEANUP_CALL_AND_POP_PFX(pfx) except_cleanup_pop_pfx(pfx,1)
468 #endif /* __EXCEPTIONS_H__ */