[PATCH evemu 1/2] evemu-record.c: fix memory access in error path

Peter Hutterer peter.hutterer at who-t.net
Sun Sep 17 00:10:56 UTC 2017


On Fri, Sep 15, 2017 at 11:02:20AM +0200, Martin Kepplinger wrote:
> In case, for example, the output file could not have been opened, it
> is accessed anyways if it's zero, resulting in a segmentation fault.
> 
> So let's free resources in the out-path only if they have been allocated
> before.
> 
> Signed-off-by: Martin Kepplinger <martink at posteo.de>
> ---
>  tools/evemu-record.c | 10 +++++++---
>  1 file changed, 7 insertions(+), 3 deletions(-)
> 
> diff --git a/tools/evemu-record.c b/tools/evemu-record.c
> index 0eaa31d..54c7f92 100644
> --- a/tools/evemu-record.c
> +++ b/tools/evemu-record.c
> @@ -337,9 +337,13 @@ int main(int argc, char *argv[])
>  
>  	rc = 0;
>  out:
> -	free(device);
> -	close(fd);
> -	if (output != stdout) {
> +	if (device)
> +		free(device);
> +
> +	if (fd > 0)
> +		close(fd);

those two aren't necessary, free(NULL) has been possible forever and
close(-1) is ignored, so looks like we only need the hunk below.

Coincidentally, checking an fd for > 0 is a potential bug, 0 is a valid fd.

> +
> +	if (output && output != stdout) {
>  		fclose(output);
>  		output = stdout;
>  	}

can you remove the two above and re-send the patch just with this hunk
please? thanks (and tested, of course ;)


Cheers,
   Peter


More information about the Input-tools mailing list