From 2b8c2f8087d69be00c33a20d33d80106efa7f8a6 Mon Sep 17 00:00:00 2001 From: Anders Blomdell Date: Wed, 22 Aug 2012 08:21:56 +0100 Subject: [PATCH] comedi_open has file descriptor leak 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 | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/comedi.c b/lib/comedi.c index 5877e6a..0d2086f 100644 --- a/lib/comedi.c +++ b/lib/comedi.c @@ -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; } -- 2.26.2