Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / extlibs / timer / timer.c
old mode 100644 (file)
new mode 100755 (executable)
index 0dccd4e..5686d19
@@ -104,7 +104,21 @@ long int getRelativeIntervalOfWeek(struct tm* tp)
 
     time(&current_time);
     current = localtime(&current_time);
+
+    if(current == NULL)
+    {
+        printf("ERROR; Getting local time fails\n");
+        return 0;
+    }
+
     midnight = (struct tm* )malloc(sizeof(struct tm));
+
+    if(midnight == NULL)
+    {
+        printf("ERROR; Memory allocation fails\n");
+        return 0;
+    }
+
     memcpy(midnight, current, sizeof(struct tm));
 
     midnight->tm_hour = 0;
@@ -117,6 +131,8 @@ long int getRelativeIntervalOfWeek(struct tm* tp)
     delayed_time = getRelativeSecondsOfDayofweek(current->tm_wday, tp->tm_wday) - delayed_time;
     delayed_time = delayed_time + getSeconds(tp);
 
+    free(midnight);
+
     return delayed_time;
 }
 
@@ -193,7 +209,7 @@ time_t registerTimer(const time_t seconds, int *id, void *cb)
 
 void unregisterTimer(int idx)
 {
-    if( 0 <= idx && idx <= TIMEOUTS)
+    if( 0 <= idx && idx < TIMEOUTS)
         timeout_list[idx].timeout_state = TIMEOUT_UNUSED;
 }
 
@@ -318,7 +334,7 @@ time_t registerTimer(const time_t seconds, int *id,  void (*cb)())
 
 void unregisterTimer(int idx)
 {
-    if( 0 <= idx && idx <= TIMEOUTS)
+    if( 0 <= idx && idx < TIMEOUTS)
         timeout_list[idx].timeout_state = TIMEOUT_UNUSED;
 }