comedi_test: Fix buffer overrun in test_mmap()
authorIan Abbott <abbotti@mev.co.uk>
Mon, 27 Jun 2011 12:03:03 +0000 (13:03 +0100)
committerIan Abbott <abbotti@mev.co.uk>
Mon, 27 Jun 2011 12:03:03 +0000 (13:03 +0100)
If the command does not stop immediately after the last sample, there
can be more data to read than we have room for.  Make sure the 'while'
loop 'read()' does not overrun the end of the malloc'ed 'buf'.

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

index 96e047bc79ee087ae531b7330edbe1ac62f56ea6..deb95e55da54065091188401aeba9b739faf3a5e 100644 (file)
@@ -113,7 +113,8 @@ int test_mmap(void)
        go=1;
        b=buf;
        while(go){
-               ret = read(comedi_fileno(device), b, N_SAMPLES * sample_size);
+               ret = read(comedi_fileno(device), b,
+                               (N_SAMPLES * sample_size) - total);
                if(ret<0){
                        if(errno==EAGAIN){
                                usleep(10000);
@@ -127,6 +128,9 @@ int test_mmap(void)
                        total += ret;
                        b += ret;
                        if(verbose) printf("read %d %d\n",ret,total);
+                       if(total >= (N_SAMPLES * sample_size)){
+                               go = 0;
+                       }
                }
        }