iowatcher: Add bounds checking in find_step
[blktrace.git] / iowatcher / plot.c
index 187789a2b94285080819743a987bf0ac1c957ef2..d486f293fc2bdf3103853c11346568cca2fa2c39 100644 (file)
@@ -12,7 +12,7 @@
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  *
  *  Parts of this file were imported from Jens Axboe's blktrace sources (also GPL)
  */
@@ -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;
 }