Fix memory leak for saved channel list
authorIan Abbott <abbotti@mev.co.uk>
Fri, 28 Sep 2012 12:06:54 +0000 (13:06 +0100)
committerIan Abbott <abbotti@mev.co.uk>
Fri, 28 Sep 2012 12:06:54 +0000 (13:06 +0100)
When `do_cmd_ioctl()` allocates memory for the kernel copy of a channel
list, it frees any previously allocated channel list in
`async->cmd.chanlist` and replaces it with the new one.  However, if the
device is ever removed (or "detached") the cleanup code in
`cleanup_device()` in "drivers.c" does not free this memory so it is
lost.

A sensible place to free the kernel copy of the channel list is in
`do_become_nonbusy()` as at that point the comedi asynchronous command
associated with the channel list is no longer valid.  Free the channel
list in `do_become_nonbusy()` instead of `do_cmd_ioctl()` and clear the
pointer to prevent it being freed more than once.

Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
comedi/comedi_fops.c

index d4e209dbf804956256a45689e99c2f75a00b28d2..5f64100ce06d77ead3970eb84cc398de0326caaf 100644 (file)
@@ -1129,8 +1129,6 @@ static int do_cmd_ioctl(comedi_device * dev, comedi_cmd __user *arg, void *file)
                goto cleanup;
        }
 
-       if (async->cmd.chanlist)
-               kfree(async->cmd.chanlist);
        async->cmd = user_cmd;
        async->cmd.data = NULL;
        /* load channel/gain list */
@@ -1873,6 +1871,8 @@ void do_become_nonbusy(comedi_device * dev, comedi_subdevice * s)
        if (async) {
                comedi_reset_async_buf(async);
                async->inttrig = NULL;
+               kfree(async->cmd.chanlist);
+               async->cmd.chanlist = NULL;
        } else {
                printk("BUG: (?) do_become_nonbusy called with async=0\n");
        }