xref: /openbsd-src/usr.bin/dig/lib/isc/assertions.c (revision bdd5abfe143ea10572b424af4dc802c404f44b2f)
1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14  * PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 /* $Id: assertions.c,v 1.4 2020/02/13 08:15:31 florian Exp $ */
18 
19 /*! \file */
20 
21 
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 
26 #include <isc/assertions.h>
27 
28 /*%
29  * Forward.
30  */
31 static void
32 default_callback(const char *, int, isc_assertiontype_t, const char *);
33 
34 static isc_assertioncallback_t isc_assertion_failed_cb = default_callback;
35 
36 /*%
37  * Public.
38  */
39 
40 /*% assertion failed handler */
41 void
42 isc_assertion_failed(const char *file, int line, isc_assertiontype_t type,
43 		     const char *cond)
44 {
45 	isc_assertion_failed_cb(file, line, type, cond);
46 	abort();
47 	/* NOTREACHED */
48 }
49 
50 /*% Type to Text */
51 const char *
52 isc_assertion_typetotext(isc_assertiontype_t type) {
53 	const char *result;
54 
55 	/*
56 	 * These strings have purposefully not been internationalized
57 	 * because they are considered to essentially be keywords of
58 	 * the ISC development environment.
59 	 */
60 	switch (type) {
61 	case isc_assertiontype_require:
62 		result = "REQUIRE";
63 		break;
64 	case isc_assertiontype_ensure:
65 		result = "ENSURE";
66 		break;
67 	case isc_assertiontype_insist:
68 		result = "INSIST";
69 		break;
70 	case isc_assertiontype_invariant:
71 		result = "INVARIANT";
72 		break;
73 	default:
74 		result = NULL;
75 	}
76 	return (result);
77 }
78 
79 /*
80  * Private.
81  */
82 
83 static void
84 default_callback(const char *file, int line, isc_assertiontype_t type,
85 		 const char *cond)
86 {
87 	fprintf(stderr, "%s:%d: %s(%s) %s\n",
88 		file, line, isc_assertion_typetotext(type), cond, "failed");
89 	fflush(stderr);
90 }
91