Add checks in case NIMBLE_CPP_DEBUG_ASSERT is not defined.

This commit is contained in:
thekurtovic 2025-01-03 13:49:29 -05:00 committed by Ryan Powell
parent aae70dfd21
commit 15cb6bc7c6
3 changed files with 30 additions and 9 deletions

View file

@ -25,6 +25,9 @@
# endif # endif
# include "NimBLEAttValue.h" # include "NimBLEAttValue.h"
# include "NimBLELog.h"
static const char* LOG_TAG = "NimBLEAttValue";
// Default constructor implementation. // Default constructor implementation.
NimBLEAttValue::NimBLEAttValue(uint16_t init_len, uint16_t max_len) NimBLEAttValue::NimBLEAttValue(uint16_t init_len, uint16_t max_len)
@ -38,13 +41,17 @@ NimBLEAttValue::NimBLEAttValue(uint16_t init_len, uint16_t max_len)
# endif # endif
{ {
NIMBLE_CPP_DEBUG_ASSERT(m_attr_value); NIMBLE_CPP_DEBUG_ASSERT(m_attr_value);
if (m_attr_value == nullptr) {
NIMBLE_LOGE(LOG_TAG, "Failed to calloc ctx");
}
} }
// Value constructor implementation. // Value constructor implementation.
NimBLEAttValue::NimBLEAttValue(const uint8_t* value, uint16_t len, uint16_t max_len) : NimBLEAttValue(len, max_len) { NimBLEAttValue::NimBLEAttValue(const uint8_t* value, uint16_t len, uint16_t max_len) : NimBLEAttValue(len, max_len) {
memcpy(m_attr_value, value, len); if (m_attr_value != nullptr) {
m_attr_value[len] = '\0'; memcpy(m_attr_value, value, len);
m_attr_len = len; m_attr_len = len;
}
} }
// Destructor implementation. // Destructor implementation.
@ -81,6 +88,10 @@ NimBLEAttValue& NimBLEAttValue::operator=(const NimBLEAttValue& source) {
void NimBLEAttValue::deepCopy(const NimBLEAttValue& source) { void NimBLEAttValue::deepCopy(const NimBLEAttValue& source) {
uint8_t* res = static_cast<uint8_t*>(realloc(m_attr_value, source.m_capacity + 1)); uint8_t* res = static_cast<uint8_t*>(realloc(m_attr_value, source.m_capacity + 1));
NIMBLE_CPP_DEBUG_ASSERT(res); NIMBLE_CPP_DEBUG_ASSERT(res);
if (res == nullptr) {
NIMBLE_LOGE(LOG_TAG, "Failed to realloc deepCopy");
return;
}
ble_npl_hw_enter_critical(); ble_npl_hw_enter_critical();
m_attr_value = res; m_attr_value = res;
@ -106,7 +117,7 @@ NimBLEAttValue& NimBLEAttValue::append(const uint8_t* value, uint16_t len) {
} }
if ((m_attr_len + len) > m_attr_max_len) { if ((m_attr_len + len) > m_attr_max_len) {
NIMBLE_LOGE("NimBLEAttValue", "val > max, len=%u, max=%u", len, m_attr_max_len); NIMBLE_LOGE(LOG_TAG, "val > max, len=%u, max=%u", len, m_attr_max_len);
return *this; return *this;
} }
@ -117,6 +128,10 @@ NimBLEAttValue& NimBLEAttValue::append(const uint8_t* value, uint16_t len) {
m_capacity = new_len; m_capacity = new_len;
} }
NIMBLE_CPP_DEBUG_ASSERT(res); NIMBLE_CPP_DEBUG_ASSERT(res);
if (res == nullptr) {
NIMBLE_LOGE(LOG_TAG, "Failed to realloc append");
return *this;
}
# if CONFIG_NIMBLE_CPP_ATT_VALUE_TIMESTAMP_ENABLED # if CONFIG_NIMBLE_CPP_ATT_VALUE_TIMESTAMP_ENABLED
time_t t = time(nullptr); time_t t = time(nullptr);
@ -135,4 +150,13 @@ NimBLEAttValue& NimBLEAttValue::append(const uint8_t* value, uint16_t len) {
return *this; return *this;
} }
uint8_t NimBLEAttValue::operator[](int pos) const {
NIMBLE_CPP_DEBUG_ASSERT(pos < m_attr_len);
if (pos >= m_attr_len) {
NIMBLE_LOGE(LOG_TAG, "pos >= len, pos=%u, len=%u", pos, m_attr_len);
return 0;
}
return m_attr_value[pos];
}
#endif // CONFIG_BT_ENABLED #endif // CONFIG_BT_ENABLED

View file

@ -24,7 +24,6 @@
# include <Arduino.h> # include <Arduino.h>
# endif # endif
# include "NimBLELog.h"
# include <string> # include <string>
# include <vector> # include <vector>
# include <ctime> # include <ctime>
@ -323,10 +322,7 @@ class NimBLEAttValue {
/*********************** Operators ************************/ /*********************** Operators ************************/
/** @brief Subscript operator */ /** @brief Subscript operator */
uint8_t operator[](int pos) const { uint8_t operator[](int pos) const;
NIMBLE_CPP_DEBUG_ASSERT(pos < m_attr_len);
return m_attr_value[pos];
}
/** @brief Operator; Get the value as a std::vector<uint8_t>. */ /** @brief Operator; Get the value as a std::vector<uint8_t>. */
operator std::vector<uint8_t>() const { return std::vector<uint8_t>(m_attr_value, m_attr_value + m_attr_len); } operator std::vector<uint8_t>() const { return std::vector<uint8_t>(m_attr_value, m_attr_value + m_attr_len); }

View file

@ -21,6 +21,7 @@
# include "NimBLERemoteValueAttribute.h" # include "NimBLERemoteValueAttribute.h"
# include "NimBLEClient.h" # include "NimBLEClient.h"
# include "NimBLEUtils.h" # include "NimBLEUtils.h"
# include "NimBLELog.h"
# include <climits> # include <climits>