Use zalloc for structs
authorSimon Ser <contact@emersion.fr>
Mon, 31 Jan 2022 21:23:30 +0000 (22:23 +0100)
committerSimon Ser <contact@emersion.fr>
Sat, 5 Feb 2022 14:25:19 +0000 (14:25 +0000)
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 <contact@emersion.fr>
src/connection.c
src/event-loop.c
src/wayland-client.c
src/wayland-server.c
src/wayland-shm.c

index 98f503b..8c51a24 100644 (file)
@@ -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) {
index c7c98b0..37cf95d 100644 (file)
@@ -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;
 
index 4fd7c90..6b0cf43 100644 (file)
@@ -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;
 
index 02f1365..5edbc9c 100644 (file)
@@ -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;
 
index 63ac0d7..28c550d 100644 (file)
@@ -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;