From 1646d274ad765022372434ed8f16fb5e94154969 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 29 Mar 2026 22:24:30 +0000 Subject: [PATCH] Fix setValue() using sizeof instead of strlen for char array arguments Agent-Logs-Url: https://github.com/h2zero/NimBLE-Arduino/sessions/42ace199-2049-4c37-8f0a-30443045b8b2 Co-authored-by: h2zero <32826625+h2zero@users.noreply.github.com> --- src/NimBLEAttValue.h | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/NimBLEAttValue.h b/src/NimBLEAttValue.h index 56ceae46..9ef92d52 100644 --- a/src/NimBLEAttValue.h +++ b/src/NimBLEAttValue.h @@ -253,6 +253,23 @@ class NimBLEAttValue { /*********************** Template Functions ************************/ # if __cplusplus < 201703L + /** + * @brief Template to set value to the value of a char array using strnlen. + * @param [in] s A reference to a char array. + * @details Only used for char array types to correctly determine length via strnlen. + */ + template +# ifdef _DOXYGEN_ + bool +# else + typename std::enable_if::value && + std::is_same::type, char>::value, + bool>::type +# endif + setValue(const T& s) { + return setValue(reinterpret_cast(s), strnlen(s, sizeof(T))); + } + /** * @brief Template to set value to the value of val. * @param [in] v The value to set. @@ -263,7 +280,10 @@ class NimBLEAttValue { # ifdef _DOXYGEN_ bool # else - typename std::enable_if::value && !Has_c_str_length::value && !Has_data_size::value, bool>::type + typename std::enable_if::value && !Has_c_str_length::value && !Has_data_size::value && + !(std::is_array::value && + std::is_same::type, char>::value), + bool>::type # endif setValue(const T& v) { return setValue(reinterpret_cast(&v), sizeof(T)); @@ -334,6 +354,9 @@ class NimBLEAttValue { } } else if constexpr (Has_c_str_length::value) { return setValue(reinterpret_cast(s.c_str()), s.length()); + } else if constexpr (std::is_array::value && + std::is_same::type, char>::value) { + return setValue(reinterpret_cast(s), strnlen(s, sizeof(s))); } else { return setValue(reinterpret_cast(&s), sizeof(s)); }