From Roland Knall via https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=9345
[metze/wireshark/wip.git] / epan / exceptions.h
1 /* exceptions.h
2  * Wireshark's exceptions.
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 #ifndef __EXCEPTIONS_H__
26 #define __EXCEPTIONS_H__
27
28 #ifndef XCEPT_H
29 #include "except.h"
30 #endif
31
32 /* Wireshark has only one exception group, to make these macros simple */
33 #define XCEPT_GROUP_WIRESHARK 1
34
35 /**
36     Index is out of range.
37     An attempt was made to read past the end of a buffer.
38     This generally means that the capture was done with a "slice"
39     length or "snapshot" length less than the maximum packet size,
40     and a link-layer packet was cut short by that, so not all of the
41     data in the link-layer packet was available.
42 **/
43 #define BoundsError             1
44
45 /**
46     Index is beyond reported length (not cap_len)
47     An attempt was made to read past the logical end of a buffer. This
48     differs from a BoundsError in that the parent protocol established a
49     limit past which this dissector should not process in the buffer and that
50     limit was exceeded.
51     This generally means that the packet is invalid, i.e. whatever
52     code constructed the packet and put it on the wire didn't put enough
53     data into it.  It is therefore currently reported as a "Malformed
54     packet".
55 **/
56 #define ReportedBoundsError     2
57
58 /**
59     Index is beyond fragment length but not reported length.
60     This means that the packet wasn't reassembled.
61 **/
62 #define FragmentBoundsError     3
63
64 /**
65     During dfilter parsing
66 **/
67 #define TypeError               4
68
69 /**
70     A bug was detected in a dissector.
71
72     DO NOT throw this with THROW(); that means that no details about
73     the dissector error will be reported.  (Instead, the message will
74     blame you for not providing details.)
75
76     Instead, use the DISSECTOR_ASSERT(), etc. macros in epan/proto.h.
77 **/
78 #define DissectorError          5
79
80 /**
81     Index is out of range.
82     An attempt was made to read past the end of a buffer.
83     This error is specific to SCSI data transfers where for some CDBs
84     it is normal that the data PDU might be short.
85     I.e. ReportLuns initially called with allocation_length=8, just enough
86     to get the "size" of lun list back after which the initiator will
87     reissue the command with an allocation_length that is big enough.
88 **/
89 #define ScsiBoundsError         6
90
91 /**
92     Running out of memory.
93     A dissector tried to allocate memory but that failed.
94 **/
95 #define OutOfMemoryError        7
96
97 /**
98     The reassembly state machine was passed a bad fragment offset,
99     or other similar issues. We used to use DissectorError in these
100     cases, but they're not necessarily the dissector's fault - if the packet
101     contains a bad fragment offset, the dissector shouldn't have to figure
102     that out by itself since that's what the reassembly machine is for.
103 **/
104 #define ReassemblyError         8
105
106 /*
107  * Catch errors that, if you're calling a subdissector and catching
108  * exceptions from the subdissector, and possibly dissecting more
109  * stuff after the subdissector returns or fails, mean it makes
110  * sense to continue dissecting:
111  *
112  * BoundsError indicates a configuration problem (the capture was
113  * set up to throw away data, and it did); there's no point in
114  * trying to dissect any more data, as there's no more data to dissect.
115  *
116  * FragmentBoundsError indicates a configuration problem (reassembly
117  * wasn't enabled or couldn't be done); there's no point in trying
118  * to dissect any more data, as there's no more data to dissect.
119  *
120  * OutOfMemoryError indicates what its name suggests; there's no point
121  * in trying to dissect any more data, as you're probably not going to
122  * have any more memory to use when dissecting them.
123  *
124  * Other errors indicate that there's some sort of problem with
125  * the packet; you should continue dissecting data, as it might
126  * be OK, and, even if it's not, you should report its problem
127  * separately.
128  */
129 #define CATCH_NONFATAL_ERRORS \
130         CATCH3(ReportedBoundsError, ScsiBoundsError, ReassemblyError)
131
132 /*
133  * Catch all bounds-checking errors.
134  */
135 #define CATCH_BOUNDS_ERRORS \
136         CATCH4(BoundsError, FragmentBoundsError, ReportedBoundsError, \
137                ScsiBoundsError)
138
139 /*
140  * Catch all bounds-checking errors, and catch dissector bugs.
141  * Should only be used at the top level, so that dissector bugs
142  * go all the way to the top level and get reported immediately.
143  */
144 #define CATCH_BOUNDS_AND_DISSECTOR_ERRORS \
145         CATCH6(BoundsError, FragmentBoundsError, ReportedBoundsError, \
146                ScsiBoundsError, DissectorError, ReassemblyError)
147
148 /* Usage:
149  *
150  * TRY {
151  *      code;
152  * }
153  *
154  * CATCH(exception) {
155  *      code;
156  * }
157  *
158  * CATCH2(exception1, exception2) {
159  *      code;
160  * }
161  *
162  * CATCH3(exception1, exception2, exception3) {
163  *      code;
164  * }
165  *
166  * CATCH4(exception1, exception2, exception3, exception4) {
167  *      code;
168  * }
169  *
170  * CATCH5(exception1, exception2, exception3, exception4, exception5) {
171  *      code;
172  * }
173  *
174  * CATCH6(exception1, exception2, exception3, exception4, exception5, exception6) {
175  *      code;
176  * }
177  *
178  * CATCH_NONFATAL_ERRORS {
179  *      code;
180  * }
181  *
182  * CATCH_BOUNDS_ERRORS {
183  *      code;
184  * }
185  *
186  * CATCH_BOUNDS_AND_DISSECTOR_ERRORS {
187  *      code;
188  * }
189  *
190  * CATCH_ALL {
191  *      code;
192  * }
193  *
194  * FINALLY {
195  *      code;
196  * }
197  *
198  * ENDTRY;
199  *
200  * ********* Never use 'goto' or 'return' inside the TRY, CATCH*, or
201  * ********* FINALLY blocks. Execution must proceed through ENDTRY before
202  * ********* branching out.
203  *
204  * This is really something like:
205  *
206  * {
207  *      caught = FALSE:
208  *      x = setjmp();
209  *      if (x == 0) {
210  *              <TRY code>
211  *      }
212  *      if (!caught && x == 1) {
213  *              caught = TRUE;
214  *              <CATCH(1) code>
215  *      }
216  *      if (!caught && x == 2) {
217  *              caught = TRUE;
218  *              <CATCH(2) code>
219  *      }
220  *      if (!caught && (x == 3 || x == 4)) {
221  *              caught = TRUE;
222  *              <CATCH2(3,4) code>
223  *      }
224  *      if (!caught && (x == 5 || x == 6 || x == 7)) {
225  *              caught = TRUE;
226  *              <CATCH3(5,6,7) code>
227  *      }
228  *      if (!caught && x != 0) {
229  *              caught = TRUE;
230  *              <CATCH_ALL code>
231  *      }
232  *      <FINALLY code>
233  *      if(!caught) {
234  *              RETHROW(x)
235  *      }
236  * }<ENDTRY tag>
237  *
238  * All CATCH's must precede a CATCH_ALL.
239  * FINALLY must occur after any CATCH or CATCH_ALL.
240  * ENDTRY marks the end of the TRY code.
241  * TRY and ENDTRY are the mandatory parts of a TRY block.
242  * CATCH, CATCH_ALL, and FINALLY are all optional (although
243  * you'll probably use at least one, otherwise why "TRY"?)
244  *
245  * GET_MESSAGE  returns string ptr to exception message
246  *              when exception is thrown via THROW_MESSAGE()
247  *
248  * To throw/raise an exception.
249  *
250  * THROW(exception)
251  * RETHROW                              rethrow the caught exception
252  *
253  * A cleanup callback is a function called in case an exception occurs
254  * and is not caught. It should be used to free any dynamically-allocated data.
255  * A pop or call_and_pop should occur at the same statement-nesting level
256  * as the push.
257  *
258  * CLEANUP_CB_PUSH(func, data)
259  * CLEANUP_CB_POP
260  * CLEANUP_CB_CALL_AND_POP
261  */
262
263 /* we do up to three passes through the bit of code after except_try_push(),
264  * and except_state is used to keep track of where we are.
265  */
266 #define EXCEPT_CAUGHT   1 /* exception has been caught, no need to rethrow at
267                            * ENDTRY */
268
269 #define EXCEPT_RETHROWN 2 /* the exception was rethrown from a CATCH
270                            * block. Don't reenter the CATCH blocks, but do
271                            * execute FINALLY and rethrow at ENDTRY */
272
273 #define EXCEPT_FINALLY  4 /* we've entered the FINALLY block - don't allow
274                            * RETHROW, and don't reenter FINALLY if a
275                            * different exception is thrown */
276
277 #define TRY \
278 {\
279         except_t *exc; \
280         volatile int except_state = 0; \
281         static const except_id_t catch_spec[] = { \
282                 { XCEPT_GROUP_WIRESHARK, XCEPT_CODE_ANY } }; \
283         except_try_push(catch_spec, 1, &exc); \
284                                                        \
285         if(except_state & EXCEPT_CAUGHT)               \
286             except_state |= EXCEPT_RETHROWN;           \
287         except_state &= ~EXCEPT_CAUGHT;                \
288                                                        \
289         if (except_state == 0 && exc == 0)             \
290                 /* user's code goes here */
291
292 #define ENDTRY \
293         /* rethrow the exception if necessary */ \
294         if(!(except_state&EXCEPT_CAUGHT) && exc != 0)  \
295             except_rethrow(exc);                 \
296         except_try_pop();\
297 }
298
299 /* the (except_state |= EXCEPT_CAUGHT) in the below is a way of setting
300  * except_state before the user's code, without disrupting the user's code if
301  * it's a one-liner.
302  */
303 #define CATCH(x) \
304         if (except_state == 0 && exc != 0 && \
305             exc->except_id.except_code == (x) && \
306             (except_state |= EXCEPT_CAUGHT)) \
307                 /* user's code goes here */
308
309 #define CATCH2(x,y) \
310         if (except_state == 0 && exc != 0 && \
311             (exc->except_id.except_code == (x) || \
312              exc->except_id.except_code == (y)) && \
313             (except_state|=EXCEPT_CAUGHT)) \
314                 /* user's code goes here */
315
316 #define CATCH3(x,y,z) \
317         if (except_state == 0 && exc != 0 && \
318             (exc->except_id.except_code == (x) || \
319              exc->except_id.except_code == (y) || \
320              exc->except_id.except_code == (z)) && \
321             (except_state|=EXCEPT_CAUGHT)) \
322                 /* user's code goes here */
323
324 #define CATCH4(w,x,y,z) \
325         if (except_state == 0 && exc != 0 && \
326             (exc->except_id.except_code == (w) || \
327              exc->except_id.except_code == (x) || \
328              exc->except_id.except_code == (y) || \
329              exc->except_id.except_code == (z)) && \
330             (except_state|=EXCEPT_CAUGHT)) \
331                 /* user's code goes here */
332
333 #define CATCH5(v,w,x,y,z) \
334         if (except_state == 0 && exc != 0 && \
335             (exc->except_id.except_code == (v) || \
336              exc->except_id.except_code == (w) || \
337              exc->except_id.except_code == (x) || \
338              exc->except_id.except_code == (y) || \
339              exc->except_id.except_code == (z)) && \
340             (except_state|=EXCEPT_CAUGHT)) \
341                 /* user's code goes here */
342
343 #define CATCH6(u,v,w,x,y,z) \
344         if (except_state == 0 && exc != 0 && \
345             (exc->except_id.except_code == (u) || \
346              exc->except_id.except_code == (v) || \
347              exc->except_id.except_code == (w) || \
348              exc->except_id.except_code == (x) || \
349              exc->except_id.except_code == (y) || \
350              exc->except_id.except_code == (z)) && \
351             (except_state|=EXCEPT_CAUGHT)) \
352                 /* user's code goes here */
353
354 #define CATCH_ALL \
355         if (except_state == 0 && exc != 0 && \
356             (except_state|=EXCEPT_CAUGHT)) \
357                 /* user's code goes here */
358
359 #define FINALLY \
360         if( !(except_state & EXCEPT_FINALLY) && (except_state|=EXCEPT_FINALLY)) \
361                 /* user's code goes here */
362
363 #define THROW(x) \
364         except_throw(XCEPT_GROUP_WIRESHARK, (x), NULL)
365
366 #define THROW_ON(cond, x) G_STMT_START { \
367         if ((cond)) \
368                 except_throw(XCEPT_GROUP_WIRESHARK, (x), NULL); \
369 } G_STMT_END
370
371 #define THROW_MESSAGE(x, y) \
372         except_throw(XCEPT_GROUP_WIRESHARK, (x), (y))
373
374 #define THROW_MESSAGE_ON(cond, x, y) G_STMT_START { \
375         if ((cond)) \
376                 except_throw(XCEPT_GROUP_WIRESHARK, (x), (y)); \
377 } G_STMT_END
378
379 #define GET_MESSAGE                     except_message(exc)
380
381 #define RETHROW                                     \
382     {                                               \
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 \
390          * desired effect.                          \
391          *                                          \
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.                   \
396          */                                         \
397         longjmp(except_ch.except_jmp,1);            \
398     }
399
400 #define EXCEPT_CODE                     except_code(exc)
401
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:
405  *
406  * CLEANUP_PUSH(func, arg)
407  *
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.
415  *
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.
420  *
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.
426  *
427  *  Cleanup handlers are called in the reverse order of their nesting:
428  *  inner handlers are called before outer handlers.
429  *
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.
434  *
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.
442  *
443  *
444  * CLEANUP_POP and CLEANUP_CALL_AND_POP
445  *
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
450  *  the same level.
451  *
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.
456  *
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.
461  *
462  */
463
464
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)
468
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)
473
474
475
476 #endif /* __EXCEPTIONS_H__ */