Skip to content

Commit

Permalink
iox-eclipse-iceoryx#838 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 authored and dkroenke committed Jun 15, 2021
1 parent 48a58fb commit 4a41101
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 19 deletions.
7 changes: 7 additions & 0 deletions iceoryx_binding_c/test/moduletests/test_publisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,14 @@ TEST(iox_pub_options_test, publisherOptionsInitializationCheckReturnsTrueAfterDe
TEST(iox_pub_options_test, publisherOptionsInitializationCheckReturnsFalseWithoutDefaultInit)
{
iox_pub_options_t sut;
#if (defined(__GNUC__) && __GNUC__ >= 7 && !defined(__clang__))
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif
EXPECT_FALSE(iox_pub_options_is_initialized(&sut));
#if (defined(__GNUC__) && __GNUC__ >= 7 && !defined(__clang__))
#pragma GCC diagnostic pop
#endif
}

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

TEST(iox_sub_options_test, subscriberOptionInitializationWithNullptrDoesNotCrash)
Expand Down
5 changes: 3 additions & 2 deletions iceoryx_posh/source/version/version_info.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) 2020 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -50,8 +51,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
33 changes: 17 additions & 16 deletions iceoryx_utils/include/iceoryx_utils/internal/cxx/string.inl
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,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 @@ -143,7 +143,7 @@ inline string<Capacity>& string<Capacity>::operator=(const char (&rhs)[N]) noexc
return *this;
}

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

Expand Down Expand Up @@ -465,14 +465,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 @@ -484,20 +488,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
4 changes: 3 additions & 1 deletion iceoryx_utils/test/moduletests/test_cxx_string.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -251,7 +252,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

0 comments on commit 4a41101

Please sign in to comment.