Skip to content

Commit

Permalink
Fix recursion check in range formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Mar 4, 2023
1 parent b94e101 commit e0748e6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
15 changes: 8 additions & 7 deletions include/fmt/ranges.h
Original file line number Diff line number Diff line change
Expand Up @@ -505,13 +505,14 @@ struct range_formatter<
enum class range_format { disabled, map, set, sequence, string, debug_string };

namespace detail {
template <typename T> struct range_format_kind_ {
static constexpr auto value = std::is_same<range_reference_type<T>, T>::value
? range_format::disabled
: is_map<T>::value ? range_format::map
: is_set<T>::value ? range_format::set
: range_format::sequence;
};
template <typename T>
struct range_format_kind_
: std::integral_constant<range_format,
std::is_same<uncvref_type<T>, T>::value
? range_format::disabled
: is_map<T>::value ? range_format::map
: is_set<T>::value ? range_format::set
: range_format::sequence> {};

template <range_format K, typename R, typename Char, typename Enable = void>
struct range_default_formatter;
Expand Down
13 changes: 9 additions & 4 deletions test/ranges-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,20 @@ TEST(ranges_test, format_to) {
EXPECT_STREQ(buf, "[1, 2, 3]");
}

struct path_like {
template <typename Char> struct path_like {
const path_like* begin() const;
const path_like* end() const;

operator std::string() const;
operator std::basic_string<Char>() const;
};

TEST(ranges_test, path_like) {
EXPECT_FALSE((fmt::is_range<path_like, char>::value));
TEST(ranges_test, disabled_range_formatting_of_path) {
// Range formatting of path is disabled because of infinite recursion
// (path element is itself a path).
EXPECT_EQ((fmt::range_format_kind<path_like<char>, char>::value),
fmt::range_format::disabled);
EXPECT_EQ((fmt::range_format_kind<path_like<wchar_t>, char>::value),
fmt::range_format::disabled);
}

// A range that provides non-const only begin()/end() to test fmt::join
Expand Down

0 comments on commit e0748e6

Please sign in to comment.