1 /*
2  * Copyright (C) 2004, 2005, 2008  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1997, 1999, 2001  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include "port_before.h"
19 
20 #include <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 #include <isc/assertions.h>
26 
27 #include "port_after.h"
28 
29 /*
30  * Forward.
31  */
32 
33 static void default_assertion_failed(const char *, int, assertion_type,
34 				     const char *, int);
35 
36 /*
37  * Public.
38  */
39 
40 assertion_failure_callback __assertion_failed = default_assertion_failed;
41 
42 void
set_assertion_failure_callback(assertion_failure_callback f)43 set_assertion_failure_callback(assertion_failure_callback f) {
44 	if (f == NULL)
45 		__assertion_failed = default_assertion_failed;
46 	else
47 		__assertion_failed = f;
48 }
49 
50 const char *
assertion_type_to_text(assertion_type type)51 assertion_type_to_text(assertion_type type) {
52 	const char *result;
53 
54 	switch (type) {
55 	case assert_require:
56 		result = "REQUIRE";
57 		break;
58 	case assert_ensure:
59 		result = "ENSURE";
60 		break;
61 	case assert_insist:
62 		result = "INSIST";
63 		break;
64 	case assert_invariant:
65 		result = "INVARIANT";
66 		break;
67 	default:
68 		result = NULL;
69 	}
70 	return (result);
71 }
72 
73 /*
74  * Private.
75  */
76 
77 /* coverity[+kill] */
78 static void
default_assertion_failed(const char * file,int line,assertion_type type,const char * cond,int print_errno)79 default_assertion_failed(const char *file, int line, assertion_type type,
80 			 const char *cond, int print_errno)
81 {
82 	fprintf(stderr, "%s:%d: %s(%s)%s%s failed.\n",
83 		file, line, assertion_type_to_text(type), cond,
84 		(print_errno) ? ": " : "",
85 		(print_errno) ? strerror(errno) : "");
86 	abort();
87 	/* NOTREACHED */
88 }
89 
90 /*! \file */
91