Skip to content

Commit d44c808

Browse files
authored
Better ASSERT output from union_string.h (#39518)
Commit Message: Better ASSERT output from union_string.h Additional Description: The existing ASSERT just tells us "a validation went wrong somewhere". Sometimes the stack trace helps, but sometimes it doesn't. This change makes the ASSERT error give us a [somewhat mangled] description of what kind of validator was running, and the horrible string it was running on, e.g. ``` [./envoy/common/union_string.h:180] assert failure: valid(). Details: N5Envoy4Http21HeaderStringValidatorE failed to validate string "�2}���i���IS9䄒�i���X�?���i��������X�?�!ꨤI�W����" ``` vs. the old output ``` [./envoy/common/union_string.h:172] assert failure: valid(). ``` Risk Level: None, it only changes anything if an ASSERT fails. Testing: Used while debugging another issue. Docs Changes: n/a Release Notes: n/a Platform Specific Features: n/a --------- Signed-off-by: Raven Black <[email protected]>
1 parent bae0117 commit d44c808

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

envoy/common/union_string.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "source/common/common/utility.h"
77

88
#include "absl/container/inlined_vector.h"
9+
#include "absl/strings/str_cat.h"
910
#include "absl/strings/string_view.h"
1011
#include "absl/types/variant.h"
1112

@@ -60,15 +61,20 @@ template <class Validator> class UnionStringBase {
6061
*/
6162
UnionStringBase() : buffer_(std::in_place_type<InlinedStringVector>) {
6263
ASSERT((getInVec(buffer_).capacity()) >= MaxIntegerLength);
63-
ASSERT(valid());
64+
assertValid();
65+
}
66+
67+
inline void assertValid() const {
68+
ASSERT(valid(), absl::StrCat(typeid(Validator).name(), " failed to validate string \"",
69+
getStringView(), "\""));
6470
}
6571

6672
/**
6773
* Constructor for a string reference.
6874
* @param ref_value MUST point to data that will live beyond the lifetime of any request/response
6975
* using the string (since a codec may optimize for zero copy).
7076
*/
71-
explicit UnionStringBase(absl::string_view ref_value) : buffer_(ref_value) { ASSERT(valid()); }
77+
explicit UnionStringBase(absl::string_view ref_value) : buffer_(ref_value) { assertValid(); }
7278

7379
UnionStringBase(UnionStringBase&& move_value) noexcept : buffer_(std::move(move_value.buffer_)) {
7480
move_value.clear();
@@ -84,7 +90,9 @@ template <class Validator> class UnionStringBase {
8490
// Make sure the requested memory allocation is below uint32_t::max
8591
const uint64_t new_capacity = static_cast<uint64_t>(data_size) + size();
8692
validateCapacity(new_capacity);
87-
ASSERT(valid(absl::string_view(data, data_size)));
93+
ASSERT(valid(absl::string_view(data, data_size)),
94+
absl::StrCat(typeid(Validator).name(), " failed to validate string \"",
95+
absl::string_view(data, data_size), "\""));
8896

8997
switch (type()) {
9098
case Type::Reference: {
@@ -171,7 +179,7 @@ template <class Validator> class UnionStringBase {
171179

172180
getInVec(buffer_).reserve(size);
173181
getInVec(buffer_).assign(data, data + size);
174-
ASSERT(valid());
182+
assertValid();
175183
}
176184

177185
/**
@@ -207,7 +215,7 @@ template <class Validator> class UnionStringBase {
207215
*/
208216
void setReference(absl::string_view ref_value) {
209217
buffer_ = ref_value;
210-
ASSERT(valid());
218+
assertValid();
211219
}
212220

213221
/**

0 commit comments

Comments
 (0)