num2str: ensure we never access postfix[] out-of-bounds
authorJens Axboe <axboe@fb.com>
Mon, 14 Apr 2014 16:21:11 +0000 (10:21 -0600)
committerJens Axboe <axboe@fb.com>
Mon, 14 Apr 2014 16:21:11 +0000 (10:21 -0600)
Signed-off-by: Jens Axboe <axboe@fb.com>
lib/num2str.c

index 12d6f39aa5dbca892e9d03af9150aa28a3a47861..896186888716424f13ecf0dc174a416cffeb7bd6 100644 (file)
@@ -4,6 +4,8 @@
 
 #include "../fio.h"
 
+#define ARRAY_LENGTH(arr)      sizeof(arr) / sizeof((arr)[0])
+
 /*
  * Cheesy number->string conversion, complete with carry rounding error.
  */
@@ -46,6 +48,9 @@ char *num2str(unsigned long num, int maxlen, int base, int pow2, int unit_base)
 
        if (modulo == -1U) {
 done:
+               if (post_index >= ARRAY_LENGTH(postfix))
+                       post_index = 0;
+
                sprintf(buf, "%lu%s%s", num, postfix[post_index],
                        byte_postfix[byte_post_index]);
                return buf;