xref: /openbsd-src/usr.bin/dig/lib/isc/assertions.c (revision b9558d14c675017cf470d7469a47201d05e39444)
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.5 2020/02/25 05:00:43 jsg Exp $ */
18 
19 /*! \file */
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 
24 #include <isc/assertions.h>
25 
26 /*%
27  * Forward.
28  */
29 static void
30 default_callback(const char *, int, isc_assertiontype_t, const char *);
31 
32 static isc_assertioncallback_t isc_assertion_failed_cb = default_callback;
33 
34 /*%
35  * Public.
36  */
37 
38 /*% assertion failed handler */
39 void
isc_assertion_failed(const char * file,int line,isc_assertiontype_t type,const char * cond)40 isc_assertion_failed(const char *file, int line, isc_assertiontype_t type,
41 		     const char *cond)
42 {
43 	isc_assertion_failed_cb(file, line, type, cond);
44 	abort();
45 	/* NOTREACHED */
46 }
47 
48 /*% Type to Text */
49 const char *
isc_assertion_typetotext(isc_assertiontype_t type)50 isc_assertion_typetotext(isc_assertiontype_t type) {
51 	const char *result;
52 
53 	/*
54 	 * These strings have purposefully not been internationalized
55 	 * because they are considered to essentially be keywords of
56 	 * the ISC development environment.
57 	 */
58 	switch (type) {
59 	case isc_assertiontype_require:
60 		result = "REQUIRE";
61 		break;
62 	case isc_assertiontype_ensure:
63 		result = "ENSURE";
64 		break;
65 	case isc_assertiontype_insist:
66 		result = "INSIST";
67 		break;
68 	case isc_assertiontype_invariant:
69 		result = "INVARIANT";
70 		break;
71 	default:
72 		result = NULL;
73 	}
74 	return (result);
75 }
76 
77 /*
78  * Private.
79  */
80 
81 static void
default_callback(const char * file,int line,isc_assertiontype_t type,const char * cond)82 default_callback(const char *file, int line, isc_assertiontype_t type,
83 		 const char *cond)
84 {
85 	fprintf(stderr, "%s:%d: %s(%s) %s\n",
86 		file, line, isc_assertion_typetotext(type), cond, "failed");
87 	fflush(stderr);
88 }
89