Use localtime_r() instead of localtime() 79/193079/1
authorMilind Murhekar <m.murhekar@samsung.com>
Wed, 14 Nov 2018 08:59:45 +0000 (14:29 +0530)
committerMilind Murhekar <m.murhekar@samsung.com>
Wed, 14 Nov 2018 08:59:45 +0000 (14:29 +0530)
This patch fixes the SVACE issue for locatime()
usage without thread safety.

Change-Id: Icc67ba1f949847b2ee5c903a1035e9ede6e764e2
Signed-off-by: Milind Murhekar <m.murhekar@samsung.com>
src/monitor/stc-time.c

index 750b43d..e0f86e0 100644 (file)
 
 time_t stc_time_get_day_start(time_t now)
 {
-       struct tm *curr;
+       struct tm curr;
+       struct tm *res;
 
-       curr = localtime(&now);
+       res = localtime_r(&now, &curr);
 
-       curr->tm_sec = 0;
-       curr->tm_min = 0;
-       curr->tm_hour = 0;
+       curr.tm_sec = 0;
+       curr.tm_min = 0;
+       curr.tm_hour = 0;
 
-       return mktime(curr);
+       return mktime(&curr);
 }
 
 time_t stc_time_get_week_start(time_t now)
 {
-       struct tm *curr;
+       struct tm curr;
+       struct tm *res;
        int days;
 
-       curr = localtime(&now);
+       res = localtime_r(&now, &curr);
 
-       curr->tm_sec = 0;
-       curr->tm_min = 0;
-       curr->tm_hour = 0;
+       curr.tm_sec = 0;
+       curr.tm_min = 0;
+       curr.tm_hour = 0;
 
-       if (curr->tm_wday > 1)
-               days = curr->tm_wday - 1;
+       if (curr.tm_wday > 1)
+               days = curr.tm_wday - 1;
        else
-               days = 1 - curr->tm_wday;
+               days = 1 - curr.tm_wday;
 
-       return (mktime(curr) - (days * SEC_IN_DAY));
+       return (mktime(&curr) - (days * SEC_IN_DAY));
 }
 
 time_t stc_time_get_month_start(time_t now, int month_start_date)
 {
-       struct tm *curr;
+       struct tm curr;
+       struct tm *res;
        bool is_leap_year;
 
-       curr = localtime(&now);
+       res = localtime_r(&now, &curr);
 
-       curr->tm_sec = 0;
-       curr->tm_min = 0;
-       curr->tm_hour = 0;
+       curr.tm_sec = 0;
+       curr.tm_min = 0;
+       curr.tm_hour = 0;
 
-       if (curr->tm_mday < month_start_date) {
-               curr->tm_mon--;
-               if (curr->tm_mon < 0) {
-                       curr->tm_mon = 11;
-                       curr->tm_year--;
+       if (curr.tm_mday < month_start_date) {
+               curr.tm_mon--;
+               if (curr.tm_mon < 0) {
+                       curr.tm_mon = 11;
+                       curr.tm_year--;
                }
        }
 
-       is_leap_year = ((curr->tm_year + 1900) % 4 ? 0 : 1);
-       curr->tm_mday = month_start_date;
+       is_leap_year = ((curr.tm_year + 1900) % 4 ? 0 : 1);
+       curr.tm_mday = month_start_date;
 
        switch (month_start_date) {
        case 29:
        case 30:
-               if (curr->tm_mon == 1 && !is_leap_year)
-                       curr->tm_mday = 28;
+               if (curr.tm_mon == 1 && !is_leap_year)
+                       curr.tm_mday = 28;
 
-               else if (curr->tm_mon == 1 && is_leap_year)
-                       curr->tm_mday = 29;
+               else if (curr.tm_mon == 1 && is_leap_year)
+                       curr.tm_mday = 29;
 
                break;
        case 31:
-               if (curr->tm_mon == 1 && !is_leap_year)
-                       curr->tm_mday = 28;
+               if (curr.tm_mon == 1 && !is_leap_year)
+                       curr.tm_mday = 28;
 
-               else if (curr->tm_mon == 1 && is_leap_year)
-                       curr->tm_mday = 29;
+               else if (curr.tm_mon == 1 && is_leap_year)
+                       curr.tm_mday = 29;
 
-               else if (curr->tm_mon == 3 || curr->tm_mon == 5 ||
-                        curr->tm_mon == 8 || curr->tm_mon == 10)
-                       curr->tm_mday = 30;
+               else if (curr.tm_mon == 3 || curr.tm_mon == 5 ||
+                        curr.tm_mon == 8 || curr.tm_mon == 10)
+                       curr.tm_mday = 30;
 
                break;
        default:
                ;//Do Nothing
        };
 
-       return mktime(curr);
+       return mktime(&curr);
 }