Prevent time_t overflow 01/243101/2
authorJusung Son <jusung07.son@samsung.com>
Thu, 3 Sep 2020 05:49:15 +0000 (14:49 +0900)
committerJusung Son <jusung07.son@samsung.com>
Sun, 9 Aug 2020 08:21:28 +0000 (17:21 +0900)
- If the RTC is reset, a time overflow may occur.

Change-Id: I61920cc1927942e151e86bd456490bb2637193b6
Signed-off-by: Jusung Son <jusung07.son@samsung.com>
server/alarm-manager-schedule.c
server/alarm-manager.c

index e454c85..ba1409f 100644 (file)
@@ -430,6 +430,11 @@ void _alarm_set_next_duetime(__alarm_info_t *__alarm_info)
                return;
        }
 
+       if (due_time < 0) {
+               LOGE("due_time(%ld) is wrong(id : %d)", due_time, __alarm_info->alarm_id);
+               return;
+       }
+
        if (mode->repeat != ALARM_REPEAT_MODE_WEEKLY && mode->repeat != ALARM_REPEAT_MODE_ONCE) {
                due_tm = localtime_r(&due_time, &tm);
                if (is_dst == 0 && due_tm && due_tm->tm_isdst == 1) {
index 3e06f06..0c91d96 100644 (file)
@@ -296,6 +296,9 @@ static bool __set_time(time_t _time)
                return false;
        }
 
+       if (!_set_latest_settime(_time))
+               LOGE("DB is failed.");
+
        if (using_rtc) {
                char buf[1024];
                char log_tag[ALARMMGR_LOG_TAG_SIZE] = {0,};
@@ -316,9 +319,6 @@ static bool __set_time(time_t _time)
                        LOGD("ALARM_SET_RTC ioctl is succeed. [%d]", (int)_time);
                        strncpy(log_tag, "SET RTC START", sizeof(log_tag) - 1);
                        _save_module_log(log_tag, log_message);
-
-                       if (!_set_latest_settime(_time))
-                               LOGE("DB is failed.");
                        return true;
                }
        } else {
@@ -611,18 +611,32 @@ static void __alarm_update_due_time_of_all_items_in_list(time_t new_time, double
                                        continue;
                                }
 
+                               if (entry->due_time + diff_time < diff_time) {
+                                       LOGE("time_t OVERFLOW!! %ld  %f id %d",
+                                               entry->due_time, diff_time, entry->alarm_id);
+                                       continue;
+                               }
                                entry->due_time += diff_time;
                                entry->base_info.reserved_info = new_time;
 
                        } else {
-                               entry->due_time += diff_time;
                                if (is_rtc_reset) {
+                                       if (entry->due_time > new_time)
+                                               continue;
+
                                        entry->due_time = new_time +
-                                               ((entry->due_time - new_time) % base_info->mode.u_interval.interval);
+                                               (new_time % base_info->mode.u_interval.interval);
                                        LOGE("[ RTC reset]: new time %s %ld, diff %f, id %d duetime %s %ld %ld",
                                                ctime(&new_time), new_time, diff_time, entry->alarm_id,
                                                ctime(&entry->due_time), entry->due_time,
                                                base_info->mode.u_interval.interval);
+                               } else {
+                                       if (entry->due_time + diff_time < diff_time) {
+                                               LOGE("time_t OVERFLOW!! %ld  %f id %d",
+                                                       entry->due_time, diff_time, entry->alarm_id);
+                                               continue;
+                                       }
+                                       entry->due_time += diff_time;
                                }
                        }
 
@@ -3171,15 +3185,15 @@ void _alarm_initialize()
                        LOGE("power saving mode init failed");
                        exit(1);
                }
+       }
 
-               if (_get_latest_settime(&latest_settime)) {
-                       time_t current_time;
-                       time(&current_time);
-                       if (current_time < latest_settime) {
-                               LOGE("[RTC_RESET] RTC goes back into the past.");
-                               _save_module_log("FAIL: RTC_RESET", "RTC goes back into the past.");
-                               is_rtc_reset = true;
-                       }
+       if (_get_latest_settime(&latest_settime)) {
+               time_t current_time;
+               time(&current_time);
+               if (current_time < latest_settime) {
+                       LOGE("[RTC_RESET] RTC goes back into the past.");
+                       _save_module_log("FAIL: RTC_RESET", "RTC goes back into the past.");
+                       is_rtc_reset = true;
                }
        }