CIP: fix no previous prototype for ‘add_cip_pccc_function_to_info_column’ [-Wmissing...
[metze/wireshark/wip.git] / epan / exceptions.h
1 /* exceptions.h
2  * Wireshark's exceptions.
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 1998 Gerald Combs
7  *
8  * SPDX-License-Identifier: GPL-2.0-or-later
9  */
10
11 #ifndef __EXCEPTIONS_H__
12 #define __EXCEPTIONS_H__
13
14 #include "except.h"
15
16 /* Wireshark has only one exception group, to make these macros simple */
17 #define XCEPT_GROUP_WIRESHARK 1
18
19 /**
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.
26 **/
27 #define BoundsError             1
28
29 /**
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
34     limit was exceeded.
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
38     packet".
39 **/
40 #define ReportedBoundsError     2
41
42 /**
43     Index is beyond fragment length but not reported length.
44     This means that the packet wasn't reassembled.
45 **/
46 #define FragmentBoundsError     3
47
48 /**
49     During dfilter parsing
50 **/
51 #define TypeError               4
52
53 /**
54     A bug was detected in a dissector.
55
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.)
59
60     Instead, use the DISSECTOR_ASSERT(), etc. macros in epan/proto.h.
61 **/
62 #define DissectorError          5
63
64 /**
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.
72 **/
73 #define ScsiBoundsError         6
74
75 /**
76     Running out of memory.
77     A dissector tried to allocate memory but that failed.
78 **/
79 #define OutOfMemoryError        7
80
81 /**
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.
87 **/
88 #define ReassemblyError         8
89
90 /*
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:
95  *
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.
99  *
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.
103  *
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.
107  *
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
111  * separately.
112  */
113 #define CATCH_NONFATAL_ERRORS \
114         CATCH3(ReportedBoundsError, ScsiBoundsError, ReassemblyError)
115
116 /*
117  * Catch all bounds-checking errors.
118  */
119 #define CATCH_BOUNDS_ERRORS \
120         CATCH4(BoundsError, FragmentBoundsError, ReportedBoundsError, \
121                ScsiBoundsError)
122
123 /*
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.
127  */
128 #define CATCH_BOUNDS_AND_DISSECTOR_ERRORS \
129         CATCH6(BoundsError, FragmentBoundsError, ReportedBoundsError, \
130                ScsiBoundsError, DissectorError, ReassemblyError)
131
132 /* Usage:
133  *
134  * TRY {
135  *      code;
136  * }
137  *
138  * CATCH(exception) {
139  *      code;
140  * }
141  *
142  * CATCH2(exception1, exception2) {
143  *      code;
144  * }
145  *
146  * CATCH3(exception1, exception2, exception3) {
147  *      code;
148  * }
149  *
150  * CATCH4(exception1, exception2, exception3, exception4) {
151  *      code;
152  * }
153  *
154  * CATCH5(exception1, exception2, exception3, exception4, exception5) {
155  *      code;
156  * }
157  *
158  * CATCH6(exception1, exception2, exception3, exception4, exception5, exception6) {
159  *      code;
160  * }
161  *
162  * CATCH_NONFATAL_ERRORS {
163  *      code;
164  * }
165  *
166  * CATCH_BOUNDS_ERRORS {
167  *      code;
168  * }
169  *
170  * CATCH_BOUNDS_AND_DISSECTOR_ERRORS {
171  *      code;
172  * }
173  *
174  * CATCH_ALL {
175  *      code;
176  * }
177  *
178  * FINALLY {
179  *      code;
180  * }
181  *
182  * ENDTRY;
183  *
184  * ********* Never use 'goto' or 'return' inside the TRY, CATCH*, or
185  * ********* FINALLY blocks. Execution must proceed through ENDTRY before
186  * ********* branching out.
187  *
188  * This is really something like:
189  *
190  * {
191  *      caught = FALSE:
192  *      x = setjmp();
193  *      if (x == 0) {
194  *              <TRY code>
195  *      }
196  *      if (!caught && x == 1) {
197  *              caught = TRUE;
198  *              <CATCH(1) code>
199  *      }
200  *      if (!caught && x == 2) {
201  *              caught = TRUE;
202  *              <CATCH(2) code>
203  *      }
204  *      if (!caught && (x == 3 || x == 4)) {
205  *              caught = TRUE;
206  *              <CATCH2(3,4) code>
207  *      }
208  *      if (!caught && (x == 5 || x == 6 || x == 7)) {
209  *              caught = TRUE;
210  *              <CATCH3(5,6,7) code>
211  *      }
212  *      if (!caught && x != 0) {
213  *              caught = TRUE;
214  *              <CATCH_ALL code>
215  *      }
216  *      <FINALLY code>
217  *      if(!caught) {
218  *              RETHROW(x)
219  *      }
220  * }<ENDTRY tag>
221  *
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"?)
228  *
229  * GET_MESSAGE  returns string ptr to exception message
230  *              when exception is thrown via THROW_MESSAGE()
231  *
232  * To throw/raise an exception.
233  *
234  * THROW(exception)
235  * RETHROW                              rethrow the caught exception
236  *
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
240  * as the push.
241  *
242  * CLEANUP_CB_PUSH(func, data)
243  * CLEANUP_CB_POP
244  * CLEANUP_CB_CALL_AND_POP
245  */
246
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.
249  */
250 #define EXCEPT_CAUGHT   1 /* exception has been caught, no need to rethrow at
251                            * ENDTRY */
252
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 */
256
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 */
260
261 #define TRY \
262 {\
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); \
268                                                        \
269         if(except_state & EXCEPT_CAUGHT)               \
270             except_state |= EXCEPT_RETHROWN;           \
271         except_state &= ~EXCEPT_CAUGHT;                \
272                                                        \
273         if (except_state == 0 && exc == 0)             \
274                 /* user's code goes here */
275
276 #define ENDTRY \
277         /* rethrow the exception if necessary */ \
278         if(!(except_state&EXCEPT_CAUGHT) && exc != 0)  \
279             except_rethrow(exc);                 \
280         except_try_pop();\
281 }
282
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
285  * it's a one-liner.
286  */
287 #define CATCH(x) \
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 */
292
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 */
299
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 */
307
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 */
316
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 */
326
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 */
337
338 #define CATCH_ALL \
339         if (except_state == 0 && exc != 0 && \
340             (except_state|=EXCEPT_CAUGHT)) \
341                 /* user's code goes here */
342
343 #define FINALLY \
344         if( !(except_state & EXCEPT_FINALLY) && (except_state|=EXCEPT_FINALLY)) \
345                 /* user's code goes here */
346
347 #define THROW(x) \
348         except_throw(XCEPT_GROUP_WIRESHARK, (x), NULL)
349
350 #define THROW_ON(cond, x) G_STMT_START { \
351         if ((cond)) \
352                 except_throw(XCEPT_GROUP_WIRESHARK, (x), NULL); \
353 } G_STMT_END
354
355 #define THROW_MESSAGE(x, y) \
356         except_throw(XCEPT_GROUP_WIRESHARK, (x), (y))
357
358 #define THROW_MESSAGE_ON(cond, x, y) G_STMT_START { \
359         if ((cond)) \
360                 except_throw(XCEPT_GROUP_WIRESHARK, (x), (y)); \
361 } G_STMT_END
362
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__)
366
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)
370
371 #define GET_MESSAGE                     except_message(exc)
372
373 #define RETHROW                                     \
374     {                                               \
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 \
382          * desired effect.                          \
383          *                                          \
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.                   \
388          */                                         \
389         longjmp(except_ch.except_jmp,1);            \
390     }
391
392 #define EXCEPT_CODE                     except_code(exc)
393
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:
397  *
398  * CLEANUP_PUSH(func, arg)
399  *
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.
407  *
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.
412  *
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.
418  *
419  *  Cleanup handlers are called in the reverse order of their nesting:
420  *  inner handlers are called before outer handlers.
421  *
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.
426  *
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.
434  *
435  *
436  * CLEANUP_POP and CLEANUP_CALL_AND_POP
437  *
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
442  *  the same level.
443  *
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.
448  *
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.
453  *
454  */
455
456
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)
460
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)
465
466
467
468 #endif /* __EXCEPTIONS_H__ */