Skip to content

Commit 15cb6bc

Browse files
thekurtovich2zero
authored andcommitted
Add checks in case NIMBLE_CPP_DEBUG_ASSERT is not defined.
1 parent aae70df commit 15cb6bc

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

src/NimBLEAttValue.cpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
# endif
2626

2727
# include "NimBLEAttValue.h"
28+
# include "NimBLELog.h"
29+
30+
static const char* LOG_TAG = "NimBLEAttValue";
2831

2932
// Default constructor implementation.
3033
NimBLEAttValue::NimBLEAttValue(uint16_t init_len, uint16_t max_len)
@@ -38,13 +41,17 @@ NimBLEAttValue::NimBLEAttValue(uint16_t init_len, uint16_t max_len)
3841
# endif
3942
{
4043
NIMBLE_CPP_DEBUG_ASSERT(m_attr_value);
44+
if (m_attr_value == nullptr) {
45+
NIMBLE_LOGE(LOG_TAG, "Failed to calloc ctx");
46+
}
4147
}
4248

4349
// Value constructor implementation.
4450
NimBLEAttValue::NimBLEAttValue(const uint8_t* value, uint16_t len, uint16_t max_len) : NimBLEAttValue(len, max_len) {
45-
memcpy(m_attr_value, value, len);
46-
m_attr_value[len] = '\0';
47-
m_attr_len = len;
51+
if (m_attr_value != nullptr) {
52+
memcpy(m_attr_value, value, len);
53+
m_attr_len = len;
54+
}
4855
}
4956

5057
// Destructor implementation.
@@ -81,6 +88,10 @@ NimBLEAttValue& NimBLEAttValue::operator=(const NimBLEAttValue& source) {
8188
void NimBLEAttValue::deepCopy(const NimBLEAttValue& source) {
8289
uint8_t* res = static_cast<uint8_t*>(realloc(m_attr_value, source.m_capacity + 1));
8390
NIMBLE_CPP_DEBUG_ASSERT(res);
91+
if (res == nullptr) {
92+
NIMBLE_LOGE(LOG_TAG, "Failed to realloc deepCopy");
93+
return;
94+
}
8495

8596
ble_npl_hw_enter_critical();
8697
m_attr_value = res;
@@ -106,7 +117,7 @@ NimBLEAttValue& NimBLEAttValue::append(const uint8_t* value, uint16_t len) {
106117
}
107118

108119
if ((m_attr_len + len) > m_attr_max_len) {
109-
NIMBLE_LOGE("NimBLEAttValue", "val > max, len=%u, max=%u", len, m_attr_max_len);
120+
NIMBLE_LOGE(LOG_TAG, "val > max, len=%u, max=%u", len, m_attr_max_len);
110121
return *this;
111122
}
112123

@@ -117,6 +128,10 @@ NimBLEAttValue& NimBLEAttValue::append(const uint8_t* value, uint16_t len) {
117128
m_capacity = new_len;
118129
}
119130
NIMBLE_CPP_DEBUG_ASSERT(res);
131+
if (res == nullptr) {
132+
NIMBLE_LOGE(LOG_TAG, "Failed to realloc append");
133+
return *this;
134+
}
120135

121136
# if CONFIG_NIMBLE_CPP_ATT_VALUE_TIMESTAMP_ENABLED
122137
time_t t = time(nullptr);
@@ -135,4 +150,13 @@ NimBLEAttValue& NimBLEAttValue::append(const uint8_t* value, uint16_t len) {
135150
return *this;
136151
}
137152

153+
uint8_t NimBLEAttValue::operator[](int pos) const {
154+
NIMBLE_CPP_DEBUG_ASSERT(pos < m_attr_len);
155+
if (pos >= m_attr_len) {
156+
NIMBLE_LOGE(LOG_TAG, "pos >= len, pos=%u, len=%u", pos, m_attr_len);
157+
return 0;
158+
}
159+
return m_attr_value[pos];
160+
}
161+
138162
#endif // CONFIG_BT_ENABLED

src/NimBLEAttValue.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
# include <Arduino.h>
2525
# endif
2626

27-
# include "NimBLELog.h"
2827
# include <string>
2928
# include <vector>
3029
# include <ctime>
@@ -323,10 +322,7 @@ class NimBLEAttValue {
323322
/*********************** Operators ************************/
324323

325324
/** @brief Subscript operator */
326-
uint8_t operator[](int pos) const {
327-
NIMBLE_CPP_DEBUG_ASSERT(pos < m_attr_len);
328-
return m_attr_value[pos];
329-
}
325+
uint8_t operator[](int pos) const;
330326

331327
/** @brief Operator; Get the value as a std::vector<uint8_t>. */
332328
operator std::vector<uint8_t>() const { return std::vector<uint8_t>(m_attr_value, m_attr_value + m_attr_len); }

src/NimBLERemoteValueAttribute.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
# include "NimBLERemoteValueAttribute.h"
2222
# include "NimBLEClient.h"
2323
# include "NimBLEUtils.h"
24+
# include "NimBLELog.h"
2425

2526
# include <climits>
2627

0 commit comments

Comments
 (0)