[PATCH] Check malloc result
Kristian Høgsberg
hoegsberg at gmail.com
Mon Apr 21 15:44:00 PDT 2014
On Fri, Apr 11, 2014 at 09:06:58AM +0200, Hardening wrote:
> This patch checks malloc was successfull and release resources if it
> wasn't.
Commmitted, thanks.
Kristian
> ---
> src/screen-share.c | 17 +++++++++++++++--
> 1 file changed, 15 insertions(+), 2 deletions(-)
>
> diff --git a/src/screen-share.c b/src/screen-share.c
> index 5de20be..d3e3f05 100644
> --- a/src/screen-share.c
> +++ b/src/screen-share.c
> @@ -434,11 +434,12 @@ shared_output_get_shm_buffer(struct shared_output *so)
> data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
> if (data == MAP_FAILED) {
> weston_log("mmap: %m");
> - close(fd);
> - return NULL;
> + goto out_close;
> }
>
> sb = zalloc(sizeof *sb);
> + if (!sb)
> + goto out_unmap;
>
> sb->output = so;
> wl_list_init(&sb->free_link);
> @@ -457,14 +458,26 @@ shared_output_get_shm_buffer(struct shared_output *so)
> wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
> wl_shm_pool_destroy(pool);
> close(fd);
> + fd = -1;
>
> memset(data, 0, sb->size);
>
> sb->pm_image =
> pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
> (uint32_t *)data, stride);
> + if (!sb->pm_image)
> + goto out_pixman_error;
>
> return sb;
> +
> +out_pixman_error:
> + pixman_region32_fini(&sb->damage);
> +out_unmap:
> + munmap(data, height * stride);
> +out_close:
> + if (fd != -1)
> + close(fd);
> + return NULL;
> }
>
> static void
> --
> 1.8.1.2
>
> _______________________________________________
> wayland-devel mailing list
> wayland-devel at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/wayland-devel
More information about the wayland-devel
mailing list