[9.20] fix: dev: 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.

Closes #4845

Backport of MR !9313

Merge branch 'backport-4845-change-NS_PER_SEC-type-from-enum-to-integer-9.20' into 'bind-9.20'

See merge request isc-projects/bind9!9339
This commit is contained in:
Ondřej Surý
2024-08-19 09:54:27 +00:00
6 changed files with 27 additions and 16 deletions

View File

@@ -2177,9 +2177,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);
}

View File

@@ -104,3 +104,9 @@
#else
#define ISC_ATTR_UNUSED __attribute__((__unused__))
#endif
#if __STDC_VERSION__ >= 202311L
#define ISC_CONSTEXPR constexpr
#else
#define ISC_CONSTEXPR static const
#endif

View File

@@ -15,19 +15,22 @@
/*! \file */
#include <inttypes.h>
#include <time.h>
#include <isc/attributes.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.
*/
ISC_CONSTEXPR unsigned int MS_PER_SEC = 1000;
ISC_CONSTEXPR unsigned int US_PER_MS = 1000;
ISC_CONSTEXPR unsigned int NS_PER_US = 1000;
ISC_CONSTEXPR unsigned int US_PER_SEC = 1000 * 1000;
ISC_CONSTEXPR unsigned int NS_PER_MS = 1000 * 1000;
ISC_CONSTEXPR unsigned int NS_PER_SEC = 1000 * 1000 * 1000;
/*
* ISC_FORMATHTTPTIMESTAMP_SIZE needs to be 30 in C locale and potentially

View 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);
}

View 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);
}

View 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);
}