Browse code

core: time_t is unsigned, handle integer underflow correctly

When the processing of the timer code takes longer than the interval, don't
sleep for 4Gs.

Alex Hermann authored on 09/10/2014 15:54:20
Showing 1 changed files
... ...
@@ -222,17 +222,20 @@ int fork_sync_timer(int child_id, char* desc, int make_sock,
222 222
 	if (pid<0) return -1;
223 223
 	if (pid==0){
224 224
 		/* child */
225
-		ts2 = interval*1000; /* miliseconds */
225
+		interval *= 1000;  /* miliseconds */
226
+		ts2 = interval;
226 227
 		if (cfg_child_init()) return -1;
227 228
 		for(;;){
228
-			if(ts2>0) sleep_us(ts2*1000); /* microseconds sleep */
229
-			else sleep_us(1000); /* 1 milisecond sleep to catch up */
229
+			if (ts2>interval)
230
+				sleep_us(1000);    /* 1 milisecond sleep to catch up */
231
+			else
232
+				sleep_us(ts2*1000); /* microseconds sleep */
230 233
 			ts1 = get_ticks_raw();
231 234
 			cfg_update();
232 235
 			f(TICKS_TO_S(ts1), param); /* ticks in sec for compatibility with old
233 236
 									  timers */
234
-			/* convert to mili-seconds and adjust the next sleep duration */
235
-			ts2 = interval*1000 - TICKS_TO_MS(get_ticks_raw()) + TICKS_TO_MS(ts1);
237
+			/* adjust the next sleep duration */
238
+			ts2 = interval - TICKS_TO_MS(get_ticks_raw()) + TICKS_TO_MS(ts1);
236 239
 		}
237 240
 	}
238 241
 	/* parent */
... ...
@@ -271,8 +274,10 @@ int fork_sync_utimer(int child_id, char* desc, int make_sock,
271 271
 		ts2 = uinterval;
272 272
 		if (cfg_child_init()) return -1;
273 273
 		for(;;){
274
-			if(ts2>0) sleep_us(uinterval);
275
-			else sleep_us(1);
274
+			if(ts2>uinterval)
275
+				sleep_us(1);
276
+			else
277
+				sleep_us(ts2);
276 278
 			ts1 = get_ticks_raw();
277 279
 			cfg_update();
278 280
 			f(TICKS_TO_MS(ts1), param); /* ticks in mili-seconds */