Use calloc() instead of malloc() when allocating 'struct' or array in memory

pull/274/head
Olivier Martin 2024-03-28 14:26:22 +01:00
parent 5f43addb8f
commit db629448fd
7 changed files with 16 additions and 12 deletions

View File

@ -83,7 +83,7 @@ static void* _discovered_device_thread_args_allocator(va_list args) {
struct gattlib_adapter* gattlib_adapter = va_arg(args, struct gattlib_adapter*);
OrgBluezDevice1* device1 = va_arg(args, OrgBluezDevice1*);
struct gattlib_discovered_device_thread_args* thread_args = malloc(sizeof(struct gattlib_discovered_device_thread_args));
struct gattlib_discovered_device_thread_args* thread_args = calloc(sizeof(struct gattlib_discovered_device_thread_args), 1);
thread_args->gattlib_adapter = gattlib_adapter;
thread_args->mac_address = strdup(org_bluez_device1_get_address(device1));
const char* device_name = org_bluez_device1_get_name(device1);

View File

@ -76,9 +76,9 @@ void gattlib_notification_device_thread(gpointer data, gpointer user_data) {
}
static void* _notification_device_thread_args_allocator(gatt_connection_t* connection, const uuid_t* uuid, const uint8_t* data, size_t data_length) {
struct gattlib_notification_device_thread_args* thread_args = malloc(sizeof(struct gattlib_notification_device_thread_args));
struct gattlib_notification_device_thread_args* thread_args = calloc(sizeof(struct gattlib_notification_device_thread_args), 1);
thread_args->connection = connection;
thread_args->uuid = malloc(sizeof(uuid_t));
thread_args->uuid = calloc(sizeof(uuid_t), 1);
if (thread_args->uuid != NULL) {
memcpy(thread_args->uuid, uuid, sizeof(uuid_t));
}

View File

@ -11,7 +11,7 @@ void* gattlib_python_callback_args(PyObject* python_callback, PyObject* python_a
assert(python_callback != NULL);
assert(python_args != NULL);
struct gattlib_python_args* args = malloc(sizeof(struct gattlib_python_args));
struct gattlib_python_args* args = calloc(sizeof(struct gattlib_python_args), 1);
if (args == NULL) {
GATTLIB_LOG(GATTLIB_ERROR, "Failed to allocate Python arguments for Python callback.");
return NULL;

View File

@ -156,8 +156,12 @@ void gattlib_handler_free(struct gattlib_handler* handler) {
#if defined(WITH_PYTHON)
if (handler->python_args != NULL) {
struct gattlib_python_args* args = handler->python_args;
Py_DECREF(args->callback);
Py_DECREF(args->args);
if (args->callback != NULL) {
Py_DECREF(args->callback);
}
if (args->args != NULL) {
Py_DECREF(args->args);
}
handler->python_args = NULL;
free(handler->python_args);
handler->python_args = NULL;

View File

@ -407,7 +407,7 @@ int gattlib_discover_primary(gatt_connection_t* connection, gattlib_primary_serv
count_max++;
}
gattlib_primary_service_t* primary_services = malloc(count_max * sizeof(gattlib_primary_service_t));
gattlib_primary_service_t* primary_services = calloc(count_max * sizeof(gattlib_primary_service_t), 1);
if (primary_services == NULL) {
return GATTLIB_OUT_OF_MEMORY;
}
@ -497,7 +497,7 @@ int gattlib_discover_primary(gatt_connection_t* connection, gattlib_primary_serv
count_max++;
}
gattlib_primary_service_t* primary_services = malloc(count_max * sizeof(gattlib_primary_service_t));
gattlib_primary_service_t* primary_services = calloc(count_max * sizeof(gattlib_primary_service_t), 1);
if (primary_services == NULL) {
return GATTLIB_OUT_OF_MEMORY;
}
@ -667,7 +667,7 @@ int gattlib_discover_char_range(gatt_connection_t* connection, uint16_t start, u
}
gattlib_characteristic_t* characteristic_list = malloc(count_max * sizeof(gattlib_characteristic_t));
gattlib_characteristic_t* characteristic_list = calloc(count_max * sizeof(gattlib_characteristic_t), 1);
if (characteristic_list == NULL) {
return GATTLIB_OUT_OF_MEMORY;
}
@ -889,7 +889,7 @@ int gattlib_discover_char_range(gatt_connection_t* connection, uint16_t start, u
count_max++;
}
gattlib_characteristic_t* characteristic_list = malloc(count_max * sizeof(gattlib_characteristic_t));
gattlib_characteristic_t* characteristic_list = calloc(count_max * sizeof(gattlib_characteristic_t), 1);
if (characteristic_list == NULL) {
return GATTLIB_OUT_OF_MEMORY;
}

View File

@ -181,7 +181,7 @@ static int connect_signal_to_characteristic_uuid(gatt_connection_t* connection,
}
// Add signal to the list
struct gattlib_notification_handle *notification_handle = malloc(sizeof(struct gattlib_notification_handle));
struct gattlib_notification_handle *notification_handle = calloc(sizeof(struct gattlib_notification_handle), 1);
if (notification_handle == NULL) {
return GATTLIB_OUT_OF_MEMORY;
}

View File

@ -117,7 +117,7 @@ static void ble_discovered_device(void *adapter, const char* addr, const char* n
printf("Discovered %s\n", addr);
}
connection = malloc(sizeof(struct connection_t));
connection = calloc(sizeof(struct connection_t), 1);
if (connection == NULL) {
GATTLIB_LOG(GATTLIB_ERROR, "Failt to allocate connection.");
return;