From 8b1f217ef6e6a61d2ed77f43807d9ed4e1e303cb Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Mon, 18 Aug 2025 11:29:33 -0700 Subject: [PATCH] [toolchain] suppress gcc string operation warning (#11810) Introduces macros to suppress a known false-positive GCC warning "-Wstringop-overflow=0" which can be triggered when manipulating network data. The `AddHasRoute()`, `AddBorderRouter()`, and `AddServer()` methods shift and update the network data bytes, which may involve inserting or updating a sub-TLV within an existing TLV. This can trigger a "writing x byte into a region of size 0" error on some GCC toolchains. This change adds the `OT_SUPPRESS_GCC_STRING_OP_BEGIN` and `OT_SUPPRESS_GCC_STRING_OP_END` macros to silence this specific warning within these code blocks. --- include/openthread/instance.h | 2 +- include/openthread/platform/toolchain.h | 17 +++++++++++++++++ src/core/thread/network_data_leader_ftd.cpp | 11 +++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/include/openthread/instance.h b/include/openthread/instance.h index 8a84368b6..9bb574fe9 100644 --- a/include/openthread/instance.h +++ b/include/openthread/instance.h @@ -52,7 +52,7 @@ extern "C" { * * @note This number versions both OpenThread platform and user APIs. */ -#define OPENTHREAD_API_VERSION (526) +#define OPENTHREAD_API_VERSION (527) /** * @addtogroup api-instance diff --git a/include/openthread/platform/toolchain.h b/include/openthread/platform/toolchain.h index 2b92ec347..0084f9f92 100644 --- a/include/openthread/platform/toolchain.h +++ b/include/openthread/platform/toolchain.h @@ -293,6 +293,23 @@ extern "C" { } while (false) /* fallthrough */ #endif +// A known false positive warning occurs on some GCC toolchains, +// resulting in "error: writing x byte into a region of size 0". The following +// macros are used to suppress this warning/error in specific code blocks. + +#if defined(__GNUC__) && (__GNUC__ >= 7) + +#define OT_SUPPRESS_GCC_STRING_OP_BEGIN \ + _Pragma("GCC diagnostic push") _Pragma("GCC diagnostic warning \"-Wstringop-overflow=0\"") +#define OT_SUPPRESS_GCC_STRING_OP_END _Pragma("GCC diagnostic pop") + +#else + +#define OT_SUPPRESS_GCC_STRING_OP_BEGIN +#define OT_SUPPRESS_GCC_STRING_OP_END + +#endif + /** * @} */ diff --git a/src/core/thread/network_data_leader_ftd.cpp b/src/core/thread/network_data_leader_ftd.cpp index 6d69e6276..8a6cfa869 100644 --- a/src/core/thread/network_data_leader_ftd.cpp +++ b/src/core/thread/network_data_leader_ftd.cpp @@ -835,6 +835,15 @@ exit: return error; } +// The `AddHasRoute()`, `AddBorderRouter()`, and `AddServer()` methods +// below shift and update the network data bytes, which may involve +// inserting or updating a sub-TLV within an existing TLV. This can +// trigger a known false positive warning on some GCC toolchains. The +// `OT_SUPPRESS_GCC_STRING_OP_BEGIN`/ `OT_SUPPRESS_GCC_STRING_OP_END` +// macros are used to silence this check within these methods. + +OT_SUPPRESS_GCC_STRING_OP_BEGIN + Error Leader::AddHasRoute(const HasRouteTlv &aHasRoute, PrefixTlv &aDstPrefix, ChangedFlags &aChangedFlags) { Error error = kErrorNone; @@ -969,6 +978,8 @@ exit: return error; } +OT_SUPPRESS_GCC_STRING_OP_END + Error Leader::AllocateServiceId(uint8_t &aServiceId) const { Error error = kErrorNotFound;