perf tests: Fix out of bounds access on array fd when cnt is 100
authorColin Ian King <[email protected]>
Wed, 14 Mar 2018 17:33:54 +0000 (17:33 +0000)
committerArnaldo Carvalho de Melo <[email protected]>
Fri, 16 Mar 2018 16:56:44 +0000 (13:56 -0300)
Currently when cnt is 100 an array bounds overflow occurs on the
assignment of fd[cnt]. Fix this by performing the bounds check on cnt
before writing to fd.

Detected by cppcheck:

tools/perf/tests/bp_account.c:115: (warning) Either the condition
'cnt==100' is redundant or the array 'fd[100]' is accessed at index 100,
which is out of bounds.

Signed-off-by: Colin King <[email protected]>
Cc: Alexander Shishkin <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Namhyung Kim <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: [email protected]
Fixes: 032db28e5fa3 ("perf tests: Add breakpoint accounting/modify test")
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
tools/perf/tests/bp_account.c

index 2f75fa0c4fefe84e4a4f3000b4413f371ce18a34..9e88d760895159730a1727c8efad7d32d777487b 100644 (file)
@@ -107,16 +107,14 @@ static int detect_cnt(bool is_x)
        int fd[100], cnt = 0, i;
 
        while (1) {
-               fd[cnt] = __event(is_x, addr, &attr);
-
-               if (fd[cnt] < 0)
-                       break;
-
                if (cnt == 100) {
                        pr_debug("way too many debug registers, fix the test\n");
                        return 0;
                }
+               fd[cnt] = __event(is_x, addr, &attr);
 
+               if (fd[cnt] < 0)
+                       break;
                cnt++;
        }