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

Pekka Paalanen ppaalanen at gmail.com
Thu Apr 24 06:26:20 PDT 2014


On Mon, 21 Apr 2014 23:51:03 +0000
"Bryce W. Harrington" <b.harrington at samsung.com> wrote:

> Checking for these errors in the clients is perhaps a bit gratuitous but
> can't hurt.
> 
> 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;

No need to check 'terminal', xzalloc() will exit() if it ever goes 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;

I'd be fine with converting all the *allocs to the x*alloc which checks
for NULL and exits. All programs using window.h can do that.


Thanks,
pq


More information about the wayland-devel mailing list