[PATCH] wcap: Check for mmap and malloc return value in wcap decode module

Kristian Høgsberg hoegsberg at gmail.com
Tue May 6 15:25:59 PDT 2014


On Tue, May 06, 2014 at 03:54:49PM +0530, vivek wrote:
> Checking for return value in main.c for wcap_decoder_create function
> and mmap, malloc return value in wcap_decoder_create function to avoid
> crashes

Thanks, that looks better.

Kristian

> Signed-off-by: vivek <vivek.ellur at samsung.com>
> ---
>  wcap/main.c        |    4 ++++
>  wcap/wcap-decode.c |    9 +++++++++
>  2 files changed, 13 insertions(+)
> 
> diff --git a/wcap/main.c b/wcap/main.c
> index 29bb9c3..16d37f0 100644
> --- a/wcap/main.c
> +++ b/wcap/main.c
> @@ -251,6 +251,10 @@ int main(int argc, char *argv[])
>  	}
>  
>  	decoder = wcap_decoder_create(argv[1]);
> +	if (decoder == NULL) {
> +		fprintf(stderr, "Creating wcap decoder failed\n");
> +		exit(EXIT_FAILURE);
> +	}
>  
>  	if (yuv4mpeg2 && isatty(1)) {
>  		fprintf(stderr, "Not dumping yuv4mpeg2 data to terminal.  Pipe output to a file or a process.\n");
> diff --git a/wcap/wcap-decode.c b/wcap/wcap-decode.c
> index 87d9337..76ecc2f 100644
> --- a/wcap/wcap-decode.c
> +++ b/wcap/wcap-decode.c
> @@ -126,6 +126,11 @@ wcap_decoder_create(const char *filename)
>  	decoder->size = buf.st_size;
>  	decoder->map = mmap(NULL, decoder->size,
>  			    PROT_READ, MAP_PRIVATE, decoder->fd, 0);
> +	if (decoder->map == MAP_FAILED) {
> +		fprintf(stderr, "mmap failed\n");
> +		free(decoder);
> +		return NULL;
> +	}
>  		
>  	header = decoder->map;
>  	decoder->format = header->format;
> @@ -137,6 +142,10 @@ wcap_decoder_create(const char *filename)
>  
>  	frame_size = header->width * header->height * 4;
>  	decoder->frame = malloc(frame_size);
> +	if (decoder->frame == NULL) {
> +		free(decoder);
> +		return NULL;
> +	}
>  	memset(decoder->frame, 0, frame_size);
>  
>  	return decoder;
> -- 
> 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