Coverage Report

Created: 2023-06-07 06:23

/src/bind9/lib/isc/assertions.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3
 *
4
 * SPDX-License-Identifier: MPL-2.0
5
 *
6
 * This Source Code Form is subject to the terms of the Mozilla Public
7
 * License, v. 2.0. If a copy of the MPL was not distributed with this
8
 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9
 *
10
 * See the COPYRIGHT file distributed with this work for additional
11
 * information regarding copyright ownership.
12
 */
13
14
/*! \file */
15
16
#include <stdio.h>
17
#include <stdlib.h>
18
19
#include <isc/assertions.h>
20
#include <isc/backtrace.h>
21
#include <isc/result.h>
22
#include <isc/strerr.h>
23
24
/*
25
 * The maximum number of stack frames to dump on assertion failure.
26
 */
27
#ifndef BACKTRACE_MAXFRAME
28
#define BACKTRACE_MAXFRAME 128
29
#endif /* ifndef BACKTRACE_MAXFRAME */
30
31
/*%
32
 * Forward.
33
 */
34
static void
35
default_callback(const char *, int, isc_assertiontype_t, const char *);
36
37
static isc_assertioncallback_t isc_assertion_failed_cb = default_callback;
38
39
/*%
40
 * Public.
41
 */
42
43
/*% assertion failed handler */
44
/* coverity[+kill] */
45
void
46
isc_assertion_failed(const char *file, int line, isc_assertiontype_t type,
47
0
         const char *cond) {
48
0
  isc_assertion_failed_cb(file, line, type, cond);
49
0
  abort();
50
0
}
51
52
/*% Set callback. */
53
void
54
0
isc_assertion_setcallback(isc_assertioncallback_t cb) {
55
0
  if (cb == NULL) {
56
0
    isc_assertion_failed_cb = default_callback;
57
0
  } else {
58
0
    isc_assertion_failed_cb = cb;
59
0
  }
60
0
}
61
62
/*% Type to Text */
63
const char *
64
0
isc_assertion_typetotext(isc_assertiontype_t type) {
65
0
  const char *result;
66
67
  /*
68
   * These strings have purposefully not been internationalized
69
   * because they are considered to essentially be keywords of
70
   * the ISC development environment.
71
   */
72
0
  switch (type) {
73
0
  case isc_assertiontype_require:
74
0
    result = "REQUIRE";
75
0
    break;
76
0
  case isc_assertiontype_ensure:
77
0
    result = "ENSURE";
78
0
    break;
79
0
  case isc_assertiontype_insist:
80
0
    result = "INSIST";
81
0
    break;
82
0
  case isc_assertiontype_invariant:
83
0
    result = "INVARIANT";
84
0
    break;
85
0
  default:
86
0
    result = "UNKNOWN";
87
0
  }
88
0
  return (result);
89
0
}
90
91
/*
92
 * Private.
93
 */
94
95
static void
96
default_callback(const char *file, int line, isc_assertiontype_t type,
97
0
     const char *cond) {
98
0
  void *tracebuf[ISC_BACKTRACE_MAXFRAME];
99
0
  int nframes = isc_backtrace(tracebuf, ISC_BACKTRACE_MAXFRAME);
100
101
0
  fprintf(stderr, "%s:%d: %s(%s) failed%s\n", file, line,
102
0
    isc_assertion_typetotext(type), cond,
103
0
    (nframes > 0) ? ", back trace" : ".");
104
105
0
  if (nframes > 0) {
106
0
    isc_backtrace_symbols_fd(tracebuf, nframes, fileno(stderr));
107
0
  }
108
109
0
  fflush(stderr);
110
0
}