2 * Wireshark's exceptions.
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #ifndef __EXCEPTIONS_H__
24 #define __EXCEPTIONS_H__
28 /* Wireshark has only one exception group, to make these macros simple */
29 #define XCEPT_GROUP_WIRESHARK 1
32 Index is out of range.
33 An attempt was made to read past the end of a buffer.
34 This generally means that the capture was done with a "slice"
35 length or "snapshot" length less than the maximum packet size,
36 and a link-layer packet was cut short by that, so not all of the
37 data in the link-layer packet was available.
42 Index is beyond reported length (not cap_len)
43 An attempt was made to read past the logical end of a buffer. This
44 differs from a BoundsError in that the parent protocol established a
45 limit past which this dissector should not process in the buffer and that
47 This generally means that the packet is invalid, i.e. whatever
48 code constructed the packet and put it on the wire didn't put enough
49 data into it. It is therefore currently reported as a "Malformed
52 #define ReportedBoundsError 2
55 Index is beyond fragment length but not reported length.
56 This means that the packet wasn't reassembled.
58 #define FragmentBoundsError 3
61 During dfilter parsing
66 A bug was detected in a dissector.
68 DO NOT throw this with THROW(); that means that no details about
69 the dissector error will be reported. (Instead, the message will
70 blame you for not providing details.)
72 Instead, use the DISSECTOR_ASSERT(), etc. macros in epan/proto.h.
74 #define DissectorError 5
77 Index is out of range.
78 An attempt was made to read past the end of a buffer.
79 This error is specific to SCSI data transfers where for some CDBs
80 it is normal that the data PDU might be short.
81 I.e. ReportLuns initially called with allocation_length=8, just enough
82 to get the "size" of lun list back after which the initiator will
83 reissue the command with an allocation_length that is big enough.
85 #define ScsiBoundsError 6
88 Running out of memory.
89 A dissector tried to allocate memory but that failed.
91 #define OutOfMemoryError 7
94 The reassembly state machine was passed a bad fragment offset,
95 or other similar issues. We used to use DissectorError in these
96 cases, but they're not necessarily the dissector's fault - if the packet
97 contains a bad fragment offset, the dissector shouldn't have to figure
98 that out by itself since that's what the reassembly machine is for.
100 #define ReassemblyError 8
103 * Catch errors that, if you're calling a subdissector and catching
104 * exceptions from the subdissector, and possibly dissecting more
105 * stuff after the subdissector returns or fails, mean it makes
106 * sense to continue dissecting:
108 * BoundsError indicates a configuration problem (the capture was
109 * set up to throw away data, and it did); there's no point in
110 * trying to dissect any more data, as there's no more data to dissect.
112 * FragmentBoundsError indicates a configuration problem (reassembly
113 * wasn't enabled or couldn't be done); there's no point in trying
114 * to dissect any more data, as there's no more data to dissect.
116 * OutOfMemoryError indicates what its name suggests; there's no point
117 * in trying to dissect any more data, as you're probably not going to
118 * have any more memory to use when dissecting them.
120 * Other errors indicate that there's some sort of problem with
121 * the packet; you should continue dissecting data, as it might
122 * be OK, and, even if it's not, you should report its problem
125 #define CATCH_NONFATAL_ERRORS \
126 CATCH3(ReportedBoundsError, ScsiBoundsError, ReassemblyError)
129 * Catch all bounds-checking errors.
131 #define CATCH_BOUNDS_ERRORS \
132 CATCH4(BoundsError, FragmentBoundsError, ReportedBoundsError, \
136 * Catch all bounds-checking errors, and catch dissector bugs.
137 * Should only be used at the top level, so that dissector bugs
138 * go all the way to the top level and get reported immediately.
140 #define CATCH_BOUNDS_AND_DISSECTOR_ERRORS \
141 CATCH6(BoundsError, FragmentBoundsError, ReportedBoundsError, \
142 ScsiBoundsError, DissectorError, ReassemblyError)
154 * CATCH2(exception1, exception2) {
158 * CATCH3(exception1, exception2, exception3) {
162 * CATCH4(exception1, exception2, exception3, exception4) {
166 * CATCH5(exception1, exception2, exception3, exception4, exception5) {
170 * CATCH6(exception1, exception2, exception3, exception4, exception5, exception6) {
174 * CATCH_NONFATAL_ERRORS {
178 * CATCH_BOUNDS_ERRORS {
182 * CATCH_BOUNDS_AND_DISSECTOR_ERRORS {
196 * ********* Never use 'goto' or 'return' inside the TRY, CATCH*, or
197 * ********* FINALLY blocks. Execution must proceed through ENDTRY before
198 * ********* branching out.
200 * This is really something like:
208 * if (!caught && x == 1) {
212 * if (!caught && x == 2) {
216 * if (!caught && (x == 3 || x == 4)) {
220 * if (!caught && (x == 5 || x == 6 || x == 7)) {
222 * <CATCH3(5,6,7) code>
224 * if (!caught && x != 0) {
234 * All CATCH's must precede a CATCH_ALL.
235 * FINALLY must occur after any CATCH or CATCH_ALL.
236 * ENDTRY marks the end of the TRY code.
237 * TRY and ENDTRY are the mandatory parts of a TRY block.
238 * CATCH, CATCH_ALL, and FINALLY are all optional (although
239 * you'll probably use at least one, otherwise why "TRY"?)
241 * GET_MESSAGE returns string ptr to exception message
242 * when exception is thrown via THROW_MESSAGE()
244 * To throw/raise an exception.
247 * RETHROW rethrow the caught exception
249 * A cleanup callback is a function called in case an exception occurs
250 * and is not caught. It should be used to free any dynamically-allocated data.
251 * A pop or call_and_pop should occur at the same statement-nesting level
254 * CLEANUP_CB_PUSH(func, data)
256 * CLEANUP_CB_CALL_AND_POP
259 /* we do up to three passes through the bit of code after except_try_push(),
260 * and except_state is used to keep track of where we are.
262 #define EXCEPT_CAUGHT 1 /* exception has been caught, no need to rethrow at
265 #define EXCEPT_RETHROWN 2 /* the exception was rethrown from a CATCH
266 * block. Don't reenter the CATCH blocks, but do
267 * execute FINALLY and rethrow at ENDTRY */
269 #define EXCEPT_FINALLY 4 /* we've entered the FINALLY block - don't allow
270 * RETHROW, and don't reenter FINALLY if a
271 * different exception is thrown */
276 volatile int except_state = 0; \
277 static const except_id_t catch_spec[] = { \
278 { XCEPT_GROUP_WIRESHARK, XCEPT_CODE_ANY } }; \
279 except_try_push(catch_spec, 1, &exc); \
281 if(except_state & EXCEPT_CAUGHT) \
282 except_state |= EXCEPT_RETHROWN; \
283 except_state &= ~EXCEPT_CAUGHT; \
285 if (except_state == 0 && exc == 0) \
286 /* user's code goes here */
289 /* rethrow the exception if necessary */ \
290 if(!(except_state&EXCEPT_CAUGHT) && exc != 0) \
291 except_rethrow(exc); \
295 /* the (except_state |= EXCEPT_CAUGHT) in the below is a way of setting
296 * except_state before the user's code, without disrupting the user's code if
300 if (except_state == 0 && exc != 0 && \
301 exc->except_id.except_code == (x) && \
302 (except_state |= EXCEPT_CAUGHT)) \
303 /* user's code goes here */
305 #define CATCH2(x,y) \
306 if (except_state == 0 && exc != 0 && \
307 (exc->except_id.except_code == (x) || \
308 exc->except_id.except_code == (y)) && \
309 (except_state|=EXCEPT_CAUGHT)) \
310 /* user's code goes here */
312 #define CATCH3(x,y,z) \
313 if (except_state == 0 && exc != 0 && \
314 (exc->except_id.except_code == (x) || \
315 exc->except_id.except_code == (y) || \
316 exc->except_id.except_code == (z)) && \
317 (except_state|=EXCEPT_CAUGHT)) \
318 /* user's code goes here */
320 #define CATCH4(w,x,y,z) \
321 if (except_state == 0 && exc != 0 && \
322 (exc->except_id.except_code == (w) || \
323 exc->except_id.except_code == (x) || \
324 exc->except_id.except_code == (y) || \
325 exc->except_id.except_code == (z)) && \
326 (except_state|=EXCEPT_CAUGHT)) \
327 /* user's code goes here */
329 #define CATCH5(v,w,x,y,z) \
330 if (except_state == 0 && exc != 0 && \
331 (exc->except_id.except_code == (v) || \
332 exc->except_id.except_code == (w) || \
333 exc->except_id.except_code == (x) || \
334 exc->except_id.except_code == (y) || \
335 exc->except_id.except_code == (z)) && \
336 (except_state|=EXCEPT_CAUGHT)) \
337 /* user's code goes here */
339 #define CATCH6(u,v,w,x,y,z) \
340 if (except_state == 0 && exc != 0 && \
341 (exc->except_id.except_code == (u) || \
342 exc->except_id.except_code == (v) || \
343 exc->except_id.except_code == (w) || \
344 exc->except_id.except_code == (x) || \
345 exc->except_id.except_code == (y) || \
346 exc->except_id.except_code == (z)) && \
347 (except_state|=EXCEPT_CAUGHT)) \
348 /* user's code goes here */
351 if (except_state == 0 && exc != 0 && \
352 (except_state|=EXCEPT_CAUGHT)) \
353 /* user's code goes here */
356 if( !(except_state & EXCEPT_FINALLY) && (except_state|=EXCEPT_FINALLY)) \
357 /* user's code goes here */
360 except_throw(XCEPT_GROUP_WIRESHARK, (x), NULL)
362 #define THROW_ON(cond, x) G_STMT_START { \
364 except_throw(XCEPT_GROUP_WIRESHARK, (x), NULL); \
367 #define THROW_MESSAGE(x, y) \
368 except_throw(XCEPT_GROUP_WIRESHARK, (x), (y))
370 #define THROW_MESSAGE_ON(cond, x, y) G_STMT_START { \
372 except_throw(XCEPT_GROUP_WIRESHARK, (x), (y)); \
375 /* Throws a formatted message, its memory is cleared after catching it. */
376 #define THROW_FORMATTED(x, ...) \
377 except_throwf(XCEPT_GROUP_WIRESHARK, (x), __VA_ARGS__)
379 #define GET_MESSAGE except_message(exc)
383 /* check we're in a catch block */ \
384 g_assert(except_state == EXCEPT_CAUGHT); \
385 /* we can't use except_rethrow here, as that pops a catch block \
386 * off the stack, and we don't want to do that, because we want to \
387 * excecute the FINALLY {} block first. \
388 * except_throw doesn't provide an interface to rethrow an existing \
389 * exception; however, longjmping back to except_try_push() has the \
392 * Note also that THROW and RETHROW should provide much the same \
393 * functionality in terms of which blocks to enter, so any messing \
394 * about with except_state in here would indicate that THROW is \
395 * doing the wrong thing. \
397 longjmp(except_ch.except_jmp,1); \
400 #define EXCEPT_CODE except_code(exc)
402 /* Register cleanup functions in case an exception is thrown and not caught.
403 * From the Kazlib documentation, with modifications for use with the
404 * Wireshark-specific macros:
406 * CLEANUP_PUSH(func, arg)
408 * The call to CLEANUP_PUSH shall be matched with a call to
409 * CLEANUP_CALL_AND_POP or CLEANUP_POP which must occur in the same
410 * statement block at the same level of nesting. This requirement allows
411 * an implementation to provide a CLEANUP_PUSH macro which opens up a
412 * statement block and a CLEANUP_POP which closes the statement block.
413 * The space for the registered pointers can then be efficiently
414 * allocated from automatic storage.
416 * The CLEANUP_PUSH macro registers a cleanup handler that will be
417 * called if an exception subsequently occurs before the matching
418 * CLEANUP_[CALL_AND_]POP is executed, and is not intercepted and
419 * handled by a try-catch region that is nested between the two.
421 * The first argument to CLEANUP_PUSH is a pointer to the cleanup
422 * handler, a function that returns nothing and takes a single
423 * argument of type void*. The second argument is a void* value that
424 * is registered along with the handler. This value is what is passed
425 * to the registered handler, should it be called.
427 * Cleanup handlers are called in the reverse order of their nesting:
428 * inner handlers are called before outer handlers.
430 * The program shall not leave the cleanup region between
431 * the call to the macro CLEANUP_PUSH and the matching call to
432 * CLEANUP_[CALL_AND_]POP by means other than throwing an exception,
433 * or calling CLEANUP_[CALL_AND_]POP.
435 * Within the call to the cleanup handler, it is possible that new
436 * exceptions may happen. Such exceptions must be handled before the
437 * cleanup handler terminates. If the call to the cleanup handler is
438 * terminated by an exception, the behavior is undefined. The exception
439 * which triggered the cleanup is not yet caught; thus the program
440 * would be effectively trying to replace an exception with one that
441 * isn't in a well-defined state.
444 * CLEANUP_POP and CLEANUP_CALL_AND_POP
446 * A call to the CLEANUP_POP or CLEANUP_CALL_AND_POP macro shall match
447 * each call to CLEANUP_PUSH which shall be in the same statement block
448 * at the same nesting level. It shall match the most recent such a
449 * call that is not matched by a previous CLEANUP_[CALL_AND_]POP at
452 * These macros causes the registered cleanup handler to be removed. If
453 * CLEANUP_CALL_AND_POP is called, the cleanup handler is called.
454 * In that case, the registered context pointer is passed to the cleanup
455 * handler. If CLEANUP_POP is called, the cleanup handler is not called.
457 * The program shall not leave the region between the call to the
458 * macro CLEANUP_PUSH and the matching call to CLEANUP_[CALL_AND_]POP
459 * other than by throwing an exception, or by executing the
460 * CLEANUP_CALL_AND_POP.
465 #define CLEANUP_PUSH(f,a) except_cleanup_push((f),(a))
466 #define CLEANUP_POP except_cleanup_pop(0)
467 #define CLEANUP_CALL_AND_POP except_cleanup_pop(1)
469 /* Variants to allow nesting of except_cleanup_push w/o "shadowing" variables */
470 #define CLEANUP_PUSH_PFX(pfx,f,a) except_cleanup_push_pfx(pfx,(f),(a))
471 #define CLEANUP_POP_PFX(pfx) except_cleanup_pop_pfx(pfx,0)
472 #define CLEANUP_CALL_AND_POP_PFX(pfx) except_cleanup_pop_pfx(pfx,1)
476 #endif /* __EXCEPTIONS_H__ */