Skip to content

Commit ea6ea66

Browse files
authored
Merge pull request #839 from ApexAI/iox-#838-fix-gcc-11-warnings
iox-#838 fix string warning in strnlen and std::memcpy
2 parents f99e0c3 + a077f8c commit ea6ea66

File tree

6 files changed

+38
-20
lines changed

6 files changed

+38
-20
lines changed

iceoryx_binding_c/test/moduletests/test_publisher.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,14 @@ TEST(iox_pub_options_test, publisherOptionsInitializationCheckReturnsTrueAfterDe
363363
TEST(iox_pub_options_test, publisherOptionsInitializationCheckReturnsFalseWithoutDefaultInit)
364364
{
365365
iox_pub_options_t sut;
366+
#if (defined(__GNUC__) && __GNUC__ >= 7 && !defined(__clang__))
367+
#pragma GCC diagnostic push
368+
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
369+
#endif
366370
EXPECT_FALSE(iox_pub_options_is_initialized(&sut));
371+
#if (defined(__GNUC__) && __GNUC__ >= 7 && !defined(__clang__))
372+
#pragma GCC diagnostic pop
373+
#endif
367374
}
368375

369376
TEST(iox_pub_options_test, publisherOptionInitializationWithNullptrDoesNotCrash)

iceoryx_binding_c/test/moduletests/test_subscriber.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,14 @@ TEST(iox_sub_options_test, subscriberOptionsInitializationCheckReturnsTrueAfterD
435435
TEST(iox_sub_options_test, subscriberOptionsInitializationCheckReturnsFalseWithoutDefaultInit)
436436
{
437437
iox_sub_options_t sut;
438+
#if (defined(__GNUC__) && __GNUC__ >= 7 && !defined(__clang__))
439+
#pragma GCC diagnostic push
440+
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
441+
#endif
438442
EXPECT_FALSE(iox_sub_options_is_initialized(&sut));
443+
#if (defined(__GNUC__) && __GNUC__ >= 7 && !defined(__clang__))
444+
#pragma GCC diagnostic pop
445+
#endif
439446
}
440447

441448
TEST(iox_sub_options_test, subscriberOptionInitializationWithNullptrDoesNotCrash)

iceoryx_hoofs/include/iceoryx_hoofs/cxx/convert.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,4 @@ class convert
9696

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

99-
#endif // IOX_HOOFS_CXX_CONVERT_HPP
99+
#endif // IOX_HOOFS_CXX_CONVERT_HPP

iceoryx_hoofs/include/iceoryx_hoofs/internal/cxx/string.inl

+17-16
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ inline string<Capacity>::string(const char (&other)[N]) noexcept
9898

9999
template <uint64_t Capacity>
100100
inline string<Capacity>::string(TruncateToCapacity_t, const char* const other) noexcept
101-
: string(TruncateToCapacity, other, strnlen(other, Capacity + 1U))
101+
: string(TruncateToCapacity, other, strnlen(other, Capacity))
102102
{
103103
}
104104

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

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

@@ -466,14 +466,18 @@ inline typename std::enable_if<internal::IsCharArray<T>::value || internal::IsCx
466466
string<Capacity>::unsafe_append(const T& t) noexcept
467467
{
468468
uint64_t tSize = internal::GetSize<T>::call(t);
469-
if (Capacity < (m_rawstringSize + tSize))
469+
const char* tData = internal::GetData<T>::call(t);
470+
uint64_t clampedTSize = std::min(Capacity - m_rawstringSize, tSize);
471+
472+
if (tSize > clampedTSize)
470473
{
471474
std::cerr << "Appending failed because the sum of sizes exceeds this' capacity." << std::endl;
472475
return false;
473476
}
474-
std::memcpy(&(m_rawstring[0]) + m_rawstringSize, internal::GetData<T>::call(t), tSize);
475-
m_rawstring[m_rawstringSize + tSize] = '\0';
476-
m_rawstringSize += tSize;
477+
478+
std::memcpy(&(m_rawstring[m_rawstringSize]), tData, clampedTSize);
479+
m_rawstringSize += clampedTSize;
480+
m_rawstring[m_rawstringSize] = '\0';
477481
return true;
478482
}
479483

@@ -485,20 +489,17 @@ inline
485489
{
486490
uint64_t tSize = internal::GetSize<T>::call(t);
487491
const char* tData = internal::GetData<T>::call(t);
488-
if (Capacity < (m_rawstringSize + tSize))
492+
uint64_t clampedTSize = std::min(Capacity - m_rawstringSize, tSize);
493+
494+
std::memcpy(&(m_rawstring[m_rawstringSize]), tData, clampedTSize);
495+
if (tSize > clampedTSize)
489496
{
490497
std::cerr << "The last " << tSize - Capacity + m_rawstringSize << " characters of " << tData
491498
<< " are truncated, because the length is larger than the capacity." << std::endl;
492-
std::memcpy(&(m_rawstring[0]) + m_rawstringSize, tData, Capacity - m_rawstringSize);
493-
m_rawstring[Capacity] = '\0';
494-
m_rawstringSize = Capacity;
495-
}
496-
else
497-
{
498-
std::memcpy(&(m_rawstring[0]) + m_rawstringSize, tData, tSize);
499-
m_rawstring[m_rawstringSize + tSize] = '\0';
500-
m_rawstringSize += tSize;
501499
}
500+
501+
m_rawstringSize += clampedTSize;
502+
m_rawstring[m_rawstringSize] = '\0';
502503
return *this;
503504
}
504505

iceoryx_hoofs/test/moduletests/test_cxx_string.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
2+
// Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
23
//
34
// Licensed under the Apache License, Version 2.0 (the "License");
45
// you may not use this file except in compliance with the License.
@@ -251,7 +252,8 @@ TYPED_TEST(stringTyped_test, UnsafeCharToStringConvConstrWithSizeCapaResultsInSi
251252
{
252253
using MyString = typename TestFixture::stringType;
253254
constexpr auto STRINGCAP = MyString().capacity();
254-
char testChar[STRINGCAP];
255+
// increase capacity by one to circumvent gcc -Werror=array-bounds
256+
char testChar[STRINGCAP + 1];
255257
for (uint64_t i = 0U; i < STRINGCAP - 1U; i++)
256258
{
257259
testChar[i] = 'M';

iceoryx_posh/source/version/version_info.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright (c) 2020 by Robert Bosch GmbH. All rights reserved.
2+
// Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
23
//
34
// Licensed under the Apache License, Version 2.0 (the "License");
45
// you may not use this file except in compliance with the License.
@@ -50,8 +51,8 @@ VersionInfo::VersionInfo(const cxx::Serialization& serial) noexcept
5051
/// @brief Serialization of the VersionInfo.
5152
VersionInfo::operator cxx::Serialization() const noexcept
5253
{
53-
SerializationString_t tmp_m_buildDateString(cxx::TruncateToCapacity, m_buildDateString.c_str());
54-
SerializationString_t tmp_commitIdString(cxx::TruncateToCapacity, m_commitIdString.c_str());
54+
SerializationString_t tmp_m_buildDateString = m_buildDateString;
55+
SerializationString_t tmp_commitIdString = m_commitIdString;
5556
return cxx::Serialization::create(
5657
m_versionMajor, m_versionMinor, m_versionPatch, m_versionTweak, tmp_m_buildDateString, tmp_commitIdString);
5758
}

0 commit comments

Comments
 (0)