Skip to content

Commit

Permalink
Fix capacity tracking.
Browse files Browse the repository at this point in the history
  • Loading branch information
h2zero committed Dec 5, 2021
1 parent 0a6224c commit d261a38
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/NimBLEAttValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ inline bool NimBLEAttValue::setValue(const uint8_t *value, uint16_t len) {
uint8_t *res = m_attr_value;
if (len > m_capacity) {
res = (uint8_t*)realloc(m_attr_value, (len + 1));
m_capacity = len;
}
assert(res && "setValue: realloc failed");

Expand Down Expand Up @@ -229,8 +230,10 @@ inline NimBLEAttValue& NimBLEAttValue::append(const uint8_t *value, uint16_t len
}

uint8_t* res = m_attr_value;
if ((m_attr_len + len) > m_capacity) {
res = (uint8_t*)realloc(m_attr_value, (m_attr_len + len + 1));
uint16_t new_len = m_attr_len + len;
if (new_len > m_capacity) {
res = (uint8_t*)realloc(m_attr_value, (new_len + 1));
m_capacity = new_len;
}
assert(res && "append: realloc failed");

Expand All @@ -243,7 +246,7 @@ inline NimBLEAttValue& NimBLEAttValue::append(const uint8_t *value, uint16_t len
ble_npl_hw_enter_critical();
m_attr_value = res;
memcpy(m_attr_value + m_attr_len, value, len);
m_attr_len = m_attr_len + len;
m_attr_len = new_len;
m_attr_value[m_attr_len] = '\0';
setTimeStamp(t);
ble_npl_hw_exit_critical(0);
Expand Down

0 comments on commit d261a38

Please sign in to comment.