Make it clear to Coverity that the tmp buffer in switch_ioscheduler() is \0-terminated
authorBart Van Assche <bart.vanassche@wdc.com>
Thu, 29 Mar 2018 15:24:23 +0000 (08:24 -0700)
committerBart Van Assche <bart.vanassche@wdc.com>
Thu, 29 Mar 2018 15:47:52 +0000 (08:47 -0700)
The 'tmp' buffer used by switch_ioscheduler() will be '\0'-terminated
because the I/O scheduler name read from sysfs has much fewer characters
than the buffer size (256). Make it clear to Coverity that it is
guaranteed that the 'tmp' buffer is '\0'-terminated. Additionally, fix
the order of the fread() arguments. This patch fixes Coverity ID #24132.

Fixes: b44b9e45cf38 ("Clear sysfs path before reading current ioscheduler from sysfs")
Signed-off-by: Bart Van Assche <bart.vanassche@wdc.com>
backend.c

index fc83ed178a05eca587570d7de5495a0182930c3f..fe335b5edbd1db3da837f095d1950a1408d3c256 100644 (file)
--- a/backend.c
+++ b/backend.c
@@ -1364,13 +1364,13 @@ static int switch_ioscheduler(struct thread_data *td)
        /*
         * Read back and check that the selected scheduler is now the default.
         */
-       memset(tmp, 0, sizeof(tmp));
-       ret = fread(tmp, sizeof(tmp), 1, f);
+       ret = fread(tmp, 1, sizeof(tmp) - 1, f);
        if (ferror(f) || ret < 0) {
                td_verror(td, errno, "fread");
                fclose(f);
                return 1;
        }
+       tmp[ret] = '\0';
        /*
         * either a list of io schedulers or "none\n" is expected.
         */