2 * Portable Exception Handling for ANSI C.
3 * Copyright (C) 1999 Kaz Kylheku <kaz@ashi.footprints.net>
5 * Free Software License:
7 * All rights are reserved by the author, with the following exceptions:
8 * Permission is granted to freely reproduce and distribute this software,
9 * possibly in exchange for a fee, provided that this copyright notice appears
10 * intact. Permission is also granted to adapt this software to produce
11 * derivative works, as long as the modified versions carry this copyright
12 * notice and additional notices stating that the work has been modified.
13 * This source code may be translated into executable form and incorporated
14 * into proprietary software; there is no requirement for such software to
15 * contain a copyright notice related to this source.
19 * Modified to support throwing an exception with a null message pointer,
20 * and to have the message not be const (as we generate messages with
21 * "g_strdup_sprintf()", which means they need to be freed; using
22 * a null message means that we don't have to use a special string
23 * for exceptions with no message, and don't have to worry about
38 #ifdef KAZLIB_TEST_MAIN
39 #include <wsutil/ws_printf.h> /* ws_debug_printf */
44 #include "exceptions.h"
47 #define XCEPT_BUFFER_SIZE 1024
49 #ifdef KAZLIB_POSIX_THREADS
53 static pthread_mutex_t init_mtx = PTHREAD_MUTEX_INITIALIZER;
54 static int init_counter;
55 static pthread_key_t top_key;
56 static pthread_key_t uh_key;
57 static pthread_key_t alloc_key;
58 static pthread_key_t dealloc_key;
59 static void unhandled_catcher(except_t *);
61 #define get_top() ((struct except_stacknode *) pthread_getspecific(top_key))
62 #define set_top(T) (pthread_setspecific(top_key, (T)), (void)((T) == (struct except_stacknode *) 0))
63 #define set_catcher(C) (pthread_setspecific(uh_key, (void *) (C)), (void)((C) == (void (*)(except_t *)) 0))
64 #define set_alloc(A) (pthread_setspecific(alloc_key, (void *) (A)), (void)((A) == (void *(*)(size_t)) 0))
65 #define set_dealloc(D) (pthread_setspecific(dealloc_key, (void *) (D)), (void)((D) == (void (*)(void *)) 0))
67 static void (*get_catcher(void))(except_t *)
69 void (*catcher)(except_t *) = (void (*)(except_t *)) pthread_getspecific(uh_key);
70 return (catcher == 0) ? unhandled_catcher : catcher;
73 static void *(*get_alloc(void))(size_t)
75 void *(*alloc)(size_t) = (void *(*)(size_t)) pthread_getspecific(alloc_key);
76 return (alloc == 0) ? malloc : alloc;
79 static void (*get_dealloc(void))(void *)
81 void (*dealloc)(void *) = (void (*)(void *)) pthread_getspecific(dealloc_key);
82 return (dealloc == 0) ? free : dealloc;
89 pthread_mutex_lock(&init_mtx);
91 assert (init_counter < INT_MAX);
93 if (init_counter++ == 0) {
94 int top_ok = (pthread_key_create(&top_key, 0) == 0);
95 int uh_ok = (pthread_key_create(&uh_key, 0) == 0);
96 int alloc_ok = (pthread_key_create(&alloc_key, 0) == 0);
97 int dealloc_ok = (pthread_key_create(&dealloc_key, 0) == 0);
99 if (!top_ok || !uh_ok || !alloc_ok || !dealloc_ok) {
103 pthread_key_delete(top_key);
105 pthread_key_delete(uh_key);
107 pthread_key_delete(alloc_key);
109 pthread_key_delete(dealloc_key);
113 pthread_mutex_unlock(&init_mtx);
118 void except_deinit(void)
120 pthread_mutex_lock(&init_mtx);
122 assert (init_counter > 0);
124 if (--init_counter == 0) {
125 pthread_key_delete(top_key);
126 pthread_key_delete(uh_key);
127 pthread_key_delete(alloc_key);
128 pthread_key_delete(dealloc_key);
131 pthread_mutex_unlock(&init_mtx);
134 #else /* no thread support */
136 static int init_counter;
137 static void unhandled_catcher(except_t *);
138 static void (*uh_catcher_ptr)(except_t *) = unhandled_catcher;
139 /* We need this 'size_t' cast due to a glitch in GLib where g_malloc was prototyped
140 * as 'gpointer g_malloc (gulong n_bytes)'. This was later fixed to the correct prototype
141 * 'gpointer g_malloc (gsize n_bytes)'. In Wireshark we use the latter prototype
142 * throughout the code. We can get away with this even with older versions of GLib by
143 * adding a '(void *(*)(size_t))' cast whenever we refer to g_malloc. The only platform
144 * supported by Wireshark where this isn't safe (sizeof size_t != sizeof gulong) is Win64.
145 * However, we _always_ bundle the newest version of GLib on this platform so
146 * the size_t issue doesn't exists here. Pheew.. */
147 static void *(*allocator)(size_t) = (void *(*)(size_t)) g_malloc;
148 static void (*deallocator)(void *) = g_free;
149 static struct except_stacknode *stack_top;
151 #define get_top() (stack_top)
152 #define set_top(T) (stack_top = (T))
153 #define get_catcher() (uh_catcher_ptr)
154 #define set_catcher(C) (uh_catcher_ptr = (C))
155 #define get_alloc() (allocator)
156 #define set_alloc(A) (allocator = (A))
157 #define get_dealloc() (deallocator)
158 #define set_dealloc(D) (deallocator = (D))
160 int except_init(void)
162 assert (init_counter < INT_MAX);
167 void except_deinit(void)
169 assert (init_counter > 0);
176 static int match(const volatile except_id_t *thrown, const except_id_t *caught)
178 int group_match = (caught->except_group == XCEPT_GROUP_ANY ||
179 caught->except_group == thrown->except_group);
180 int code_match = (caught->except_code == XCEPT_CODE_ANY ||
181 caught->except_code == thrown->except_code);
183 return group_match && code_match;
186 WS_NORETURN static void do_throw(except_t *except)
188 struct except_stacknode *top;
190 assert (except->except_id.except_group != 0 &&
191 except->except_id.except_code != 0);
193 for (top = get_top(); top != 0; top = top->except_down) {
194 if (top->except_type == XCEPT_CLEANUP) {
195 top->except_info.except_cleanup->except_func(top->except_info.except_cleanup->except_context);
197 struct except_catch *catcher = top->except_info.except_catcher;
198 const except_id_t *pi = catcher->except_id;
201 assert (top->except_type == XCEPT_CATCHER);
202 except_free(catcher->except_obj.except_dyndata);
204 for (i = 0; i < catcher->except_size; pi++, i++) {
205 if (match(&except->except_id, pi)) {
206 catcher->except_obj = *except;
208 longjmp(catcher->except_jmp, 1);
215 get_catcher()(except); /* unhandled exception */
219 static void unhandled_catcher(except_t *except)
221 if (except->except_message == NULL) {
222 fprintf(stderr, "Unhandled exception (group=%lu, code=%lu)\n",
223 except->except_id.except_group,
224 except->except_id.except_code);
226 fprintf(stderr, "Unhandled exception (\"%s\", group=%lu, code=%lu)\n",
227 except->except_message, except->except_id.except_group,
228 except->except_id.except_code);
233 static void stack_push(struct except_stacknode *node)
235 node->except_down = get_top();
239 void except_setup_clean(struct except_stacknode *esn,
240 struct except_cleanup *ecl, void (*cleanf)(void *), void *context)
242 esn->except_type = XCEPT_CLEANUP;
243 ecl->except_func = cleanf;
244 ecl->except_context = context;
245 esn->except_info.except_cleanup = ecl;
249 void except_setup_try(struct except_stacknode *esn,
250 struct except_catch *ech, const except_id_t id[], size_t size)
253 ech->except_size = size;
254 ech->except_obj.except_dyndata = 0;
255 esn->except_type = XCEPT_CATCHER;
256 esn->except_info.except_catcher = ech;
260 struct except_stacknode *except_pop(void)
262 struct except_stacknode *top = get_top();
263 set_top(top->except_down);
267 WS_NORETURN void except_rethrow(except_t *except)
269 struct except_stacknode *top = get_top();
271 assert (top->except_type == XCEPT_CATCHER);
272 assert (&top->except_info.except_catcher->except_obj == except);
273 set_top(top->except_down);
277 WS_NORETURN void except_throw(long group, long code, const char *msg)
281 except.except_id.except_group = group;
282 except.except_id.except_code = code;
283 except.except_message = msg;
284 except.except_dyndata = 0;
287 if (code == DissectorError && IsDebuggerPresent()) {
295 WS_NORETURN void except_throwd(long group, long code, const char *msg, void *data)
299 except.except_id.except_group = group;
300 except.except_id.except_code = code;
301 except.except_message = msg;
302 except.except_dyndata = data;
308 * XXX - should we use g_strdup_sprintf() here, so we're not limited by
309 * XCEPT_BUFFER_SIZE? We could then just use this to generate formatted
312 WS_NORETURN void except_throwf(long group, long code, const char *fmt, ...)
314 char *buf = (char *)except_alloc(XCEPT_BUFFER_SIZE);
318 g_vsnprintf(buf, XCEPT_BUFFER_SIZE, fmt, vl);
320 except_throwd(group, code, buf, buf);
323 void (*except_unhandled_catcher(void (*new_catcher)(except_t *)))(except_t *)
325 void (*old_catcher)(except_t *) = get_catcher();
326 set_catcher(new_catcher);
332 #undef except_message
335 unsigned long except_code(except_t *ex)
337 return ex->except_id.except_code;
340 unsigned long except_group(except_t *ex)
342 return ex->except_id.except_group;
345 const char *except_message(except_t *ex)
347 return ex->except_message;
350 void *except_data(except_t *ex)
352 return ex->except_dyndata;
355 void *except_take_data(except_t *ex)
357 void *data = ex->except_dyndata;
358 ex->except_dyndata = 0;
362 void except_set_allocator(void *(*alloc)(size_t), void (*dealloc)(void *))
365 set_dealloc(dealloc);
368 void *except_alloc(size_t size)
370 void *ptr = get_alloc()(size);
373 except_throw(XCEPT_BAD_ALLOC, 0, "out of memory");
377 void except_free(void *ptr)
382 #ifdef KAZLIB_TEST_MAIN
385 static void cleanup(void *arg)
387 ws_debug_printf("cleanup(\"%s\") called\n", (char *) arg);
390 static void bottom_level(void)
393 ws_debug_printf("throw exception? "); fflush(stdout);
394 fgets(buf, sizeof buf, stdin);
396 if (buf[0] >= 0 && (buf[0] == 'Y' || buf[0] == 'y'))
397 except_throw(1, 1, "nasty exception");
400 static void top_level(void)
402 except_cleanup_push(cleanup, "argument");
404 except_cleanup_pop(0);
407 int main(int argc, char **argv)
409 static const except_id_t catch[] = { { 1, 1 }, { 1, 2 } };
414 * Nested exception ``try blocks''
418 except_try_push(catch, 2, &ex);
421 except_try_push(catch, 2, &ex);
426 msg = except_message(ex);
428 ws_debug_printf("caught exception (inner): s=%lu, c=%lu\n",
429 except_group(ex), except_code(ex));
431 ws_debug_printf("caught exception (inner): \"%s\", s=%lu, c=%lu\n",
432 msg, except_group(ex), except_code(ex));
439 msg = except_message(ex);
441 ws_debug_printf("caught exception (outer): s=%lu, c=%lu\n",
442 except_group(ex), except_code(ex));
444 ws_debug_printf("caught exception (outer): \"%s\", s=%lu, c=%lu\n",
445 except_message(ex), except_group(ex), except_code(ex));
449 except_throw(99, 99, "exception in main");
457 * Editor modelines - http://www.wireshark.org/tools/modelines.html
462 * indent-tabs-mode: nil
465 * vi: set shiftwidth=4 tabstop=8 expandtab:
466 * :indentSize=4:tabSize=8:noTabs=true: