Handle exceptional cases in request_check_speed() to avoid divide-by-zero 67/158167/2
authorHyotaek Shim <hyotaek.shim@samsung.com>
Mon, 30 Oct 2017 07:32:30 +0000 (16:32 +0900)
committerHyotaek Shim <hyotaek.shim@samsung.com>
Mon, 30 Oct 2017 07:34:29 +0000 (07:34 +0000)
Change-Id: I9ff67572cbe5b866aff2e1f49220c60271aa81b9
Signed-off-by: Hyotaek Shim <hyotaek.shim@samsung.com>
src/block/block.c

index e371b6d..1c22a56 100644 (file)
@@ -3452,6 +3452,7 @@ static DBusMessage *request_check_speed(dbus_method_reply_handle_h reply_handle,
        int ret = 0;
        int id;
        int fd;
+       int time_diff;
 
        if (!reply_handle || !msg)
                return NULL;
@@ -3477,7 +3478,7 @@ static DBusMessage *request_check_speed(dbus_method_reply_handle_h reply_handle,
 
        _D("speed check: %s", data->devnode);
        fd = open(data->devnode, O_RDWR | O_SYNC);
-       buf = calloc(1, SPEEDCHECK * 1024 * 1024);
+       buf = calloc(1, SPEEDCHECK << 20);
        if (!buf) {
                _E("calloc() failed");
                close(fd);
@@ -3486,13 +3487,14 @@ static DBusMessage *request_check_speed(dbus_method_reply_handle_h reply_handle,
        }
        clock_gettime(CLOCK_REALTIME, &start_time);
        _I("start time: %lu.%lu", start_time.tv_sec, start_time.tv_nsec);
-       ret = write(fd, buf, SPEEDCHECK * 1024 * 1024);
+       ret = write(fd, buf, SPEEDCHECK << 20);
        clock_gettime(CLOCK_REALTIME, &end_time);
        _I("end time %lu.%lu", end_time.tv_sec, end_time.tv_nsec);
 
        free(buf);
 
-       if (SPEEDCHECK / (end_time.tv_sec - start_time.tv_sec) < 4) {
+       time_diff = end_time.tv_sec - start_time.tv_sec;
+       if (time_diff > 0 && (SPEEDCHECK / time_diff < 4)) {
                ret = -1;
                close(fd);
                goto out;