Skip to content

Commit

Permalink
Replace toString(uint8_t) with template to avoid unintentional trun…
Browse files Browse the repository at this point in the history
…cation of values (#2229)

e.g. uint32_t -> uint8_t gets truncated without warning
  • Loading branch information
mikee47 committed Feb 25, 2021
1 parent 7d66eb9 commit 90908fe
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Sming/Core/Data/BitSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,10 @@ constexpr
return a | b;
}

String toString(uint8_t value);
template <typename T> typename std::enable_if<std::is_integral<T>::value, String>::type toString(T value)
{
return String(value);
}

/**
* @brief Class template to print the contents of a BitSet to a String
Expand Down
7 changes: 7 additions & 0 deletions tests/HostTests/modules/BitSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ class BitSetTest : public TestGroup
REQUIRE(sizeof(large) == 8);
REQUIRE(large.domain().value() == 0x7FFFFFFFFULL);
}

TEST_CASE("toString")
{
REQUIRE(toString(12) == "12");
REQUIRE(toString(12345678) == "12345678");
REQUIRE(toString(12345678912345ULL) == "12345678912345");
}
}
};

Expand Down

0 comments on commit 90908fe

Please sign in to comment.