[PATCH] Check malloc result

Bryce W. Harrington b.harrington at samsung.com
Tue Apr 15 11:55:58 PDT 2014


Much better error checking.  In the out_pixman_error finalizer should it
also destroy the zalloc'd sb object, since we're going to return NULL in
this case?  Also see one more comment below.

But these are just nit picks; LGTM even without those changes.

Reviewed-by: Bryce Harrington <b.harrington at samsung.com>

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.
> ---
>  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);

maybe, since sb->size already holds the calculation:

	munmap(data, sb->size);

> +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