1 #ifndef __EXCEPTIONS_H__
2 #define __EXCEPTIONS_H__
8 /* Wireshark has only one exception group, to make these macros simple */
9 #define XCEPT_GROUP_WIRESHARK 1
11 /* Wireshark's exceptions */
14 Index is out of range.
15 An attempt was made to read past the end of a buffer.
16 This generally means that the capture was done with a "slice"
17 length or "snapshot" length less than the maximum packet size,
18 and a link-layer packet was cut short by that, so not all of the
19 data in the link-layer packet was available.
24 Index is beyond reported length (not cap_len)
25 An attempt was made to read past the logical end of a buffer. This
26 differs from a BoundsError in that the parent protocol established a
27 limit past which this dissector should not process in the buffer and that
29 This generally means that the packet is invalid, i.e. whatever
30 code constructed the packet and put it on the wire didn't put enough
31 data into it. It is therefore currently reported as a "Malformed
33 However, it also happens in some cases where the packet was fragmented
34 and the fragments weren't reassembled. We need to add another length
35 field to a tvbuff, so that "length of the packet from the link layer"
36 and "length of the packet were it fully reassembled" are different,
37 and going past the first of those without going past the second would
38 throw a different exception, which would be reported as an "Unreassembled
39 packet" rather than a "Malformed packet".
41 #define ReportedBoundsError 2
44 During dfilter parsing
49 A bug was detected in a dissector.
51 DO NOT throw this with THROW(); that means that no details about
52 the dissector error will be reported. (Instead, the message will
53 blame you for not providing details.)
55 Instead, use the DISSECTOR_ASSERT(), etc. macros in epan/proto.h.
57 #define DissectorError 4
60 Index is out of range.
61 An attempt was made to read past the end of a buffer.
62 This error is specific to SCSI data transfers where for some CDBs
63 it is normal that the data PDU might be short.
64 I.e. ReportLuns initially called with allocation_length=8, just enough
65 to get the "size" of lun list back after which the initiator will
66 reissue the command with an allocation_length that is big enough.
68 #define ScsiBoundsError 5
71 Running out of memory.
72 A dissector tried to allocate memory but that failed.
74 #define OutOfMemoryError 6
87 * CATCH2(exception1, exception2) {
101 * ********* Never use 'goto' or 'return' inside the TRY, CATCH, CATCH_ALL,
102 * ********* or FINALLY blocks. Execution must proceed through ENDTRY before
103 * ********* branching out.
105 * This is really something like:
113 * if (!caught && x == 1) {
117 * if (!caught && x == 2) {
121 * if (!caught && (x == 3 || x == 4)) {
125 * if (!caught && x != 0) {
135 * All CATCH's must precede a CATCH_ALL.
136 * FINALLY must occur after any CATCH or CATCH_ALL.
137 * ENDTRY marks the end of the TRY code.
138 * TRY and ENDTRY are the mandatory parts of a TRY block.
139 * CATCH, CATCH_ALL, and FINALLY are all optional (although
140 * you'll probably use at least one, otherwise why "TRY"?)
142 * GET_MESSAGE returns string ptr to exception message
143 * when exception is thrown via THROW_MESSAGE()
145 * To throw/raise an exception.
148 * RETHROW rethrow the caught exception
150 * A cleanup callback is a function called in case an exception occurs
151 * and is not caught. It should be used to free any dynamically-allocated data.
152 * A pop or call_and_pop should occur at the same statement-nesting level
155 * CLEANUP_CB_PUSH(func, data)
157 * CLEANUP_CB_CALL_AND_POP
160 /* we do up to three passes through the bit of code after except_try_push(),
161 * and except_state is used to keep track of where we are.
163 #define EXCEPT_CAUGHT 1 /* exception has been caught, no need to rethrow at
166 #define EXCEPT_RETHROWN 2 /* the exception was rethrown from a CATCH
167 * block. Don't reenter the CATCH blocks, but do
168 * execute FINALLY and rethrow at END_TRY */
170 #define EXCEPT_FINALLY 4 /* we've entered the FINALLY block - don't allow
171 * RETHROW, and don't reenter FINALLY if a
172 * different exception is thrown */
177 volatile int except_state = 0; \
178 static const except_id_t catch_spec[] = { \
179 { XCEPT_GROUP_WIRESHARK, XCEPT_CODE_ANY } }; \
180 except_try_push(catch_spec, 1, &exc); \
182 if(except_state & EXCEPT_CAUGHT) \
183 except_state |= EXCEPT_RETHROWN; \
184 except_state &= ~EXCEPT_CAUGHT; \
186 if (except_state == 0 && exc == 0) \
187 /* user's code goes here */
190 /* rethrow the exception if necessary */ \
191 if(!(except_state&EXCEPT_CAUGHT) && exc != 0) \
192 except_rethrow(exc); \
196 /* the (except_state |= EXCEPT_CAUGHT) in the below is a way of setting
197 * except_state before the user's code, without disrupting the user's code if
201 if (except_state == 0 && exc != 0 && exc->except_id.except_code == (x) && \
202 (except_state |= EXCEPT_CAUGHT)) \
203 /* user's code goes here */
205 #define CATCH2(x,y) \
206 if (except_state == 0 && exc != 0 && \
207 (exc->except_id.except_code == (x) || exc->except_id.except_code == (y)) && \
208 (except_state|=EXCEPT_CAUGHT)) \
209 /* user's code goes here */
212 if (except_state == 0 && exc != 0 && \
213 (except_state|=EXCEPT_CAUGHT)) \
214 /* user's code goes here */
218 if( !(except_state & EXCEPT_FINALLY) && (except_state|=EXCEPT_FINALLY)) \
219 /* user's code goes here */
222 except_throw(XCEPT_GROUP_WIRESHARK, (x), NULL)
224 #define THROW_MESSAGE(x, y) \
225 except_throw(XCEPT_GROUP_WIRESHARK, (x), (y))
227 #define GET_MESSAGE except_message(exc)
231 /* check we're in a catch block */ \
232 g_assert(except_state == EXCEPT_CAUGHT); \
233 /* we can't use except_rethrow here, as that pops a catch block \
234 * off the stack, and we don't want to do that, because we want to \
235 * excecute the FINALLY {} block first. \
236 * except_throw doesn't provide an interface to rethrow an existing \
237 * exception; however, longjmping back to except_try_push() has the \
240 * Note also that THROW and RETHROW should provide much the same \
241 * functionality in terms of which blocks to enter, so any messing \
242 * about with except_state in here would indicate that THROW is \
243 * doing the wrong thing. \
245 longjmp(except_ch.except_jmp,1); \
248 #define EXCEPT_CODE except_code(exc)
250 /* Register cleanup functions in case an exception is thrown and not caught.
251 * From the Kazlib documentation, with modifications for use with the
252 * Wireshark-specific macros:
254 * CLEANUP_PUSH(func, arg)
256 * The call to CLEANUP_PUSH shall be matched with a call to
257 * CLEANUP_CALL_AND_POP or CLEANUP_POP which must occur in the same
258 * statement block at the same level of nesting. This requirement allows
259 * an implementation to provide a CLEANUP_PUSH macro which opens up a
260 * statement block and a CLEANUP_POP which closes the statement block.
261 * The space for the registered pointers can then be efficiently
262 * allocated from automatic storage.
264 * The CLEANUP_PUSH macro registers a cleanup handler that will be
265 * called if an exception subsequently occurs before the matching
266 * CLEANUP_[CALL_AND_]POP is executed, and is not intercepted and
267 * handled by a try-catch region that is nested between the two.
269 * The first argument to CLEANUP_PUSH is a pointer to the cleanup
270 * handler, a function that returns nothing and takes a single
271 * argument of type void*. The second argument is a void* value that
272 * is registered along with the handler. This value is what is passed
273 * to the registered handler, should it be called.
275 * Cleanup handlers are called in the reverse order of their nesting:
276 * inner handlers are called before outer handlers.
278 * The program shall not leave the cleanup region between
279 * the call to the macro CLEANUP_PUSH and the matching call to
280 * CLEANUP_[CALL_AND_]POP by means other than throwing an exception,
281 * or calling CLEANUP_[CALL_AND_]POP.
283 * Within the call to the cleanup handler, it is possible that new
284 * exceptions may happen. Such exceptions must be handled before the
285 * cleanup handler terminates. If the call to the cleanup handler is
286 * terminated by an exception, the behavior is undefined. The exception
287 * which triggered the cleanup is not yet caught; thus the program
288 * would be effectively trying to replace an exception with one that
289 * isn't in a well-defined state.
292 * CLEANUP_POP and CLEANUP_CALL_AND_POP
294 * A call to the CLEANUP_POP or CLEANUP_CALL_AND_POP macro shall match
295 * each call to CLEANUP_PUSH which shall be in the same statement block
296 * at the same nesting level. It shall match the most recent such a
297 * call that is not matched by a previous CLEANUP_[CALL_AND_]POP at
300 * These macros causes the registered cleanup handler to be removed. If
301 * CLEANUP_CALL_AND_POP is called, the cleanup handler is called.
302 * In that case, the registered context pointer is passed to the cleanup
303 * handler. If CLEANUP_POP is called, the cleanup handler is not called.
305 * The program shall not leave the region between the call to the
306 * macro CLEANUP_PUSH and the matching call to CLEANUP_[CALL_AND_]POP
307 * other than by throwing an exception, or by executing the
308 * CLEANUP_CALL_AND_POP.
313 #define CLEANUP_PUSH(f,a) except_cleanup_push((f),(a))
314 #define CLEANUP_POP except_cleanup_pop(0)
315 #define CLEANUP_CALL_AND_POP except_cleanup_pop(1)
317 #endif /* __EXCEPTIONS_H__ */