dovecot-1.2: Avoid integer overflows when calculating with very ...

dovecot at dovecot.org dovecot at dovecot.org
Sun Nov 30 04:20:22 EET 2008


details:   http://hg.dovecot.org/dovecot-1.2/rev/16ca1b36e6c3
changeset: 8503:16ca1b36e6c3
user:      Timo Sirainen <tss at iki.fi>
date:      Sun Nov 30 04:20:18 2008 +0200
description:
Avoid integer overflows when calculating with very large timeout values.

diffstat:

1 file changed, 9 insertions(+), 4 deletions(-)
src/lib/ioloop.c |   13 +++++++++----

diffs (34 lines):

diff -r 570927dfc769 -r 16ca1b36e6c3 src/lib/ioloop.c
--- a/src/lib/ioloop.c	Sun Nov 30 04:03:20 2008 +0200
+++ b/src/lib/ioloop.c	Sun Nov 30 04:20:18 2008 +0200
@@ -190,6 +190,8 @@ static int timeout_get_wait_time(struct 
 		tv_r->tv_sec = tv_now->tv_sec;
 		tv_r->tv_usec = tv_now->tv_usec;
 	}
+	i_assert(tv_r->tv_sec > 0);
+	i_assert(timeout->next_run.tv_sec > 0);
 
 	tv_r->tv_sec = timeout->next_run.tv_sec - tv_r->tv_sec;
 	tv_r->tv_usec = timeout->next_run.tv_usec - tv_r->tv_usec;
@@ -198,14 +200,17 @@ static int timeout_get_wait_time(struct 
 		tv_r->tv_usec += 1000000;
 	}
 
-	/* round wait times up to next millisecond */
-	ret = tv_r->tv_sec * 1000 + (tv_r->tv_usec + 999) / 1000;
-	if (ret <= 0) {
+	if (tv_r->tv_sec < 0 || (tv_r->tv_sec == 0 && tv_r->tv_usec < 1000)) {
 		tv_r->tv_sec = 0;
 		tv_r->tv_usec = 0;
 		return 0;
 	}
-	i_assert(tv_r->tv_sec >= 0 && tv_r->tv_usec >= 0);
+	if (tv_r->tv_sec > INT_MAX/1000-1)
+		tv_r->tv_sec = INT_MAX/1000-1;
+
+	/* round wait times up to next millisecond */
+	ret = tv_r->tv_sec * 1000 + (tv_r->tv_usec + 999) / 1000;
+	i_assert(ret > 0 && tv_r->tv_sec >= 0 && tv_r->tv_usec >= 0);
 	return ret;
 }
 


More information about the dovecot-cvs mailing list