]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
Make all time values signed longs instead of a mix of signed and unsigned, to avoid...
authorPeter McCurdy <peter.mccurdy@gmail.com>
Tue, 3 Mar 2015 19:52:36 +0000 (20:52 +0100)
committerRalf Habacker <ralf.habacker@freenet.de>
Wed, 4 Mar 2015 08:56:40 +0000 (09:56 +0100)
check_timeout() tries to use some unsigned long variables to
perform some intermediate calculations, then has to cast
back to signed long. As it happens, there's no real chance
of overflowing a signed long (it'll only happen if the current
time is within 49.7 days of rolling over, at which point you're
already pretty doomed), so we can make the calculations a bit
simpler, and also avoid the mixed-signedness arithmetic we'd
otherwise need to do.

Bug: https://bugs.freedesktop.org/attachment.cgi?id=18494
Reviewed-by: Ralf Habacker <ralf.habacker@freenet.de>
dbus/dbus-mainloop.c

index 7ff9dc2c78517a4525d89d7aa5b0ddd808ec3b4f..250196789e5e84d8d8f94bdb6160f7ab2dba1520 100644 (file)
@@ -54,8 +54,8 @@ struct DBusLoop
 typedef struct
 {
   DBusTimeout *timeout;
-  unsigned long last_tv_sec;
-  unsigned long last_tv_usec;
+  long last_tv_sec;
+  long last_tv_usec;
 } TimeoutCallback;
 
 #define TIMEOUT_CALLBACK(callback) ((TimeoutCallback*)callback)
@@ -421,15 +421,15 @@ _dbus_loop_remove_timeout (DBusLoop           *loop,
  * to do this.
  */
 static dbus_bool_t
-check_timeout (unsigned long    tv_sec,
-               unsigned long    tv_usec,
+check_timeout (long            tv_sec,
+               long            tv_usec,
                TimeoutCallback *tcb,
                int             *timeout)
 {
   long sec_remaining;
   long msec_remaining;
-  unsigned long expiration_tv_sec;
-  unsigned long expiration_tv_usec;
+  long expiration_tv_sec;
+  long expiration_tv_usec;
   long interval_seconds;
   long interval_milliseconds;
   int interval;
@@ -450,10 +450,7 @@ check_timeout (unsigned long    tv_sec,
     }
   
   sec_remaining = expiration_tv_sec - tv_sec;
-  /* need to force this to be signed, as it is intended to sometimes
-   * produce a negative result
-   */
-  msec_remaining = ((long) expiration_tv_usec - (long) tv_usec) / 1000L;
+  msec_remaining = (expiration_tv_usec - tv_usec) / 1000L;
 
 #if MAINLOOP_SPEW
   _dbus_verbose ("Interval is %ld seconds %ld msecs\n",
@@ -595,8 +592,8 @@ _dbus_loop_iterate (DBusLoop     *loop,
   timeout = -1;
   if (loop->timeout_count > 0)
     {
-      unsigned long tv_sec;
-      unsigned long tv_usec;
+      long tv_sec;
+      long tv_usec;
       
       _dbus_get_monotonic_time (&tv_sec, &tv_usec);
 
@@ -704,8 +701,8 @@ _dbus_loop_iterate (DBusLoop     *loop,
 
   if (loop->timeout_count > 0)
     {
-      unsigned long tv_sec;
-      unsigned long tv_usec;
+      long tv_sec;
+      long tv_usec;
 
       _dbus_get_monotonic_time (&tv_sec, &tv_usec);