Coverage Report

Created: 2025-07-11 06:40

/src/varnish-cache/lib/libvarnish/vas.c
Line
Count
Source (jump to first uncovered line)
1
/*-
2
 * Copyright (c) 2006 Verdens Gang AS
3
 * Copyright (c) 2006-2016 Varnish Software AS
4
 * All rights reserved.
5
 *
6
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
7
 *
8
 * SPDX-License-Identifier: BSD-2-Clause
9
 *
10
 * Redistribution and use in source and binary forms, with or without
11
 * modification, are permitted provided that the following conditions
12
 * are met:
13
 * 1. Redistributions of source code must retain the above copyright
14
 *    notice, this list of conditions and the following disclaimer.
15
 * 2. Redistributions in binary form must reproduce the above copyright
16
 *    notice, this list of conditions and the following disclaimer in the
17
 *    documentation and/or other materials provided with the distribution.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
23
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29
 * SUCH DAMAGE.
30
 *
31
 * This is the default backend function for libvarnish' assert facilities.
32
 */
33
34
#include "config.h"
35
36
#include <stdio.h>
37
#include <stdlib.h>
38
#include <string.h>
39
#include <syslog.h>
40
41
#include "vdef.h"
42
#include "vbt.h"
43
44
#include "vas.h"
45
46
const char *
47
VAS_errtxt(int e)
48
0
{
49
0
  const char *p;
50
0
  int oerrno = errno;
51
52
0
  p = strerror(e);
53
0
  if (p != NULL)
54
0
    return (p);
55
56
0
  errno = oerrno;
57
0
  return ("strerror(3) returned NULL");
58
0
}
59
60
vas_f *VAS_Fail_Func v_noreturn_;
61
62
static void
63
vas_default(const char *func, const char *file, int line,
64
    const char *cond, enum vas_e kind)
65
0
{
66
0
  int err = errno;
67
0
  char buf[4096];
68
69
0
  if (kind == VAS_MISSING) {
70
0
    fprintf(stderr,
71
0
        "Missing error handling code in %s(), %s line %d:\n"
72
0
        "  Condition(%s) not true.\n",
73
0
        func, file, line, cond);
74
0
  } else if (kind == VAS_INCOMPLETE) {
75
0
    fprintf(stderr,
76
0
        "Incomplete code in %s(), %s line %d:\n",
77
0
        func, file, line);
78
0
  } else if (kind == VAS_WRONG) {
79
0
    fprintf(stderr,
80
0
        "Wrong turn in %s(), %s line %d: %s\n",
81
0
        func, file, line, cond);
82
0
  } else {
83
0
    fprintf(stderr,
84
0
        "Assert error in %s(), %s line %d:\n"
85
0
        "  Condition(%s) not true.\n",
86
0
        func, file, line, cond);
87
0
  }
88
0
  if (err) {
89
0
    fprintf(stderr,
90
0
        "  errno = %d (%s)\n", err, strerror(err));
91
0
  }
92
93
0
  if (VBT_dump(sizeof buf, buf) < 0) {
94
0
    bprintf(buf, "Failed to print backtrace: %d (%s)",
95
0
        errno, strerror(errno));
96
0
  }
97
98
0
  syslog(LOG_DEBUG, "%s", buf);
99
0
}
100
101
void v_noreturn_
102
VAS_Fail(const char *func, const char *file, int line,
103
    const char *cond, enum vas_e kind)
104
0
{
105
106
0
  if (VAS_Fail_Func != NULL)
107
0
    VAS_Fail_Func(func, file, line, cond, kind);
108
0
  else
109
0
    vas_default(func, file, line, cond, kind);
110
0
  abort();
111
0
}