On 2014-04-15 18:37, H Hartley Sweeten wrote:
Step 5 of the (*do_cmdtest) validates that the chanlist, chan/range/aref/etc,
is compatible with the actual hardware. At this point in the (*do_cmdtest)
the chanlist is valid, due to checks in the core, so the sanity check is not
needed.

For aesthetics, factor the step 5 code into a helper function. Tidy up the
factored out code.

Signed-off-by: H Hartley Sweeten <hswee...@visionengravers.com>
Cc: Ian Abbott <abbo...@mev.co.uk>
Cc: Greg Kroah-Hartman <gre...@linuxfoundation.org>
---
  drivers/staging/comedi/drivers/adl_pci9111.c | 65 +++++++++++++++-------------
  1 file changed, 36 insertions(+), 29 deletions(-)

diff --git a/drivers/staging/comedi/drivers/adl_pci9111.c 
b/drivers/staging/comedi/drivers/adl_pci9111.c
index a29ceac..80b3090 100644
--- a/drivers/staging/comedi/drivers/adl_pci9111.c
+++ b/drivers/staging/comedi/drivers/adl_pci9111.c
@@ -314,6 +314,40 @@ static int pci9111_ai_cancel(struct comedi_device *dev,
        return 0;
  }

+static int pci9111_ai_check_chanlist(struct comedi_device *dev,
+                                    struct comedi_subdevice *s,
+                                    struct comedi_cmd *cmd)
+{
+       unsigned int range0 = CR_RANGE(cmd->chanlist[0]);
+       unsigned int aref0 = CR_AREF(cmd->chanlist[0]);
+       int i;
+
+       for (i = 0; i < cmd->chanlist_len; i++) {
+               unsigned int chan = CR_CHAN(cmd->chanlist[i]);
+               unsigned int range = CR_RANGE(cmd->chanlist[i]);
+               unsigned int aref = CR_AREF(cmd->chanlist[i]);
+
+               if (chan != i) {
+                       dev_err(dev->class_dev,
+                               "chanlist must be consecutive channels, counting 
upwards from 0\n");
+                       return -EINVAL;
+               }
+
+               if (range != range0) {
+                       dev_err(dev->class_dev,
+                               "chanlist must all have the same range\n");
+                       return -EINVAL;
+               }
+
+               if (aref != aref0) {
+                       dev_err(dev->class_dev,
+                               "chanlist must all have the same reference\n");
+                       return -EINVAL;
+               }
+       }
+       return 0;
+}
+
  static int pci9111_ai_do_cmd_test(struct comedi_device *dev,
                                  struct comedi_subdevice *s,
                                  struct comedi_cmd *cmd)
@@ -321,8 +355,6 @@ static int pci9111_ai_do_cmd_test(struct comedi_device *dev,
        struct pci9111_private_data *dev_private = dev->private;
        int tmp;
        int error = 0;
-       int range, reference;
-       int i;

        /* Step 1 : check if triggers are trivially valid */

@@ -426,34 +458,9 @@ static int pci9111_ai_do_cmd_test(struct comedi_device 
*dev,
        if (error)
                return 4;

-       /*  Step 5 : check channel list */
-
-       if (cmd->chanlist) {
-
-               range = CR_RANGE(cmd->chanlist[0]);
-               reference = CR_AREF(cmd->chanlist[0]);
+       /* Step 5: check channel list */

-               if (cmd->chanlist_len > 1) {
-                       for (i = 0; i < cmd->chanlist_len; i++) {
-                               if (CR_CHAN(cmd->chanlist[i]) != i) {
-                                       comedi_error(dev,
-                                                    "entries in chanlist must be 
consecutive "
-                                                    "channels,counting upwards from 
0\n");
-                                       error++;
-                               }
-                               if (CR_RANGE(cmd->chanlist[i]) != range) {
-                                       comedi_error(dev,
-                                                    "entries in chanlist must all 
have the same gain\n");
-                                       error++;
-                               }
-                               if (CR_AREF(cmd->chanlist[i]) != reference) {
-                                       comedi_error(dev,
-                                                    "entries in chanlist must all 
have the same reference\n");
-                                       error++;
-                               }
-                       }
-               }
-       }
+       error |= pci9111_ai_check_chanlist(dev, s, cmd);

        if (error)
                return 5;


As for the remarks in patch 02, it's possible for chanlist to be NULL or for chanlist_len to be 0, so pci9111_ai_check_chanlist() should only be called if chanlist is non-NULL and chanlist_len is non-0.

But if chanlist is forced to be NULL by __comedi_get_user_chanlist() when chanlist_len is 0, it would be sufficient to check that chanlist is non-NULL before calling pci9111_ai_check_chanlist().

The original code is buggy as well because it accesses cmd->chanlist[0] without checking cmd->chanlist_len.

--
-=( Ian Abbott @ MEV Ltd.    E-mail: <abbo...@mev.co.uk>        )=-
-=( Tel: +44 (0)161 477 1898   FAX: +44 (0)161 718 3587         )=-
_______________________________________________
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

Reply via email to