Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src Avoid overflowing the "year" value by making the field uint6...



details:   https://anonhg.NetBSD.org/src/rev/8e2aae6f4d52
branches:  trunk
changeset: 802250:8e2aae6f4d52
user:      martin <martin%NetBSD.org@localhost>
date:      Sun Sep 07 11:50:23 2014 +0000

description:
Avoid overflowing the "year" value by making the field uint64_t. Adapt
arguments and local variables accordingly.
This now fixes PR 49144 for real.

diffstat:

 sys/dev/clock_subr.c          |  26 ++++++++++++++++++--------
 sys/dev/clock_subr.h          |   6 +++---
 tools/compat/dev/clock_subr.h |   6 +++---
 3 files changed, 24 insertions(+), 14 deletions(-)

diffs (138 lines):

diff -r 50c39231f982 -r 8e2aae6f4d52 sys/dev/clock_subr.c
--- a/sys/dev/clock_subr.c      Sun Sep 07 09:10:09 2014 +0000
+++ b/sys/dev/clock_subr.c      Sun Sep 07 11:50:23 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: clock_subr.c,v 1.21 2014/09/06 18:04:28 martin Exp $   */
+/*     $NetBSD: clock_subr.c,v 1.22 2014/09/07 11:50:23 martin Exp $   */
 
 /*
  * Copyright (c) 1988 University of Utah.
@@ -50,18 +50,20 @@
 
 #ifdef _KERNEL
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: clock_subr.c,v 1.21 2014/09/06 18:04:28 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: clock_subr.c,v 1.22 2014/09/07 11:50:23 martin Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
+#include <sys/errno.h>
 #else /* ! _KERNEL */
 #include <string.h>
 #include <time.h>
+#include <errno.h>
 #endif /* ! _KERNEL */
 
 #include <dev/clock_subr.h>
 
-static inline int leapyear(int year);
+static inline int leapyear(uint64_t year);
 #define FEBRUARY       2
 #define        days_in_year(a)         (leapyear(a) ? 366 : 365)
 #define        days_in_month(a)        (month_days[(a) - 1])
@@ -92,10 +94,13 @@
  * It is otherwise equivalent.
  */
 static inline int
-leapyear(int year)
+leapyear(uint64_t year)
 {
        int rv = 0;
 
+       if (year < 1969)
+               return EINVAL;
+
        if ((year & 3) == 0) {
                rv = 1;
                if ((year % 100) == 0) {
@@ -110,8 +115,7 @@
 time_t
 clock_ymdhms_to_secs(struct clock_ymdhms *dt)
 {
-       uint64_t secs;
-       int i, year, days;
+       uint64_t secs, i, year, days;
 
        year = dt->dt_year;
 
@@ -167,13 +171,17 @@
        return secs;
 }
 
-void
+int
 clock_secs_to_ymdhms(time_t secs, struct clock_ymdhms *dt)
 {
-       int i, leap;
+       int leap;
+       uint64_t i;
        time_t days;
        time_t rsec;    /* remainder seconds */
 
+       if (secs < 0)
+               return EINVAL;
+
        days = secs / SECDAY;
        rsec = secs % SECDAY;
 
@@ -225,4 +233,6 @@
        dt->dt_min  = rsec / 60;
        rsec = rsec % 60;
        dt->dt_sec  = rsec;
+
+       return 0;
 }
diff -r 50c39231f982 -r 8e2aae6f4d52 sys/dev/clock_subr.h
--- a/sys/dev/clock_subr.h      Sun Sep 07 09:10:09 2014 +0000
+++ b/sys/dev/clock_subr.h      Sun Sep 07 11:50:23 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: clock_subr.h,v 1.21 2009/12/12 15:10:34 tsutsui Exp $  */
+/*     $NetBSD: clock_subr.h,v 1.22 2014/09/07 11:50:23 martin Exp $   */
 
 /*-
  * Copyright (c) 1996 The NetBSD Foundation, Inc.
@@ -36,7 +36,7 @@
  * "POSIX time" to/from "YY/MM/DD/hh/mm/ss"
  */
 struct clock_ymdhms {
-       u_short dt_year;
+       uint64_t dt_year;
        u_char dt_mon;
        u_char dt_day;
        u_char dt_wday; /* Day of week */
@@ -46,7 +46,7 @@
 };
 
 time_t clock_ymdhms_to_secs(struct clock_ymdhms *);
-void   clock_secs_to_ymdhms(time_t, struct clock_ymdhms *);
+int    clock_secs_to_ymdhms(time_t, struct clock_ymdhms *);
 
 /*
  * BCD to binary and binary to BCD.
diff -r 50c39231f982 -r 8e2aae6f4d52 tools/compat/dev/clock_subr.h
--- a/tools/compat/dev/clock_subr.h     Sun Sep 07 09:10:09 2014 +0000
+++ b/tools/compat/dev/clock_subr.h     Sun Sep 07 11:50:23 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: clock_subr.h,v 1.1 2014/09/01 07:48:16 martin Exp $    */
+/*     $NetBSD: clock_subr.h,v 1.2 2014/09/07 11:50:23 martin Exp $    */
 
 /*-
  * Copyright (c) 1996 The NetBSD Foundation, Inc.
@@ -40,7 +40,7 @@
  * "POSIX time" to/from "YY/MM/DD/hh/mm/ss"
  */
 struct clock_ymdhms {
-       u_short dt_year;
+       uint64_t dt_year;
        u_char dt_mon;
        u_char dt_day;
        u_char dt_wday; /* Day of week */
@@ -50,7 +50,7 @@
 };
 
 time_t clock_ymdhms_to_secs(struct clock_ymdhms *);
-void   clock_secs_to_ymdhms(time_t, struct clock_ymdhms *);
+int    clock_secs_to_ymdhms(time_t, struct clock_ymdhms *);
 
 /* Some handy constants. */
 #define SECDAY         (24 * 60 * 60)



Home | Main Index | Thread Index | Old Index