]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Change the NS_PER_SEC (and friends) from enum to static const
authorOndřej Surý <ondrej@isc.org>
Wed, 14 Aug 2024 14:10:18 +0000 (16:10 +0200)
committerOndřej Surý <ondrej@isc.org>
Mon, 19 Aug 2024 09:08:55 +0000 (09:08 +0000)
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
lib/isc/include/isc/time.h
lib/isc/stdtime.c
lib/isc/time.c
tests/libtest/dns.c

index 3c9d9a937758497bd6ad7a10b36336bc88633045..b5b14936c0f7affbd27fa396172f996fea571645 100644 (file)
@@ -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);
                                }
index e9c2920bc9be6740410a8febaada5507156a519a..3e944352b3b52ef7a8425ae0b32c32f000c23698 100644 (file)
 
 /*! \file */
 
+#include <inttypes.h>
 #include <time.h>
 
 #include <isc/lang.h>
 #include <isc/types.h>
 
-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
index 6f3ef49d78c26bfab51876c922c04a4e029326fd..0bd4c7ad6d47e8b7a7e4693f3b071a587b2574c6 100644 (file)
@@ -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);
 }
index 29285937ead23bdb42d9eb2a08f729be0d6ce43a..cff17645548ca6512a2a36b4518236bdcedf2cbf 100644 (file)
@@ -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);
        }
 
index ba5aa995c39993fe6c88837f3ba2d71d8803ed0a..e4ba0e39a67eeda78b6810ec6febb16525528337 100644 (file)
@@ -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);
 }