In clock_ymdhms_to_secs(), rather than returning rubbish when presented with
a year before 1970 or a date beyond the time_t rollover, return -1 so callers can detect it. Callers which expect the function not to fail just get a different kind of rubbish from before.
This commit is contained in:
parent
74718675cc
commit
8f7f78f2f0
@ -1,4 +1,4 @@
|
|||||||
/* $NetBSD: clock_subr.c,v 1.9 2003/08/13 11:35:25 ragge Exp $ */
|
/* $NetBSD: clock_subr.c,v 1.10 2004/12/29 20:55:57 bjh21 Exp $ */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 1982, 1990, 1993
|
* Copyright (c) 1982, 1990, 1993
|
||||||
@ -84,7 +84,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <sys/cdefs.h>
|
#include <sys/cdefs.h>
|
||||||
__KERNEL_RCSID(0, "$NetBSD: clock_subr.c,v 1.9 2003/08/13 11:35:25 ragge Exp $");
|
__KERNEL_RCSID(0, "$NetBSD: clock_subr.c,v 1.10 2004/12/29 20:55:57 bjh21 Exp $");
|
||||||
|
|
||||||
#include <sys/param.h>
|
#include <sys/param.h>
|
||||||
#include <sys/systm.h>
|
#include <sys/systm.h>
|
||||||
@ -129,7 +129,7 @@ time_t
|
|||||||
clock_ymdhms_to_secs(dt)
|
clock_ymdhms_to_secs(dt)
|
||||||
struct clock_ymdhms *dt;
|
struct clock_ymdhms *dt;
|
||||||
{
|
{
|
||||||
time_t secs;
|
uint64_t secs;
|
||||||
int i, year, days;
|
int i, year, days;
|
||||||
|
|
||||||
year = dt->dt_year;
|
year = dt->dt_year;
|
||||||
@ -138,6 +138,7 @@ clock_ymdhms_to_secs(dt)
|
|||||||
* Compute days since start of time
|
* Compute days since start of time
|
||||||
* First from years, then from months.
|
* First from years, then from months.
|
||||||
*/
|
*/
|
||||||
|
if (year < POSIX_BASE_YEAR) return -1;
|
||||||
days = 0;
|
days = 0;
|
||||||
for (i = POSIX_BASE_YEAR; i < year; i++)
|
for (i = POSIX_BASE_YEAR; i < year; i++)
|
||||||
days += days_in_year(i);
|
days += days_in_year(i);
|
||||||
@ -150,11 +151,12 @@ clock_ymdhms_to_secs(dt)
|
|||||||
days += (dt->dt_day - 1);
|
days += (dt->dt_day - 1);
|
||||||
|
|
||||||
/* Add hours, minutes, seconds. */
|
/* Add hours, minutes, seconds. */
|
||||||
secs = ((days
|
secs = (((uint64_t)days
|
||||||
* 24 + dt->dt_hour)
|
* 24 + dt->dt_hour)
|
||||||
* 60 + dt->dt_min)
|
* 60 + dt->dt_min)
|
||||||
* 60 + dt->dt_sec;
|
* 60 + dt->dt_sec;
|
||||||
|
|
||||||
|
if ((time_t)secs != secs) return -1;
|
||||||
return (secs);
|
return (secs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user