diff --git a/iceoryx_binding_c/test/moduletests/test_publisher.cpp b/iceoryx_binding_c/test/moduletests/test_publisher.cpp index c4bf4d5dd5..db28439415 100644 --- a/iceoryx_binding_c/test/moduletests/test_publisher.cpp +++ b/iceoryx_binding_c/test/moduletests/test_publisher.cpp @@ -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) diff --git a/iceoryx_binding_c/test/moduletests/test_subscriber.cpp b/iceoryx_binding_c/test/moduletests/test_subscriber.cpp index 2f311b0cbd..a9b72e6d5c 100644 --- a/iceoryx_binding_c/test/moduletests/test_subscriber.cpp +++ b/iceoryx_binding_c/test/moduletests/test_subscriber.cpp @@ -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) diff --git a/iceoryx_posh/source/version/version_info.cpp b/iceoryx_posh/source/version/version_info.cpp index f4f005c250..52af4d61f6 100644 --- a/iceoryx_posh/source/version/version_info.cpp +++ b/iceoryx_posh/source/version/version_info.cpp @@ -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. @@ -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); } diff --git a/iceoryx_utils/include/iceoryx_utils/internal/cxx/string.inl b/iceoryx_utils/include/iceoryx_utils/internal/cxx/string.inl index 9bdf141a8b..a48e523b56 100755 --- a/iceoryx_utils/include/iceoryx_utils/internal/cxx/string.inl +++ b/iceoryx_utils/include/iceoryx_utils/internal/cxx/string.inl @@ -97,7 +97,7 @@ inline string::string(const char (&other)[N]) noexcept template inline string::string(TruncateToCapacity_t, const char* const other) noexcept - : string(TruncateToCapacity, other, strnlen(other, Capacity + 1U)) + : string(TruncateToCapacity, other, strnlen(other, Capacity)) { } @@ -143,7 +143,7 @@ inline string& string::operator=(const char (&rhs)[N]) noexc return *this; } - m_rawstringSize = strnlen(rhs, Capacity); + m_rawstringSize = std::min(Capacity, static_cast(strnlen(rhs, N))); std::memcpy(&(m_rawstring[0]), rhs, m_rawstringSize); m_rawstring[m_rawstringSize] = '\0'; @@ -465,14 +465,18 @@ inline typename std::enable_if::value || internal::IsCx string::unsafe_append(const T& t) noexcept { uint64_t tSize = internal::GetSize::call(t); - if (Capacity < (m_rawstringSize + tSize)) + const char* tData = internal::GetData::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::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; } @@ -484,20 +488,17 @@ inline { uint64_t tSize = internal::GetSize::call(t); const char* tData = internal::GetData::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; } diff --git a/iceoryx_utils/test/moduletests/test_cxx_string.cpp b/iceoryx_utils/test/moduletests/test_cxx_string.cpp index d1ec6b4ced..03a7dcbf39 100644 --- a/iceoryx_utils/test/moduletests/test_cxx_string.cpp +++ b/iceoryx_utils/test/moduletests/test_cxx_string.cpp @@ -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. @@ -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';