Module Name:    src
Committed By:   drochner
Date:           Wed Jan 26 19:15:13 UTC 2011

Modified Files:
        src/sys/kern: subr_time.c

Log Message:
fix and cleanup for tvtohz():
-assume (KASSERT) that the timeval given is normalized, and remove
 some partial fixup which I don't see what it is good for
 (I'm ready to back that out if someone tells a reason)
-catch overflows due to conversion of time_t (from tv_sec) to
 integer -- this function doesn't do 64-bit arithmetics (which makes
 sense because relative times which don't fit into 32 bits can be
 considered nonsense here), and before a huge tv_sec could lead to
 a zero hz result, violating the caller's assumptions (in particular
 trigger a diagnostic panic in abstimeout2timo())


To generate a diff of this commit:
cvs rdiff -u -r1.7 -r1.8 src/sys/kern/subr_time.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/kern/subr_time.c
diff -u src/sys/kern/subr_time.c:1.7 src/sys/kern/subr_time.c:1.8
--- src/sys/kern/subr_time.c:1.7	Mon Apr 26 16:26:11 2010
+++ src/sys/kern/subr_time.c	Wed Jan 26 19:15:13 2011
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_time.c,v 1.7 2010/04/26 16:26:11 rmind Exp $	*/
+/*	$NetBSD: subr_time.c,v 1.8 2011/01/26 19:15:13 drochner Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1989, 1993
@@ -33,7 +33,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: subr_time.c,v 1.7 2010/04/26 16:26:11 rmind Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_time.c,v 1.8 2011/01/26 19:15:13 drochner Exp $");
 
 #include <sys/param.h>
 #include <sys/kernel.h>
@@ -89,12 +89,15 @@
 	sec = tv->tv_sec;
 	usec = tv->tv_usec;
 
-	if (usec < 0) {
-		sec--;
-		usec += 1000000;
-	}
+	KASSERT(usec >= 0 && usec < 1000000);
+
+	/* catch overflows in conversion time_t->int */
+	if (tv->tv_sec > INT_MAX)
+		return INT_MAX;
+	if (tv->tv_sec < 0)
+		return 0;
 
-	if (sec < 0 || (sec == 0 && usec <= 0)) {
+	if (sec < 0 || (sec == 0 && usec == 0)) {
 		/*
 		 * Would expire now or in the past.  Return 0 ticks.
 		 * This is different from the legacy tvhzto() interface,

Reply via email to