[ntpsec-main commit] Fix a type clash found by Coverity.
Eric S. Raymond
esr at ntpsec.org
Sat Nov 7 15:43:36 UTC 2015
Module: ntpsec-main
Branch: master
Commit: 4ac004b10abc244c91cb1a67fad70d0c1f79b9af
Changeset: http://git.ntpsec.org//commit/?id=4ac004b10abc244c91cb1a67fad70d0c1f79b9af
Author: Eric S. Raymond <esr at thyrsus.com>
Date: Sat Nov 7 10:40:35 2015 -0500
Fix a type clash found by Coverity.
This involved reverting slightly erroneous booleanization in the calendar code.
There was no practical effect of the error except when a date calculation
overflowed, which is why it was hard to spot by eyeball.
---
include/ntp_calendar.h | 8 ++++----
libntp/ntp_calendar.c | 21 +++++++++++----------
ntpd/refclock_nmea.c | 3 ++-
3 files changed, 17 insertions(+), 15 deletions(-)
diff --git a/include/ntp_calendar.h b/include/ntp_calendar.h
index 9aabef0..8f89374 100644
--- a/include/ntp_calendar.h
+++ b/include/ntp_calendar.h
@@ -222,7 +222,7 @@ ntpcal_date_to_rd(const struct calendar * /* jt */);
* for regular years and a non-zero value for leap years.
*/
extern ntpcal_split
-ntpcal_split_eradays(int32_t /* days */, bool * /* isleapyear */);
+ntpcal_split_eradays(int32_t /* days */, int32_t * /* isleapyear */);
/*
* Given a number of elapsed days in a year and a leap year indicator,
@@ -235,10 +235,10 @@ ntpcal_split_yeardays(int32_t /* eyd */, bool /* isleapyear */);
/*
* Convert a RataDie number into the date part of a 'struct
- * calendar'. Return false if the year is regular year, true if the year is
- * a leap year.
+ * calendar'. Return 0 if the year is regular year, 1 if the year is
+ * a leap year, -1 if the calculation overflowed.
*/
-extern bool
+extern int
ntpcal_rd_to_date(struct calendar * /* jt */, int32_t /* rd */);
/*
diff --git a/libntp/ntp_calendar.c b/libntp/ntp_calendar.c
index d1e2c04..bf63e44 100644
--- a/libntp/ntp_calendar.c
+++ b/libntp/ntp_calendar.c
@@ -511,7 +511,7 @@ priv_timesplit(
ntpcal_split
ntpcal_split_eradays(
int32_t days,
- bool *isleapyear
+ int32_t *isleapyear
)
{
ntpcal_split res;
@@ -594,20 +594,21 @@ ntpcal_split_yeardays(
/*
*---------------------------------------------------------------------
* Convert a RD into the date part of a 'struct calendar'.
+ * Returns -1 on calculation overflow.
*---------------------------------------------------------------------
*/
-bool
+int
ntpcal_rd_to_date(
struct calendar *jd,
int32_t rd
)
{
ntpcal_split split;
- bool leaps;
- bool retv;
+ int32_t leaps;
+ int32_t retv;
- leaps = false;
- retv = false;
+ leaps = 0;
+ retv = 0;
/* Get day-of-week first. Since rd is signed, the remainder can
* be in the range [-6..+6], but the assignment to an unsigned
* variable maps the negative values to positive values >=7.
@@ -620,7 +621,7 @@ ntpcal_rd_to_date(
jd->weekday += 7;
split = ntpcal_split_eradays(rd - 1, &leaps);
- retv = leaps;
+ retv = (int)leaps;
/* get year and day-of-year */
jd->year = (uint16_t)split.hi + 1;
if (jd->year != split.hi + 1) {
@@ -649,7 +650,7 @@ ntpcal_rd_to_tm(
)
{
ntpcal_split split;
- bool leaps;
+ int32_t leaps;
leaps = 0;
/* get day-of-week first */
@@ -667,7 +668,7 @@ ntpcal_rd_to_tm(
utm->tm_mon = split.hi; /* 0-based */
utm->tm_mday = split.lo + 1; /* 1-based */
- return leaps;
+ return leaps != 0;
}
/*
@@ -1055,7 +1056,7 @@ ntpcal_rd_to_mstart(
)
{
ntpcal_split split;
- bool leaps;
+ int32_t leaps;
split = ntpcal_split_eradays(rd - 1, &leaps);
split = ntpcal_split_yeardays(split.lo, leaps);
diff --git a/ntpd/refclock_nmea.c b/ntpd/refclock_nmea.c
index b3f954a..64e9ff3 100644
--- a/ntpd/refclock_nmea.c
+++ b/ntpd/refclock_nmea.c
@@ -1631,7 +1631,7 @@ parse_weekdata(
* The function updates the calendar structure it also uses as
* input to fetch the time from.
*
- * returns 1 on success, 0 on failure
+ * returns true on success, false on failure
* -------------------------------------------------------------------
*/
static bool
@@ -1656,6 +1656,7 @@ unfold_day(
ntpcal_date_to_daysec(jd),
SECSPERDAY);
rec_ds.hi += ntpcal_daysec_to_date(jd, rec_ds.lo);
+ /* -1 return means calculation overflowed */
return (ntpcal_rd_to_date(jd, rec_ds.hi + DAY_NTP_STARTS) >= 0);
}
More information about the vc
mailing list