2525# endif
2626
2727# include " NimBLEAttValue.h"
28+ # include " NimBLELog.h"
29+
30+ static const char * LOG_TAG = " NimBLEAttValue" ;
2831
2932// Default constructor implementation.
3033NimBLEAttValue::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.
4450NimBLEAttValue::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) {
8188void 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
0 commit comments