25
25
# endif
26
26
27
27
# include " NimBLEAttValue.h"
28
+ # include " NimBLELog.h"
29
+
30
+ static const char * LOG_TAG = " NimBLEAttValue" ;
28
31
29
32
// Default constructor implementation.
30
33
NimBLEAttValue::NimBLEAttValue (uint16_t init_len, uint16_t max_len)
@@ -38,13 +41,17 @@ NimBLEAttValue::NimBLEAttValue(uint16_t init_len, uint16_t max_len)
38
41
# endif
39
42
{
40
43
NIMBLE_CPP_DEBUG_ASSERT (m_attr_value);
44
+ if (m_attr_value == nullptr ) {
45
+ NIMBLE_LOGE (LOG_TAG, " Failed to calloc ctx" );
46
+ }
41
47
}
42
48
43
49
// Value constructor implementation.
44
50
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
+ }
48
55
}
49
56
50
57
// Destructor implementation.
@@ -81,6 +88,10 @@ NimBLEAttValue& NimBLEAttValue::operator=(const NimBLEAttValue& source) {
81
88
void NimBLEAttValue::deepCopy (const NimBLEAttValue& source) {
82
89
uint8_t * res = static_cast <uint8_t *>(realloc (m_attr_value, source.m_capacity + 1 ));
83
90
NIMBLE_CPP_DEBUG_ASSERT (res);
91
+ if (res == nullptr ) {
92
+ NIMBLE_LOGE (LOG_TAG, " Failed to realloc deepCopy" );
93
+ return ;
94
+ }
84
95
85
96
ble_npl_hw_enter_critical ();
86
97
m_attr_value = res;
@@ -106,7 +117,7 @@ NimBLEAttValue& NimBLEAttValue::append(const uint8_t* value, uint16_t len) {
106
117
}
107
118
108
119
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);
110
121
return *this ;
111
122
}
112
123
@@ -117,6 +128,10 @@ NimBLEAttValue& NimBLEAttValue::append(const uint8_t* value, uint16_t len) {
117
128
m_capacity = new_len;
118
129
}
119
130
NIMBLE_CPP_DEBUG_ASSERT (res);
131
+ if (res == nullptr ) {
132
+ NIMBLE_LOGE (LOG_TAG, " Failed to realloc append" );
133
+ return *this ;
134
+ }
120
135
121
136
# if CONFIG_NIMBLE_CPP_ATT_VALUE_TIMESTAMP_ENABLED
122
137
time_t t = time (nullptr );
@@ -135,4 +150,13 @@ NimBLEAttValue& NimBLEAttValue::append(const uint8_t* value, uint16_t len) {
135
150
return *this ;
136
151
}
137
152
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
+
138
162
#endif // CONFIG_BT_ENABLED
0 commit comments