diff --git a/src/shared/gatt-db.c b/src/shared/gatt-db.c
index 2515e5a..30fa4df 100644
--- a/src/shared/gatt-db.c
+++ b/src/shared/gatt-db.c
{
struct gatt_db_service *service;
- if (!db || (num_handles + db->next_handle) > UINT16_MAX)
+ if (!db || (num_handles + db->next_handle - 1) > UINT16_MAX)
return NULL;
service = gatt_db_service_create(uuid, primary, num_handles);
struct insert_loc_data data;
struct gatt_db_service *service;
- if (!db || num_handles < 1 || handle + num_handles > UINT16_MAX)
+ if (!db || num_handles < 1 || (handle + num_handles - 1) > UINT16_MAX)
return NULL;
memset(&data, 0, sizeof(data));
service->num_handles = num_handles;
/* Fast-forward next_handle if the new service was added to the end */
- db->next_handle = MAX(handle + num_handles + 1, db->next_handle);
+ db->next_handle = MAX(handle + num_handles, db->next_handle);
return service->attributes[0];