[PATCH libevdev 1/3] uinput: close the managed fd on error

Peter Hutterer peter.hutterer at who-t.net
Sun Aug 24 16:53:12 PDT 2014


On Fri, Aug 22, 2014 at 04:58:14PM +0200, David Herrmann wrote:
> Hi
> 
> On Fri, Aug 22, 2014 at 4:56 AM, Peter Hutterer
> <peter.hutterer at who-t.net> wrote:
> > Using LIBEVDEV_UINPUT_OPEN_MANAGED can leak the fd if an error occurs after
> > opening it.
> >
> > Found by Coverity.
> >
> > Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
> > ---
> >  libevdev/libevdev-uinput.c | 3 +++
> >  1 file changed, 3 insertions(+)
> >
> > diff --git a/libevdev/libevdev-uinput.c b/libevdev/libevdev-uinput.c
> > index c1b20e0..ea2b025 100644
> > --- a/libevdev/libevdev-uinput.c
> > +++ b/libevdev/libevdev-uinput.c
> > @@ -279,6 +279,7 @@ libevdev_uinput_create_from_device(const struct libevdev *dev, int fd, struct li
> >         int rc;
> >         struct uinput_user_dev uidev;
> >         struct libevdev_uinput *new_device;
> > +       int close_fd_on_error = (fd == LIBEVDEV_UINPUT_OPEN_MANAGED);
> 
> You still need to fix the error-path exactly below into:
> 
> if (!new_device) {
>         rc = -ENOMEM;
>         goto error;
> }

not necessary IMO, if the first memory allocation fails before we do
anything else, returning immediately is easier here than handling the
various exceptions.

Cheers,
   Peter

> 
> Otherwise, looks good.
> 
> Thanks
> David
> 
> >         new_device = alloc_uinput_device(libevdev_get_name(dev));
> >         if (!new_device)
> > @@ -346,6 +347,8 @@ libevdev_uinput_create_from_device(const struct libevdev *dev, int fd, struct li
> >  error:
> >         rc = -errno;
> >         libevdev_uinput_destroy(new_device);
> > +       if (fd != -1 && close_fd_on_error)
> > +               close(fd);
> >         return rc;
> >  }
> >
> > --
> > 1.9.3
> >
> > _______________________________________________
> > Input-tools mailing list
> > Input-tools at lists.freedesktop.org
> > http://lists.freedesktop.org/mailman/listinfo/input-tools


More information about the Input-tools mailing list