Skip to content

Commit

Permalink
Guard against basic T::value_type recursion, fixes #90
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-rifkin committed Apr 3, 2024
1 parent 5ed5c8a commit 2d79db7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
6 changes: 5 additions & 1 deletion include/libassert/assert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,11 @@ namespace libassert::detail {
template<typename T> constexpr bool stringifiable_container() {
// TODO: Guard against std::expected....?
if constexpr(has_value_type<T>::value) {
return stringifiable<typename T::value_type>;
if constexpr(std::is_same_v<typename T::value_type, T>) {
return false;
} else {
return stringifiable<typename T::value_type>;
}
} else if constexpr(std::is_array_v<typename std::remove_reference_t<T>>) { // C arrays
return stringifiable<decltype(std::declval<T>()[0])>;
} else if constexpr(stringification::is_tuple_like<T>::value) {
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/stringify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,31 @@
using namespace libassert::detail;
using namespace std::literals;

struct basic_fields {
struct value_type {
value_type() {}
};
struct const_iterator {
bool operator==(const const_iterator&) const {
return true;
}
};
const_iterator begin() {
return {};
}
const_iterator end() {
return {};
}
};

void regression01() {
// regression test for #90, just making sure this can compile
constexpr bool b = stringifiable_container<basic_fields::value_type>();
ASSERT(!b);
basic_fields fields;
ASSERT(fields.begin() == fields.end());
}

struct S {};
struct S2 {};

Expand Down Expand Up @@ -124,4 +149,6 @@ int main() {
// libfmt
// std::format
// stringification tests

regression01();
}

0 comments on commit 2d79db7

Please sign in to comment.