Coverage Report

Created: 2026-05-23 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/abseil-cpp/absl/base/internal/hardening.h
Line
Count
Source
1
//
2
// Copyright 2026 The Abseil Authors.
3
//
4
// Licensed under the Apache License, Version 2.0 (the "License");
5
// you may not use this file except in compliance with the License.
6
// You may obtain a copy of the License at
7
//
8
//      https://www.apache.org/licenses/LICENSE-2.0
9
//
10
// Unless required by applicable law or agreed to in writing, software
11
// distributed under the License is distributed on an "AS IS" BASIS,
12
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
// See the License for the specific language governing permissions and
14
// limitations under the License.
15
//
16
// -----------------------------------------------------------------------------
17
// File: hardening.h
18
// -----------------------------------------------------------------------------
19
//
20
// This header file defines macros and functions for performing Abseil
21
// hardening checks and aborts.
22
23
#ifndef ABSL_BASE_INTERNAL_HARDENING_H_
24
#define ABSL_BASE_INTERNAL_HARDENING_H_
25
26
#include "absl/base/config.h"
27
#include "absl/base/macros.h"
28
#include "absl/base/options.h"
29
30
namespace absl {
31
ABSL_NAMESPACE_BEGIN
32
33
namespace base_internal {
34
35
// `HardeningAssert` performs runtime checks when Abseil Hardening is enabled,
36
// even if `NDEBUG` is defined.
37
//
38
// When `NDEBUG` is not defined, `HardeningAssert`'s behavior is identical to
39
// `ABSL_ASSERT`.
40
//
41
// Prefer a more specific assertion function over this more general one,
42
// as assertion functions which perform the comparison themselves
43
// can have the cost of the comparison attributed to them.
44
0
constexpr void HardeningAssert(bool cond) {
45
0
  ABSL_ASSERT(cond);
46
0
#if (ABSL_OPTION_HARDENED == 1 || ABSL_OPTION_HARDENED == 2) && defined(NDEBUG)
47
0
  if (ABSL_PREDICT_FALSE(!cond)) {
48
0
    base_internal::HardeningAbort();
49
0
  }
50
0
#endif
51
0
}
52
53
// `HardeningAssertSlow` is used to perform runtime checks which are too
54
// computationally expensive to enable widely by default.
55
//
56
// When `NDEBUG` is not defined, `HardeningAssertSlow`'s behavior is identical
57
// to `ABSL_ASSERT`.
58
0
constexpr void HardeningAssertSlow(bool cond) {
59
0
  ABSL_ASSERT(cond);
60
0
#if (ABSL_OPTION_HARDENED == 1) && defined(NDEBUG)
61
0
  if (ABSL_PREDICT_FALSE(!cond)) {
62
0
    base_internal::HardeningAbort();
63
0
  }
64
0
#endif
65
0
}
66
67
template <typename T>
68
0
constexpr void HardeningAssertGT(T val1, T val2) {
69
0
  ABSL_ASSERT(val1 > val2);
70
0
#if (ABSL_OPTION_HARDENED == 1 || ABSL_OPTION_HARDENED == 2) && defined(NDEBUG)
71
0
  if (!ABSL_PREDICT_TRUE(val1 > val2)) {
72
0
    base_internal::HardeningAbort();
73
0
  }
74
0
#endif
75
0
}
76
77
template <typename T>
78
0
constexpr void HardeningAssertGE(T val1, T val2) {
79
0
  ABSL_ASSERT(val1 >= val2);
80
#if (ABSL_OPTION_HARDENED == 1 || ABSL_OPTION_HARDENED == 2) && defined(NDEBUG)
81
  if (!ABSL_PREDICT_TRUE(val1 >= val2)) {
82
    base_internal::HardeningAbort();
83
  }
84
#endif
85
0
}
86
87
template <typename T>
88
996k
constexpr void HardeningAssertLT(T val1, T val2) {
89
996k
  ABSL_ASSERT(val1 < val2);
90
#if (ABSL_OPTION_HARDENED == 1 || ABSL_OPTION_HARDENED == 2) && defined(NDEBUG)
91
  if (!ABSL_PREDICT_TRUE(val1 < val2)) {
92
    base_internal::HardeningAbort();
93
  }
94
#endif
95
996k
}
96
97
template <typename T>
98
0
constexpr void HardeningAssertLE(T val1, T val2) {
99
0
  ABSL_ASSERT(val1 <= val2);
100
#if (ABSL_OPTION_HARDENED == 1 || ABSL_OPTION_HARDENED == 2) && defined(NDEBUG)
101
  if (!ABSL_PREDICT_TRUE(val1 <= val2)) {
102
    base_internal::HardeningAbort();
103
  }
104
#endif
105
0
}
106
107
0
constexpr void HardeningAssertInBounds(size_t index, size_t size) {
108
0
  HardeningAssertLT(index, size);
109
0
}
110
111
template <typename T>
112
constexpr void HardeningAssertNonEmpty(const T& container) {
113
  ABSL_ASSERT(!container.empty());
114
#if (ABSL_OPTION_HARDENED == 1 || ABSL_OPTION_HARDENED == 2) && defined(NDEBUG)
115
  if (ABSL_PREDICT_FALSE(container.empty())) {
116
    base_internal::HardeningAbort();
117
  }
118
#endif
119
}
120
121
template <typename T>
122
constexpr void HardeningAssertNonNull(T ptr) {
123
  ABSL_ASSERT(ptr != nullptr);
124
#if (ABSL_OPTION_HARDENED == 1 || ABSL_OPTION_HARDENED == 2) && defined(NDEBUG)
125
  if (ABSL_PREDICT_FALSE(ptr == nullptr)) {
126
    base_internal::HardeningAbort();
127
  }
128
#endif
129
}
130
131
}  // namespace base_internal
132
133
ABSL_NAMESPACE_END
134
}  // namespace absl
135
136
#endif  // ABSL_BASE_INTERNAL_HARDENING_H_