From 5eb5620cbd317aac4a61e357c3c92bd889d6079b Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Mon, 31 Jan 2022 22:23:30 +0100 Subject: [PATCH] Use zalloc for structs When allocating memory for structs, use zalloc instead of malloc. This ensures the memory is zero-initialized, and reduces the risk of forgetting to initialize all struct fields. Signed-off-by: Simon Ser --- src/connection.c | 4 ++-- src/event-loop.c | 10 +++++----- src/wayland-client.c | 2 +- src/wayland-server.c | 8 ++++---- src/wayland-shm.c | 4 ++-- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/connection.c b/src/connection.c index 98f503b..8c51a24 100644 --- a/src/connection.c +++ b/src/connection.c @@ -568,10 +568,10 @@ wl_closure_init(const struct wl_message *message, uint32_t size, if (size) { *num_arrays = wl_message_count_arrays(message); - closure = malloc(sizeof *closure + size + + closure = zalloc(sizeof *closure + size + *num_arrays * sizeof(struct wl_array)); } else { - closure = malloc(sizeof *closure); + closure = zalloc(sizeof *closure); } if (!closure) { diff --git a/src/event-loop.c b/src/event-loop.c index c7c98b0..37cf95d 100644 --- a/src/event-loop.c +++ b/src/event-loop.c @@ -179,7 +179,7 @@ wl_event_loop_add_fd(struct wl_event_loop *loop, { struct wl_event_source_fd *source; - source = malloc(sizeof *source); + source = zalloc(sizeof *source); if (source == NULL) return NULL; @@ -568,7 +568,7 @@ wl_event_loop_add_timer(struct wl_event_loop *loop, if (wl_timer_heap_ensure_timerfd(&loop->timers) < 0) return NULL; - source = malloc(sizeof *source); + source = zalloc(sizeof *source); if (source == NULL) return NULL; @@ -718,7 +718,7 @@ wl_event_loop_add_signal(struct wl_event_loop *loop, struct wl_event_source_signal *source; sigset_t mask; - source = malloc(sizeof *source); + source = zalloc(sizeof *source); if (source == NULL) return NULL; @@ -775,7 +775,7 @@ wl_event_loop_add_idle(struct wl_event_loop *loop, { struct wl_event_source_idle *source; - source = malloc(sizeof *source); + source = zalloc(sizeof *source); if (source == NULL) return NULL; @@ -885,7 +885,7 @@ wl_event_loop_create(void) { struct wl_event_loop *loop; - loop = malloc(sizeof *loop); + loop = zalloc(sizeof *loop); if (loop == NULL) return NULL; diff --git a/src/wayland-client.c b/src/wayland-client.c index 4fd7c90..6b0cf43 100644 --- a/src/wayland-client.c +++ b/src/wayland-client.c @@ -343,7 +343,7 @@ wl_display_create_queue(struct wl_display *display) { struct wl_event_queue *queue; - queue = malloc(sizeof *queue); + queue = zalloc(sizeof *queue); if (queue == NULL) return NULL; diff --git a/src/wayland-server.c b/src/wayland-server.c index 02f1365..5edbc9c 100644 --- a/src/wayland-server.c +++ b/src/wayland-server.c @@ -1062,7 +1062,7 @@ wl_display_create(void) if (debug && (strstr(debug, "server") || strstr(debug, "1"))) debug_server = 1; - display = malloc(sizeof *display); + display = zalloc(sizeof *display); if (display == NULL) return NULL; @@ -1238,7 +1238,7 @@ wl_global_create(struct wl_display *display, return NULL; } - global = malloc(sizeof *global); + global = zalloc(sizeof *global); if (global == NULL) return NULL; @@ -1822,7 +1822,7 @@ wl_resource_create(struct wl_client *client, { struct wl_resource *resource; - resource = malloc(sizeof *resource); + resource = zalloc(sizeof *resource); if (resource == NULL) return NULL; @@ -1888,7 +1888,7 @@ wl_display_add_protocol_logger(struct wl_display *display, { struct wl_protocol_logger *logger; - logger = malloc(sizeof *logger); + logger = zalloc(sizeof *logger); if (!logger) return NULL; diff --git a/src/wayland-shm.c b/src/wayland-shm.c index 63ac0d7..28c550d 100644 --- a/src/wayland-shm.c +++ b/src/wayland-shm.c @@ -223,7 +223,7 @@ shm_pool_create_buffer(struct wl_client *client, struct wl_resource *resource, return; } - buffer = malloc(sizeof *buffer); + buffer = zalloc(sizeof *buffer); if (buffer == NULL) { wl_client_post_no_memory(client); return; @@ -312,7 +312,7 @@ shm_create_pool(struct wl_client *client, struct wl_resource *resource, goto err_close; } - pool = malloc(sizeof *pool); + pool = zalloc(sizeof *pool); if (pool == NULL) { wl_client_post_no_memory(client); goto err_close; -- 2.7.4