comedi_open has file descriptor leak
authorAnders Blomdell <anders.blomdell@control.lth.se>
Wed, 22 Aug 2012 07:21:56 +0000 (08:21 +0100)
committerIan Abbott <abbotti@mev.co.uk>
Wed, 22 Aug 2012 08:54:19 +0000 (09:54 +0100)
If something fails after a succesful open, cleanup fails to close the file.

The code

   cleanup:
         if(it)
                 free(it);

needs to be replaced with something like:

   cleanup:
         if (it) {
           if (it->fd >= 0) {
             close(it->fd);
           }
         free(it);

Regards

Anders Blomdell

lib/comedi.c

index 5877e6a2548391215c0a8008f1b34c511fabd82a..0d2086fedeac57c5be5eff53d6a8e361dc65f3c2 100644 (file)
@@ -77,8 +77,14 @@ comedi_t* _comedi_open(const char *fn)
 
        return it;
 cleanup:
-       if(it)
+       if(it) {
+               /* As long as get_subdevices is the last action above,
+                  it->subdevices should not need any cleanup, since
+                  get_subdevices should have done the cleanup already */
+               if (it->fd >= 0)
+                       close(it->fd);
                free(it);
+       }
 
        return NULL;
 }