Disallow constructing sf::String from std::nullptr_t

This commit is contained in:
Chris Thrasher 2024-06-30 22:26:41 -06:00
parent 705aa7e891
commit 27943ea774
2 changed files with 97 additions and 33 deletions

View File

@ -110,6 +110,14 @@ public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
String() = default; String() = default;
////////////////////////////////////////////////////////////
/// \brief Deleted nullptr constructor
///
/// Disallow construction from nullptr literal
///
////////////////////////////////////////////////////////////
String(std::nullptr_t, const std::locale& = {}) = delete;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Construct from a single ANSI character and a locale /// \brief Construct from a single ANSI character and a locale
/// ///

View File

@ -200,6 +200,8 @@ TEST_CASE("[System] sf::String")
SECTION("Type traits") SECTION("Type traits")
{ {
STATIC_CHECK(!std::is_constructible_v<sf::String, std::nullptr_t>);
STATIC_CHECK(!std::is_constructible_v<sf::String, std::nullptr_t, const std::locale&>);
STATIC_CHECK(std::is_copy_constructible_v<sf::String>); STATIC_CHECK(std::is_copy_constructible_v<sf::String>);
STATIC_CHECK(std::is_copy_assignable_v<sf::String>); STATIC_CHECK(std::is_copy_assignable_v<sf::String>);
STATIC_CHECK(std::is_nothrow_move_constructible_v<sf::String>); STATIC_CHECK(std::is_nothrow_move_constructible_v<sf::String>);
@ -239,6 +241,23 @@ TEST_CASE("[System] sf::String")
} }
SECTION("ANSI C string constructor") SECTION("ANSI C string constructor")
{
SECTION("Nullptr")
{
const sf::String string = static_cast<char*>(nullptr);
CHECK(std::string(string).empty());
CHECK(std::wstring(string).empty());
CHECK(string.toAnsiString().empty());
CHECK(string.toWideString().empty());
CHECK(string.toUtf8().empty());
CHECK(string.toUtf16().empty());
CHECK(string.toUtf32().empty());
CHECK(string.getSize() == 0);
CHECK(string.isEmpty());
CHECK(string.getData() != nullptr);
}
SECTION("Non-empty string")
{ {
const sf::String string = "def"; const sf::String string = "def";
CHECK(std::string(string) == "def"s); CHECK(std::string(string) == "def"s);
@ -252,6 +271,7 @@ TEST_CASE("[System] sf::String")
CHECK(!string.isEmpty()); CHECK(!string.isEmpty());
CHECK(string.getData() != nullptr); CHECK(string.getData() != nullptr);
} }
}
SECTION("ANSI string constructor") SECTION("ANSI string constructor")
{ {
@ -284,6 +304,23 @@ TEST_CASE("[System] sf::String")
} }
SECTION("Wide C string constructor") SECTION("Wide C string constructor")
{
SECTION("Nullptr")
{
const sf::String string = static_cast<wchar_t*>(nullptr);
CHECK(std::string(string).empty());
CHECK(std::wstring(string).empty());
CHECK(string.toAnsiString().empty());
CHECK(string.toWideString().empty());
CHECK(string.toUtf8().empty());
CHECK(string.toUtf16().empty());
CHECK(string.toUtf32().empty());
CHECK(string.getSize() == 0);
CHECK(string.isEmpty());
CHECK(string.getData() != nullptr);
}
SECTION("Non-empty string")
{ {
const sf::String string = L"j\xFAl"; const sf::String string = L"j\xFAl";
CHECK(std::string(string) == select("j\xFAl"s, "j\0l"s)); CHECK(std::string(string) == select("j\xFAl"s, "j\0l"s));
@ -297,6 +334,7 @@ TEST_CASE("[System] sf::String")
CHECK(!string.isEmpty()); CHECK(!string.isEmpty());
CHECK(string.getData() != nullptr); CHECK(string.getData() != nullptr);
} }
}
SECTION("Wide string constructor") SECTION("Wide string constructor")
{ {
@ -329,6 +367,23 @@ TEST_CASE("[System] sf::String")
} }
SECTION("UTF-32 C string constructor") SECTION("UTF-32 C string constructor")
{
SECTION("Nullptr")
{
const sf::String string = static_cast<char32_t*>(nullptr);
CHECK(std::string(string).empty());
CHECK(std::wstring(string).empty());
CHECK(string.toAnsiString().empty());
CHECK(string.toWideString().empty());
CHECK(string.toUtf8().empty());
CHECK(string.toUtf16().empty());
CHECK(string.toUtf32().empty());
CHECK(string.getSize() == 0);
CHECK(string.isEmpty());
CHECK(string.getData() != nullptr);
}
SECTION("Non-empty string")
{ {
const sf::String string = U"\U0010ABCDrs"; const sf::String string = U"\U0010ABCDrs";
CHECK(std::string(string) == "\0rs"s); CHECK(std::string(string) == "\0rs"s);
@ -342,6 +397,7 @@ TEST_CASE("[System] sf::String")
CHECK(!string.isEmpty()); CHECK(!string.isEmpty());
CHECK(string.getData() != nullptr); CHECK(string.getData() != nullptr);
} }
}
SECTION("UTF-32 string constructor") SECTION("UTF-32 string constructor")
{ {