[PATCH libinput] Check realloc() return value before assigning.

Peter Hutterer peter.hutterer at who-t.net
Tue Apr 26 02:38:45 UTC 2016


On Mon, Apr 25, 2016 at 05:57:51PM +0200, Carlos Olmedo Escobar wrote:
> Signed-off-by: Carlos Olmedo Escobar <carlos.olmedo.e at gmail.com>
> ---
>  src/libinput.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/src/libinput.c b/src/libinput.c
> index bcd0dcd..e5a1ae4 100644
> --- a/src/libinput.c
> +++ b/src/libinput.c
> @@ -2578,6 +2578,7 @@ libinput_post_event(struct libinput *libinput,
>  		    struct libinput_event *event)
>  {
>  	struct libinput_event **events = libinput->events;
> +	struct libinput_event **events_temp;
>  	size_t events_len = libinput->events_len;
>  	size_t events_count = libinput->events_count;
>  	size_t move_len;
> @@ -2590,13 +2591,14 @@ libinput_post_event(struct libinput *libinput,
>  	events_count++;
>  	if (events_count > events_len) {
>  		events_len *= 2;
> -		events = realloc(events, events_len * sizeof *events);
> -		if (!events) {
> +		events_temp = realloc(events, events_len * sizeof *events);
> +		if (!events_temp) {
>  			log_error(libinput,
>  				  "Failed to reallocate event ring buffer. "
>  				  "Events may be discarded\n");
>  			return;
>  		}
> +		events = events_temp;

this isn't needed. events is a copy of the libinput->events pointer, so if
'events' cannot be reallocated and is NULL, we discard that copy and exit
the function without other changes. libinput->events thus stays at the
original value.

This patch merely protects 'events' but that isn't used in the realloc
failure anyway.

Cheers,
   Peter

>  
>  		if (libinput->events_count > 0 && libinput->events_in == 0) {
>  			libinput->events_in = libinput->events_len;
> -- 
> 2.8.1


More information about the wayland-devel mailing list