mirror of
https://github.com/OpenMW/openmw.git
synced 2025-01-30 08:45:33 +00:00
Fix gcc warning: array subscript 5 is outside array bounds of ‘const char [5]’
In function ‘bool ESM::operator==(const FixedString<capacity>&, const T* const&) [with long unsigned int capacity = 5; T = char; <template-parameter-1-3> = void]’, inlined from ‘testing::AssertionResult testing::internal::CmpHelperEQ(const char*, const char*, const T1&, const T2&) [with T1 = ESM::FixedString<5>; T2 = const char*]’ at /home/elsid/dev/googletest/build/gcc/release/install/include/gtest/gtest.h:1358:11, inlined from ‘static testing::AssertionResult testing::internal::EqHelper::Compare(const char*, const char*, const T1&, const T2&) [with T1 = ESM::FixedString<5>; T2 = const char*; typename std::enable_if<((! std::is_integral<_Tp>::value) || (! std::is_pointer<_Dp>::value))>::type* <anonymous> = 0]’ at /home/elsid/dev/googletest/build/gcc/release/install/include/gtest/gtest.h:1377:64, inlined from ‘virtual void {anonymous}::EsmFixedString_equality_operator_for_not_convertible_to_uint32_with_const_char_pointer_Test::TestBody()’ at apps/openmw_test_suite/esm/test_fixed_string.cpp:165:9: components/esm/esmcommon.hpp:134:19: warning: array subscript 5 is outside array bounds of ‘const char [5]’ [-Warray-bounds] 134 | return rhs[capacity] == '\0'; | ~~~^ apps/openmw_test_suite/esm/test_fixed_string.cpp: In member function ‘virtual void {anonymous}::EsmFixedString_equality_operator_for_not_convertible_to_uint32_with_const_char_pointer_Test::TestBody()’: apps/openmw_test_suite/esm/test_fixed_string.cpp:164:20: note: at offset 5 into object ‘other’ of size 5 164 | const char other[5] = { 'a', 'b', 'c', 'd', '\0' }; | ^~~~~
This commit is contained in:
parent
c80ba92ab7
commit
2bbed8cc06
2 changed files with 28 additions and 7 deletions
|
@ -170,4 +170,19 @@ namespace
|
|||
const ESM::FixedString<5> value("abcd");
|
||||
EXPECT_EQ(value, std::string("abcd"));
|
||||
}
|
||||
|
||||
TEST(EsmFixedString, equality_operator_for_not_convertible_to_uint32_with_string_view)
|
||||
{
|
||||
const ESM::FixedString<5> value("abcd");
|
||||
const std::string other("abcd");
|
||||
EXPECT_EQ(value, std::string_view(other));
|
||||
}
|
||||
|
||||
TEST(EsmFixedString, equality_operator_should_not_get_out_of_bounds)
|
||||
{
|
||||
ESM::FixedString<5> value;
|
||||
const char other[5] = { 'a', 'b', 'c', 'd', 'e' };
|
||||
std::memcpy(value.mData, other, sizeof(other));
|
||||
EXPECT_EQ(value, static_cast<const char*>(other));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -121,23 +121,23 @@ namespace ESM
|
|||
}
|
||||
};
|
||||
|
||||
template <std::size_t capacity, class T, typename = std::enable_if_t<std::is_same_v<T, char>>>
|
||||
inline bool operator==(const FixedString<capacity>& lhs, const T* const& rhs) noexcept
|
||||
template <std::size_t capacity>
|
||||
inline bool operator==(const FixedString<capacity>& lhs, std::string_view rhs) noexcept
|
||||
{
|
||||
for (std::size_t i = 0; i < capacity; ++i)
|
||||
for (std::size_t i = 0, n = std::min(rhs.size(), capacity); i < n; ++i)
|
||||
{
|
||||
if (lhs.mData[i] != rhs[i])
|
||||
return false;
|
||||
if (lhs.mData[i] == '\0')
|
||||
return true;
|
||||
}
|
||||
return rhs[capacity] == '\0';
|
||||
return rhs.size() <= capacity || rhs[capacity] == '\0';
|
||||
}
|
||||
|
||||
template <std::size_t capacity>
|
||||
inline bool operator==(const FixedString<capacity>& lhs, const std::string& rhs) noexcept
|
||||
template <std::size_t capacity, class T, typename = std::enable_if_t<std::is_same_v<T, char>>>
|
||||
inline bool operator==(const FixedString<capacity>& lhs, const T* const& rhs) noexcept
|
||||
{
|
||||
return lhs == rhs.c_str();
|
||||
return lhs == std::string_view(rhs, capacity);
|
||||
}
|
||||
|
||||
template <std::size_t capacity, std::size_t rhsSize>
|
||||
|
@ -156,6 +156,12 @@ namespace ESM
|
|||
return lhs.toInt() == rhs.toInt();
|
||||
}
|
||||
|
||||
template <class T, typename = std::enable_if_t<std::is_same_v<T, char>>>
|
||||
inline bool operator==(const FixedString<4>& lhs, const T* const& rhs) noexcept
|
||||
{
|
||||
return lhs == std::string_view(rhs, 5);
|
||||
}
|
||||
|
||||
template <std::size_t capacity, class Rhs>
|
||||
inline bool operator!=(const FixedString<capacity>& lhs, const Rhs& rhs) noexcept
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue