From b03e90e0d4d463a0b729d0333f99a3535a9f6a5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Wed, 14 Aug 2024 16:10:18 +0200 Subject: [PATCH] Change the NS_PER_SEC (and friends) from enum to static const New version of clang (19) has introduced a stricter checks when mixing integer (and float types) with enums. In this case, we used enum {} as C17 doesn't have constexpr yet. Change the time conversion constants to be static const unsigned int instead of enum values. --- bin/tools/mdig.c | 6 +++--- lib/isc/include/isc/time.h | 18 ++++++++++-------- lib/isc/stdtime.c | 3 ++- lib/isc/time.c | 5 +++-- tests/libtest/dns.c | 4 ++-- 5 files changed, 20 insertions(+), 16 deletions(-) diff --git a/bin/tools/mdig.c b/bin/tools/mdig.c index 3c9d9a9377..b5b14936c0 100644 --- a/bin/tools/mdig.c +++ b/bin/tools/mdig.c @@ -2176,9 +2176,9 @@ main(int argc, char *argv[]) { now = isc_time_now(); if (isc_time_seconds(&start) == isc_time_seconds(&now)) { - int us = US_PER_SEC - - (isc_time_nanoseconds(&now) / - NS_PER_US); + unsigned int us = US_PER_SEC - + (isc_time_nanoseconds(&now) / + NS_PER_US); if (us > US_PER_MS) { usleep(us - US_PER_MS); } diff --git a/lib/isc/include/isc/time.h b/lib/isc/include/isc/time.h index e9c2920bc9..3e944352b3 100644 --- a/lib/isc/include/isc/time.h +++ b/lib/isc/include/isc/time.h @@ -15,19 +15,21 @@ /*! \file */ +#include #include #include #include -enum { - MS_PER_SEC = 1000, /*%< Milliseonds per second. */ - US_PER_MS = 1000, /*%< Microseconds per millisecond. */ - US_PER_SEC = 1000 * 1000, /*%< Microseconds per second. */ - NS_PER_US = 1000, /*%< Nanoseconds per microsecond. */ - NS_PER_MS = 1000 * 1000, /*%< Nanoseconds per millisecond. */ - NS_PER_SEC = 1000 * 1000 * 1000, /*%< Nanoseconds per second. */ -}; +/* + * Define various time conversion constants. + */ +static const unsigned int MS_PER_SEC = 1000; +static const unsigned int US_PER_MS = 1000; +static const unsigned int NS_PER_US = 1000; +static const unsigned int US_PER_SEC = 1000 * 1000; +static const unsigned int NS_PER_MS = 1000 * 1000; +static const unsigned int NS_PER_SEC = 1000 * 1000 * 1000; /* * ISC_FORMATHTTPTIMESTAMP_SIZE needs to be 30 in C locale and potentially diff --git a/lib/isc/stdtime.c b/lib/isc/stdtime.c index 6f3ef49d78..0bd4c7ad6d 100644 --- a/lib/isc/stdtime.c +++ b/lib/isc/stdtime.c @@ -40,7 +40,8 @@ isc_stdtime_now(void) { if (clock_gettime(CLOCKSOURCE, &ts) == -1) { FATAL_SYSERROR(errno, "clock_gettime()"); } - INSIST(ts.tv_sec > 0 && ts.tv_nsec >= 0 && ts.tv_nsec < NS_PER_SEC); + INSIST(ts.tv_sec > 0 && ts.tv_nsec >= 0 && + ts.tv_nsec < (long)NS_PER_SEC); return ((isc_stdtime_t)ts.tv_sec); } diff --git a/lib/isc/time.c b/lib/isc/time.c index 29285937ea..cff1764554 100644 --- a/lib/isc/time.c +++ b/lib/isc/time.c @@ -86,7 +86,8 @@ time_now(clockid_t clock) { struct timespec ts; RUNTIME_CHECK(clock_gettime(clock, &ts) == 0); - INSIST(ts.tv_sec >= 0 && ts.tv_nsec >= 0 && ts.tv_nsec < NS_PER_SEC); + INSIST(ts.tv_sec >= 0 && ts.tv_nsec >= 0 && + ts.tv_nsec < (long)NS_PER_SEC); /* * Ensure the tv_sec value fits in t->seconds. @@ -135,7 +136,7 @@ isc_time_nowplusinterval(isc_time_t *t, const isc_interval_t *i) { return (ISC_R_UNEXPECTED); } - if (ts.tv_sec < 0 || ts.tv_nsec < 0 || ts.tv_nsec >= NS_PER_SEC) { + if (ts.tv_sec < 0 || ts.tv_nsec < 0 || ts.tv_nsec >= (long)NS_PER_SEC) { return (ISC_R_UNEXPECTED); } diff --git a/tests/libtest/dns.c b/tests/libtest/dns.c index ba5aa995c3..e4ba0e39a6 100644 --- a/tests/libtest/dns.c +++ b/tests/libtest/dns.c @@ -203,8 +203,8 @@ void dns_test_nap(uint32_t usec) { struct timespec ts; - ts.tv_sec = usec / 1000000; - ts.tv_nsec = (usec % 1000000) * 1000; + ts.tv_sec = usec / (long)US_PER_SEC; + ts.tv_nsec = (usec % (long)US_PER_SEC) * (long)NS_PER_US; nanosleep(&ts, NULL); }