Coverage Report

Created: 2025-07-11 06:40

/src/varnish-cache/include/vas.h
Line
Count
Source (jump to first uncovered line)
1
/*-
2
 * Copyright (c) 2006 Verdens Gang AS
3
 * Copyright (c) 2006-2011 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
 * assert(), AN() and AZ() are static checks that should not happen.
32
 *  In general asserts should be cheap, such as checking return
33
 *  values and similar.
34
 * diagnostic() are asserts which are so expensive that we may want
35
 *  to compile them out for performance at a later date.
36
 * xxxassert(), XXXAN() and XXXAZ() marks conditions we ought to
37
 *  handle gracefully, such as malloc failure.
38
 */
39
40
#ifndef VAS_H_INCLUDED
41
#define VAS_H_INCLUDED
42
43
44
#include <errno.h>
45
#include <stddef.h> // size_t
46
47
const char * VAS_errtxt(int e);
48
49
enum vas_e {
50
  VAS_WRONG,
51
  VAS_MISSING,
52
  VAS_ASSERT,
53
  VAS_INCOMPLETE,
54
  VAS_VCL,
55
};
56
57
typedef void vas_f(const char *, const char *, int, const char *, enum vas_e);
58
59
extern vas_f *VAS_Fail_Func v_noreturn_;
60
extern vas_f VAS_Fail v_noreturn_;
61
62
#ifdef WITHOUT_ASSERTS
63
#define assert(e) ((void)(e))
64
#else /* WITH_ASSERTS */
65
59.1M
#define assert(e)             \
66
59.1M
do {                 \
67
59.1M
  if (!(e)) {             \
68
0
    VAS_Fail(__func__, __FILE__, __LINE__,      \
69
0
        #e, VAS_ASSERT);          \
70
0
  }                \
71
59.1M
} while (0)
72
#endif
73
74
#define xxxassert(e)              \
75
do {                  \
76
  if (!(e)) {             \
77
    VAS_Fail(__func__, __FILE__, __LINE__,      \
78
        #e, VAS_MISSING);         \
79
  }               \
80
} while (0)
81
82
/* Assert zero return value */
83
13.0k
#define AZ(foo)   do { assert((foo) == 0); } while (0)
84
11.8M
#define AN(foo)   do { assert((foo) != 0); } while (0)
85
#define XXXAZ(foo)  do { xxxassert((foo) == 0); } while (0)
86
#define XXXAN(foo)  do { xxxassert((foo) != 0); } while (0)
87
#define diagnostic(foo) assert(foo)
88
0
#define WRONG(expl)             \
89
0
do {                 \
90
0
  VAS_Fail(__func__, __FILE__, __LINE__, expl, VAS_WRONG);  \
91
0
} while (0)
92
93
#define _PTOK(call, var)            \
94
do {                  \
95
  int var = call;             \
96
  if (!var)             \
97
    break;              \
98
  errno = var;              \
99
  WRONG(#call " failed");           \
100
} while (0)
101
102
#define PTOK(call) _PTOK(call, VUNIQ_NAME(_pterr))
103
104
#define INCOMPL()             \
105
do {                  \
106
  VAS_Fail(__func__, __FILE__, __LINE__,        \
107
      "", VAS_INCOMPLETE);          \
108
} while (0)
109
110
/*
111
 * Most of this nightmare is stolen from FreeBSD's <cdefs.h>
112
 */
113
#ifndef __has_extension
114
#  define __has_extension(x)  0
115
#endif
116
117
#if __STDC_VERSION__ - 0 >= 202311L
118
#   define v_static_assert static_assert
119
#elif __STDC_VERSION__ - 0 >= 201112L
120
#   define v_static_assert _Static_assert
121
#elif __has_extension(c_static_assert)
122
#   define v_static_assert _Static_assert
123
#elif __GNUC_PREREQ__(4,6) && !defined(__cplusplus)
124
#   define v_static_assert _Static_assert
125
#else
126
#   if defined(__COUNTER__)
127
# define v_static_assert(x, y)  __v_static_assert(x, __COUNTER__)
128
#   else
129
# define v_static_assert(x, y)  __v_static_assert(x, __LINE__)
130
#   endif
131
#   define __v_static_assert(x, y)  ___v_static_assert(x, y)
132
#   define ___v_static_assert(x, y) \
133
    typedef char __vassert_## y[(x) ? 1 : -1] v_unused_
134
#endif
135
136
/*
137
 * A normal pointer difference is signed, but when we don't want a negative
138
 * value this little tool will make sure we don't get that.
139
 */
140
141
static inline size_t
142
pdiff(const void *b, const void *e)
143
4.59k
{
144
145
4.59k
  AN(b);
146
4.59k
  AN(e);
147
4.59k
  assert(b <= e);
148
4.59k
  return ((size_t)((const char *)e - (const char *)b));
149
4.59k
}
cache_ws_emu.c:pdiff
Line
Count
Source
143
4.59k
{
144
145
4.59k
  AN(b);
146
4.59k
  AN(e);
147
4.59k
  assert(b <= e);
148
4.59k
  return ((size_t)((const char *)e - (const char *)b));
149
4.59k
}
Unexecuted instantiation: cache_ws_common.c:pdiff
Unexecuted instantiation: cache_esi_parse.c:pdiff
Unexecuted instantiation: esi_parse_fuzzer.c:pdiff
Unexecuted instantiation: vas.c:pdiff
Unexecuted instantiation: vbt.c:pdiff
Unexecuted instantiation: vct.c:pdiff
Unexecuted instantiation: vsb.c:pdiff
150
151
#endif