Skip to content

Commit 9149a53

Browse files
chmorganh2zero
authored andcommitted
NimBLEAttValue.cpp - preserve capacity after realloc failure
NimBLEAttValue::append advanced m_capacity immediately after calling realloc. If realloc failed, the original buffer remained allocated but the object advertised a larger capacity than it actually owned. A later append could then skip reallocation and copy past the end of the allocation. Track whether the append actually needs growth and only update m_capacity after the realloc result passes the null check. Appends that fit the existing allocation leave capacity unchanged; failed growth preserves the old capacity invariant.
1 parent 657583a commit 9149a53

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

src/NimBLEAttValue.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,20 @@ NimBLEAttValue& NimBLEAttValue::append(const uint8_t* value, uint16_t len) {
124124

125125
uint8_t* res = m_attr_value;
126126
uint16_t new_len = m_attr_len + len;
127-
if (new_len > m_capacity) {
128-
res = static_cast<uint8_t*>(realloc(m_attr_value, (new_len + 1)));
129-
m_capacity = new_len;
127+
bool grow = new_len > m_capacity;
128+
if (grow) {
129+
res = static_cast<uint8_t*>(realloc(m_attr_value, (new_len + 1)));
130130
}
131131
NIMBLE_CPP_DEBUG_ASSERT(res);
132132
if (res == nullptr) {
133133
NIMBLE_LOGE(LOG_TAG, "Failed to realloc append");
134134
return *this;
135135
}
136136

137+
if (grow) {
138+
m_capacity = new_len;
139+
}
140+
137141
# if MYNEWT_VAL(NIMBLE_CPP_ATT_VALUE_TIMESTAMP_ENABLED)
138142
time_t t = time(nullptr);
139143
# else

0 commit comments

Comments
 (0)