[PATCH 3/3] clients: Check zalloc return for out of memory situation

Kristian Høgsberg hoegsberg at gmail.com
Fri Apr 25 13:21:15 PDT 2014


On Mon, Apr 21, 2014 at 11:51:03PM +0000, Bryce W. Harrington wrote:
> Checking for these errors in the clients is perhaps a bit gratuitous but
> can't hurt.

In the clients we use the x*alloc functions which abort on allocation
failure.

Kristian

> Signed-off-by: Bryce Harrington <b.harrington at samsung.com>
> ---
>  clients/gears.c    |    3 +++
>  clients/terminal.c |   20 ++++++++++++++++++++
>  2 files changed, 23 insertions(+)
> 
> diff --git a/clients/gears.c b/clients/gears.c
> index 93a86b4..22159be 100644
> --- a/clients/gears.c
> +++ b/clients/gears.c
> @@ -402,6 +402,9 @@ gears_create(struct display *display)
>  	int i;
>  
>  	gears = zalloc(sizeof *gears);
> +	if (gears == NULL)
> +		die("failed to zalloc memory for gears\n");
> +
>  	gears->d = display;
>  	gears->window = window_create(display);
>  	gears->widget = window_frame_create(gears->window, gears);
> diff --git a/clients/terminal.c b/clients/terminal.c
> index 5931ce2..6bdb039 100644
> --- a/clients/terminal.c
> +++ b/clients/terminal.c
> @@ -773,9 +773,24 @@ terminal_resize_cells(struct terminal *terminal,
>  		terminal->max_width = width;
>  		data_pitch = width * sizeof(union utf8_char);
>  		data = zalloc(data_pitch * terminal->buffer_height);
> +		if (data == NULL) {
> +			fprintf(stderr, "failed to zalloc data: %m\n");
> +			return;
> +		}
>  		attr_pitch = width * sizeof(struct attr);
>  		data_attr = malloc(attr_pitch * terminal->buffer_height);
> +		if (data_attr == NULL) {
> +			fprintf(stderr, "failed to zalloc data_attr: %m\n");
> +			free(data);
> +			return;
> +		}
>  		tab_ruler = zalloc(width);
> +		if (tab_ruler == NULL) {
> +			fprintf(stderr, "failed to zalloc tab_ruler: %m\n");
> +			free(data);
> +			free(data_attr);
> +			return;
> +		}
>  		attr_init(data_attr, terminal->curr_attr,
>  			  width * terminal->buffer_height);
>  
> @@ -2835,6 +2850,8 @@ terminal_create(struct display *display)
>  	cairo_text_extents_t text_extents;
>  
>  	terminal = xzalloc(sizeof *terminal);
> +	if (terminal == NULL)
> +		return NULL;
>  	terminal->color_scheme = &DEFAULT_COLORS;
>  	terminal_init(terminal);
>  	terminal->margin_top = 0;
> @@ -2961,6 +2978,9 @@ terminal_run(struct terminal *terminal, const char *path)
>  	} else if (pid < 0) {
>  		fprintf(stderr, "failed to fork and create pty (%m).\n");
>  		return -1;
> +	} else if (terminal == NULL) {
> +		fprintf(stderr, "out of memory: %m\n");
> +		return -1;
>  	}
>  
>  	terminal->master = master;
> -- 
> 1.7.9.5
> _______________________________________________
> 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