From a49079c84c383b7b0b592edb266048ac14757492 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 #defined constants because of RHEL 8 compiler doesn't consider static const unsigned int to be constant. (cherry picked from commit b03e90e0d4d463a0b729d0333f99a3535a9f6a5c) --- bin/tools/mdig.c | 6 +++--- lib/isc/include/isc/time.h | 17 +++++++++-------- lib/isc/stdtime.c | 3 ++- lib/isc/time.c | 4 ++-- tests/libtest/dns.c | 4 ++-- 5 files changed, 18 insertions(+), 16 deletions(-) diff --git a/bin/tools/mdig.c b/bin/tools/mdig.c index b0ff68718a..20b43f9519 100644 --- a/bin/tools/mdig.c +++ b/bin/tools/mdig.c @@ -2187,9 +2187,9 @@ main(int argc, char *argv[]) { RUNCHECK(isc_time_now(&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 9c9da37378..912f27e3c5 100644 --- a/lib/isc/include/isc/time.h +++ b/lib/isc/include/isc/time.h @@ -21,14 +21,15 @@ #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 millisecond. */ - NS_PER_MS = 1000 * 1000, /*%< Nanoseconds per microsecond. */ - NS_PER_SEC = 1000 * 1000 * 1000, /*%< Nanoseconds per second. */ -}; +/* + * Define various time conversion constants. + */ +#define MS_PER_SEC 1000U +#define US_PER_MS 1000U +#define NS_PER_US 1000U +#define US_PER_SEC (1000U * 1000U) +#define NS_PER_MS (1000U * 1000U) +#define NS_PER_SEC (1000U * 1000U * 1000U) /*** *** Intervals diff --git a/lib/isc/stdtime.c b/lib/isc/stdtime.c index b7cec81842..359102c2ee 100644 --- a/lib/isc/stdtime.c +++ b/lib/isc/stdtime.c @@ -42,7 +42,8 @@ isc_stdtime_get(isc_stdtime_t *t) { FATAL_SYSERROR(errno, "clock_gettime()"); } - REQUIRE(ts.tv_sec > 0 && ts.tv_nsec >= 0 && ts.tv_nsec < NS_PER_SEC); + REQUIRE(ts.tv_sec > 0 && ts.tv_nsec >= 0 && + ts.tv_nsec < (long)NS_PER_SEC); *t = (isc_stdtime_t)ts.tv_sec; } diff --git a/lib/isc/time.c b/lib/isc/time.c index b03f377a3e..8a3a169583 100644 --- a/lib/isc/time.c +++ b/lib/isc/time.c @@ -133,7 +133,7 @@ time_now(isc_time_t *t, clockid_t clock) { 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); } @@ -175,7 +175,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 1432b34958..f3c2145867 100644 --- a/tests/libtest/dns.c +++ b/tests/libtest/dns.c @@ -198,8 +198,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); }