Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a8da047

Browse files
committed
Add checks in case NIMBLE_CPP_DEBUG_ASSERT is not defined.
1 parent ad12a48 commit a8da047

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

src/NimBLEAttValue.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
# include "NimBLEAttValue.h"
2828

29+
static const char* LOG_TAG = "NimBLEAttValue";
30+
2931
// Default constructor implementation.
3032
NimBLEAttValue::NimBLEAttValue(uint16_t init_len, uint16_t max_len)
3133
: m_attr_value{static_cast<uint8_t*>(calloc(init_len + 1, 1))},
@@ -38,6 +40,9 @@ NimBLEAttValue::NimBLEAttValue(uint16_t init_len, uint16_t max_len)
3840
# endif
3941
{
4042
NIMBLE_CPP_DEBUG_ASSERT(m_attr_value);
43+
if (m_attr_value == nullptr) {
44+
NIMBLE_LOGE(LOG_TAG, "Failed to calloc ctx");
45+
}
4146
}
4247

4348
// Value constructor implementation.
@@ -57,6 +62,10 @@ NimBLEAttValue::~NimBLEAttValue() {
5762
// Move assignment operator implementation.
5863
NimBLEAttValue& NimBLEAttValue::operator=(NimBLEAttValue&& source) {
5964
if (this != &source) {
65+
if (m_attr_value == nullptr) {
66+
NIMBLE_LOGE(LOG_TAG, "Move assign nullptr");
67+
return *this;
68+
}
6069
free(m_attr_value);
6170
m_attr_value = source.m_attr_value;
6271
m_attr_max_len = source.m_attr_max_len;
@@ -81,6 +90,10 @@ NimBLEAttValue& NimBLEAttValue::operator=(const NimBLEAttValue& source) {
8190
void NimBLEAttValue::deepCopy(const NimBLEAttValue& source) {
8291
uint8_t* res = static_cast<uint8_t*>(realloc(m_attr_value, source.m_capacity + 1));
8392
NIMBLE_CPP_DEBUG_ASSERT(res);
93+
if (res == nullptr) {
94+
NIMBLE_LOGE(LOG_TAG, "Failed to realloc deepCopy");
95+
return;
96+
}
8497

8598
ble_npl_hw_enter_critical();
8699
m_attr_value = res;
@@ -106,7 +119,7 @@ NimBLEAttValue& NimBLEAttValue::append(const uint8_t* value, uint16_t len) {
106119
}
107120

108121
if ((m_attr_len + len) > m_attr_max_len) {
109-
NIMBLE_LOGE("NimBLEAttValue", "val > max, len=%u, max=%u", len, m_attr_max_len);
122+
NIMBLE_LOGE(LOG_TAG, "val > max, len=%u, max=%u", len, m_attr_max_len);
110123
return *this;
111124
}
112125

@@ -117,6 +130,10 @@ NimBLEAttValue& NimBLEAttValue::append(const uint8_t* value, uint16_t len) {
117130
m_capacity = new_len;
118131
}
119132
NIMBLE_CPP_DEBUG_ASSERT(res);
133+
if (res == nullptr) {
134+
NIMBLE_LOGE(LOG_TAG, "Failed to realloc append");
135+
return *this;
136+
}
120137

121138
# if CONFIG_NIMBLE_CPP_ATT_VALUE_TIMESTAMP_ENABLED
122139
time_t t = time(nullptr);

src/NimBLEAttValue.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,10 @@ class NimBLEAttValue {
325325
/** @brief Subscript operator */
326326
uint8_t operator[](int pos) const {
327327
NIMBLE_CPP_DEBUG_ASSERT(pos < m_attr_len);
328+
if (pos >= m_attr_len) {
329+
NIMBLE_LOGE("NimBLEAttValue", "pos >= len, pos=%u, len=%u", pos, m_attr_len);
330+
return 0;
331+
}
328332
return m_attr_value[pos];
329333
}
330334

src/NimBLEDevice.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1231,7 +1231,7 @@ std::string NimBLEDevice::toString() {
12311231
* @param [in] line The line number where the assert occurred.
12321232
*/
12331233
void nimble_cpp_assert(const char* file, unsigned line) {
1234-
console_printf("Assertion failed at %s:%u\n", file, line);
1234+
NIMBLE_LOGE("", "Assertion failed at %s:%u\n", file, line);
12351235
ble_npl_time_delay(10);
12361236
abort();
12371237
}

0 commit comments

Comments
 (0)