[Bugfix] Clear attribute value when zero length value is written.

This commit is contained in:
h2zero
2025-04-23 08:00:38 -06:00
committed by Ryan Powell
parent 9ac9551a2b
commit 54ec3f4c92
2 changed files with 5 additions and 4 deletions

View File

@ -106,6 +106,7 @@ void NimBLEAttValue::deepCopy(const NimBLEAttValue& source) {
// Set the value of the attribute.
bool NimBLEAttValue::setValue(const uint8_t* value, uint16_t len) {
m_attr_len = 0; // Just set the value length to 0 and append instead of repeating code.
m_attr_value[0] = '\0'; // Set the first byte to 0 incase the len of the new value is 0.
append(value, len);
return memcmp(m_attr_value, value, len) == 0 && m_attr_len == len;
}

View File

@ -635,12 +635,12 @@ int NimBLEServer::handleGattEvent(uint16_t connHandle, uint16_t attrHandle, ble_
case BLE_GATT_ACCESS_OP_WRITE_DSC:
case BLE_GATT_ACCESS_OP_WRITE_CHR: {
uint16_t maxLen = val.max_size();
if (ctxt->om->om_len > maxLen) {
uint16_t len = ctxt->om->om_len;
if (len > maxLen) {
return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
}
uint8_t buf[maxLen];
uint16_t len = ctxt->om->om_len;
memcpy(buf, ctxt->om->om_data, len);
os_mbuf* next;