iowatcher: Add bounds checking in find_step
authorAndrew Price <anprice@redhat.com>
Wed, 6 Nov 2013 16:11:18 +0000 (16:11 +0000)
committerChris Mason <clm@fb.com>
Wed, 24 Sep 2014 19:02:08 +0000 (12:02 -0700)
Check the value of cur_mini_step is sane before using it as an index to
mini_step array.

Signed-off-by: Andrew Price <anprice@redhat.com>
iowatcher/plot.c

index 971a2539ff93a2db1cd6255ce15aebbba6a73658..d486f293fc2bdf3103853c11346568cca2fa2c39 100644 (file)
@@ -530,10 +530,12 @@ static double find_step(double first, double last, int num_ticks)
        /* Round to power of 10 */
        step = exp(floor(log(step) / log10) * log10);
        /* Scale down step to provide enough ticks */
-       while ((last - first) / (step * mini_step[cur_mini_step]) > num_ticks
-              && cur_mini_step < TICK_MINI_STEPS)
+       while (cur_mini_step < TICK_MINI_STEPS
+              && (last - first) / (step * mini_step[cur_mini_step]) > num_ticks)
                cur_mini_step++;
-       step *= mini_step[cur_mini_step - 1];
+
+       if (cur_mini_step > 0)
+               step *= mini_step[cur_mini_step - 1];
 
        return step;
 }