diff --git a/src/shared/gatt-db.c b/src/shared/gatt-db.c
index b67bf89..d0e149d 100644
--- a/src/shared/gatt-db.c
+++ b/src/shared/gatt-db.c
* declaration. All characteristic definitions shall have a
* Characteristic Value declaration.
*/
- if (handle == UINT16_MAX)
+ if ((handle == UINT16_MAX) || (value_handle && handle &&
+ value_handle <= handle))
return NULL;
i = service_get_attribute_index(service, &handle, 1);
/* Update handle of characteristic value_handle if it has changed */
put_le16(value_handle, &value[1]);
- if (!(*chrc) || !(*chrc)->value)
+ if (!(*chrc)->value) {
+ free(*chrc);
+ *chrc = NULL;
return NULL;
+ }
if (memcmp((*chrc)->value, value, len))
memcpy((*chrc)->value, value, len);