Coverage Report

Created: 2026-12-01 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/bind9/lib/isc/assertions.c
Line
Count
Source
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
void
45
isc_assertion_failed(const char *file, int line, isc_assertiontype_t type,
46
0
         const char *cond) {
47
0
  isc_assertion_failed_cb(file, line, type, cond);
48
0
  abort();
49
0
}
50
51
/*% Set callback. */
52
void
53
0
isc_assertion_setcallback(isc_assertioncallback_t cb) {
54
0
  if (cb == NULL) {
55
0
    isc_assertion_failed_cb = default_callback;
56
0
  } else {
57
0
    isc_assertion_failed_cb = cb;
58
0
  }
59
0
}
60
61
/*% Type to Text */
62
const char *
63
0
isc_assertion_typetotext(isc_assertiontype_t type) {
64
0
  const char *result;
65
66
  /*
67
   * These strings have purposefully not been internationalized
68
   * because they are considered to essentially be keywords of
69
   * the ISC development environment.
70
   */
71
0
  switch (type) {
72
0
  case isc_assertiontype_require:
73
0
    result = "REQUIRE";
74
0
    break;
75
0
  case isc_assertiontype_ensure:
76
0
    result = "ENSURE";
77
0
    break;
78
0
  case isc_assertiontype_insist:
79
0
    result = "INSIST";
80
0
    break;
81
0
  case isc_assertiontype_invariant:
82
0
    result = "INVARIANT";
83
0
    break;
84
0
  default:
85
0
    result = "UNKNOWN";
86
0
  }
87
0
  return result;
88
0
}
89
90
/*
91
 * Private.
92
 */
93
94
static void
95
default_callback(const char *file, int line, isc_assertiontype_t type,
96
0
     const char *cond) {
97
0
  void *tracebuf[ISC_BACKTRACE_MAXFRAME];
98
0
  int nframes = isc_backtrace(tracebuf, ISC_BACKTRACE_MAXFRAME);
99
100
0
  fprintf(stderr, "%s:%d: %s(%s) failed%s\n", file, line,
101
0
    isc_assertion_typetotext(type), cond,
102
0
    (nframes > 0) ? ", back trace" : ".");
103
104
0
  if (nframes > 0) {
105
0
    isc_backtrace_symbols_fd(tracebuf, nframes, fileno(stderr));
106
0
  }
107
108
  fflush(stderr);
109
0
}