Skip to content

Commit

Permalink
iox-eclipse-iceoryx#838 fix string warning in strnlen and std::memcpy
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Eltzschig <me@elchris.org>
  • Loading branch information
elfenpiff committed Jun 8, 2021
1 parent f99e0c3 commit 849c842
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 20 deletions.
3 changes: 3 additions & 0 deletions iceoryx_binding_c/test/moduletests/test_publisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,10 @@ TEST(iox_pub_options_test, publisherOptionsInitializationCheckReturnsTrueAfterDe
TEST(iox_pub_options_test, publisherOptionsInitializationCheckReturnsFalseWithoutDefaultInit)
{
iox_pub_options_t sut;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
EXPECT_FALSE(iox_pub_options_is_initialized(&sut));
#pragma GCC diagnostic pop
}

TEST(iox_pub_options_test, publisherOptionInitializationWithNullptrDoesNotCrash)
Expand Down
3 changes: 3 additions & 0 deletions iceoryx_binding_c/test/moduletests/test_subscriber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,10 @@ TEST(iox_sub_options_test, subscriberOptionsInitializationCheckReturnsTrueAfterD
TEST(iox_sub_options_test, subscriberOptionsInitializationCheckReturnsFalseWithoutDefaultInit)
{
iox_sub_options_t sut;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
EXPECT_FALSE(iox_sub_options_is_initialized(&sut));
#pragma GCC diagnostic pop
}

TEST(iox_sub_options_test, subscriberOptionInitializationWithNullptrDoesNotCrash)
Expand Down
2 changes: 1 addition & 1 deletion iceoryx_hoofs/include/iceoryx_hoofs/cxx/convert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,4 @@ class convert

#include "iceoryx_hoofs/internal/cxx/convert.inl"

#endif // IOX_HOOFS_CXX_CONVERT_HPP
#endif // IOX_HOOFS_CXX_CONVERT_HPP
33 changes: 17 additions & 16 deletions iceoryx_hoofs/include/iceoryx_hoofs/internal/cxx/string.inl
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ inline string<Capacity>::string(const char (&other)[N]) noexcept

template <uint64_t Capacity>
inline string<Capacity>::string(TruncateToCapacity_t, const char* const other) noexcept
: string(TruncateToCapacity, other, strnlen(other, Capacity + 1U))
: string(TruncateToCapacity, other, strnlen(other, Capacity))
{
}

Expand Down Expand Up @@ -144,7 +144,7 @@ inline string<Capacity>& string<Capacity>::operator=(const char (&rhs)[N]) noexc
return *this;
}

m_rawstringSize = strnlen(rhs, Capacity);
m_rawstringSize = std::min(Capacity, strnlen(rhs, N));
std::memcpy(&(m_rawstring[0]), rhs, m_rawstringSize);
m_rawstring[m_rawstringSize] = '\0';

Expand Down Expand Up @@ -466,14 +466,18 @@ inline typename std::enable_if<internal::IsCharArray<T>::value || internal::IsCx
string<Capacity>::unsafe_append(const T& t) noexcept
{
uint64_t tSize = internal::GetSize<T>::call(t);
if (Capacity < (m_rawstringSize + tSize))
const char* tData = internal::GetData<T>::call(t);
uint64_t clampedTSize = std::min(Capacity - m_rawstringSize, tSize);

if (tSize > clampedTSize)
{
std::cerr << "Appending failed because the sum of sizes exceeds this' capacity." << std::endl;
return false;
}
std::memcpy(&(m_rawstring[0]) + m_rawstringSize, internal::GetData<T>::call(t), tSize);
m_rawstring[m_rawstringSize + tSize] = '\0';
m_rawstringSize += tSize;

std::memcpy(&(m_rawstring[m_rawstringSize]), tData, clampedTSize);
m_rawstringSize += clampedTSize;
m_rawstring[m_rawstringSize] = '\0';
return true;
}

Expand All @@ -485,20 +489,17 @@ inline
{
uint64_t tSize = internal::GetSize<T>::call(t);
const char* tData = internal::GetData<T>::call(t);
if (Capacity < (m_rawstringSize + tSize))
uint64_t clampedTSize = std::min(Capacity - m_rawstringSize, tSize);

std::memcpy(&(m_rawstring[m_rawstringSize]), tData, clampedTSize);
if (tSize > clampedTSize)
{
std::cerr << "The last " << tSize - Capacity + m_rawstringSize << " characters of " << tData
<< " are truncated, because the length is larger than the capacity." << std::endl;
std::memcpy(&(m_rawstring[0]) + m_rawstringSize, tData, Capacity - m_rawstringSize);
m_rawstring[Capacity] = '\0';
m_rawstringSize = Capacity;
}
else
{
std::memcpy(&(m_rawstring[0]) + m_rawstringSize, tData, tSize);
m_rawstring[m_rawstringSize + tSize] = '\0';
m_rawstringSize += tSize;
}

m_rawstringSize += clampedTSize;
m_rawstring[m_rawstringSize] = '\0';
return *this;
}

Expand Down
3 changes: 2 additions & 1 deletion iceoryx_hoofs/test/moduletests/test_cxx_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ TYPED_TEST(stringTyped_test, UnsafeCharToStringConvConstrWithSizeCapaResultsInSi
{
using MyString = typename TestFixture::stringType;
constexpr auto STRINGCAP = MyString().capacity();
char testChar[STRINGCAP];
// increase capacity by one to circumvent gcc -Werror=array-bounds
char testChar[STRINGCAP + 1];
for (uint64_t i = 0U; i < STRINGCAP - 1U; i++)
{
testChar[i] = 'M';
Expand Down
4 changes: 2 additions & 2 deletions iceoryx_posh/source/version/version_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ VersionInfo::VersionInfo(const cxx::Serialization& serial) noexcept
/// @brief Serialization of the VersionInfo.
VersionInfo::operator cxx::Serialization() const noexcept
{
SerializationString_t tmp_m_buildDateString(cxx::TruncateToCapacity, m_buildDateString.c_str());
SerializationString_t tmp_commitIdString(cxx::TruncateToCapacity, m_commitIdString.c_str());
SerializationString_t tmp_m_buildDateString = m_buildDateString;
SerializationString_t tmp_commitIdString = m_commitIdString;
return cxx::Serialization::create(
m_versionMajor, m_versionMinor, m_versionPatch, m_versionTweak, tmp_m_buildDateString, tmp_commitIdString);
}
Expand Down

0 comments on commit 849c842

Please sign in to comment.