Skip to content

Commit dd32365

Browse files
Copiloth2zero
andcommitted
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>
1 parent 8bb9578 commit dd32365

1 file changed

Lines changed: 24 additions & 1 deletion

File tree

src/NimBLEAttValue.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,23 @@ class NimBLEAttValue {
253253
/*********************** Template Functions ************************/
254254

255255
# if __cplusplus < 201703L
256+
/**
257+
* @brief Template to set value to the value of a char array using strnlen.
258+
* @param [in] s A reference to a char array.
259+
* @details Only used for char array types to correctly determine length via strnlen.
260+
*/
261+
template <typename T>
262+
# ifdef _DOXYGEN_
263+
bool
264+
# else
265+
typename std::enable_if<std::is_array<T>::value &&
266+
std::is_same<typename std::remove_extent<T>::type, char>::value,
267+
bool>::type
268+
# endif
269+
setValue(const T& s) {
270+
return setValue(reinterpret_cast<const uint8_t*>(s), strnlen(s, sizeof(T)));
271+
}
272+
256273
/**
257274
* @brief Template to set value to the value of <type\>val.
258275
* @param [in] v The <type\>value to set.
@@ -263,7 +280,10 @@ class NimBLEAttValue {
263280
# ifdef _DOXYGEN_
264281
bool
265282
# else
266-
typename std::enable_if<!std::is_pointer<T>::value && !Has_c_str_length<T>::value && !Has_data_size<T>::value, bool>::type
283+
typename std::enable_if<!std::is_pointer<T>::value && !Has_c_str_length<T>::value && !Has_data_size<T>::value &&
284+
!(std::is_array<T>::value &&
285+
std::is_same<typename std::remove_extent<T>::type, char>::value),
286+
bool>::type
267287
# endif
268288
setValue(const T& v) {
269289
return setValue(reinterpret_cast<const uint8_t*>(&v), sizeof(T));
@@ -334,6 +354,9 @@ class NimBLEAttValue {
334354
}
335355
} else if constexpr (Has_c_str_length<T>::value) {
336356
return setValue(reinterpret_cast<const uint8_t*>(s.c_str()), s.length());
357+
} else if constexpr (std::is_array<T>::value &&
358+
std::is_same<typename std::remove_extent<T>::type, char>::value) {
359+
return setValue(reinterpret_cast<const uint8_t*>(s), strnlen(s, sizeof(s)));
337360
} else {
338361
return setValue(reinterpret_cast<const uint8_t*>(&s), sizeof(s));
339362
}

0 commit comments

Comments
 (0)