Skip to content

Commit

Permalink
Add checks in case NIMBLE_CPP_DEBUG_ASSERT is not defined.
Browse files Browse the repository at this point in the history
  • Loading branch information
thekurtovic committed Jan 3, 2025
1 parent ad12a48 commit a53fd1a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
22 changes: 18 additions & 4 deletions src/NimBLEAttValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

# include "NimBLEAttValue.h"

static const char* LOG_TAG = "NimBLEAttValue";

// Default constructor implementation.
NimBLEAttValue::NimBLEAttValue(uint16_t init_len, uint16_t max_len)
: m_attr_value{static_cast<uint8_t*>(calloc(init_len + 1, 1))},
Expand All @@ -38,13 +40,17 @@ NimBLEAttValue::NimBLEAttValue(uint16_t init_len, uint16_t max_len)
# endif
{
NIMBLE_CPP_DEBUG_ASSERT(m_attr_value);
if (m_attr_value == nullptr) {
NIMBLE_LOGE(LOG_TAG, "Failed to calloc ctx");
}
}

// Value constructor implementation.
NimBLEAttValue::NimBLEAttValue(const uint8_t* value, uint16_t len, uint16_t max_len) : NimBLEAttValue(len, max_len) {
memcpy(m_attr_value, value, len);
m_attr_value[len] = '\0';
m_attr_len = len;
if (m_attr_value != nullptr) {
memcpy(m_attr_value, value, len);
m_attr_len = len;
}
}

// Destructor implementation.
Expand Down Expand Up @@ -81,6 +87,10 @@ NimBLEAttValue& NimBLEAttValue::operator=(const NimBLEAttValue& source) {
void NimBLEAttValue::deepCopy(const NimBLEAttValue& source) {
uint8_t* res = static_cast<uint8_t*>(realloc(m_attr_value, source.m_capacity + 1));
NIMBLE_CPP_DEBUG_ASSERT(res);
if (res == nullptr) {
NIMBLE_LOGE(LOG_TAG, "Failed to realloc deepCopy");
return;
}

ble_npl_hw_enter_critical();
m_attr_value = res;
Expand All @@ -106,7 +116,7 @@ NimBLEAttValue& NimBLEAttValue::append(const uint8_t* value, uint16_t 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;
}

Expand All @@ -117,6 +127,10 @@ NimBLEAttValue& NimBLEAttValue::append(const uint8_t* value, uint16_t len) {
m_capacity = new_len;
}
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
time_t t = time(nullptr);
Expand Down
4 changes: 4 additions & 0 deletions src/NimBLEAttValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ class NimBLEAttValue {
/** @brief Subscript operator */
uint8_t operator[](int pos) const {
NIMBLE_CPP_DEBUG_ASSERT(pos < m_attr_len);
if (pos >= m_attr_len) {
NIMBLE_LOGE("NimBLEAttValue", "pos >= len, pos=%u, len=%u", pos, m_attr_len);
return 0;
}
return m_attr_value[pos];
}

Expand Down
2 changes: 1 addition & 1 deletion src/NimBLEDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1231,7 +1231,7 @@ std::string NimBLEDevice::toString() {
* @param [in] line The line number where the assert occurred.
*/
void nimble_cpp_assert(const char* file, unsigned line) {
console_printf("Assertion failed at %s:%u\n", file, line);
console_printf(NIMBLE_LOG_FORMAT(E, "Assertion failed at %s:%u"), 0, "", file, line);
ble_npl_time_delay(10);
abort();
}
Expand Down

0 comments on commit a53fd1a

Please sign in to comment.