From ce4cf8b6a0c8610cc4df096184c006e7cfdd044b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Gomez?= Date: Sun, 9 Jan 2022 21:02:48 +0100 Subject: [PATCH] sanitize: add bip_clock_gettime util function --- src/connection.c | 14 +++----------- src/util.c | 11 +++++++++++ src/util.h | 1 + 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/connection.c b/src/connection.c index fdbab10..3ffe5be 100644 --- a/src/connection.c +++ b/src/connection.c @@ -874,7 +874,7 @@ static int cn_want_write(connection_t *cn) list_t *wait_event(list_t *cn_list, time_t *msec, int *nc) { fd_set fds_read, fds_write, fds_except; - int maxfd = -1, err, errtime; + int maxfd = -1, err; list_t *cn_newdata; list_iterator_t it; struct timeval tv; @@ -945,11 +945,7 @@ list_t *wait_event(list_t *cn_list, time_t *msec, int *nc) mylog(LOG_DEBUGTOOMUCH, "msec: %d, sec: %d, usec: %d", *msec, tv.tv_sec, tv.tv_usec); - errtime = clock_gettime(CLOCK_MONOTONIC, &btv); - if (errtime != 0) { - fatal("clock_gettime: %s", strerror(errno)); - } - + bip_clock_gettime(CLOCK_MONOTONIC, &btv); err = select(maxfd + 1, &fds_read, &fds_write, &fds_except, &tv); if (err == 0) { @@ -962,11 +958,7 @@ list_t *wait_event(list_t *cn_list, time_t *msec, int *nc) tv.tv_sec, tv.tv_usec); } - errtime = clock_gettime(CLOCK_MONOTONIC, &etv); - if (errtime != 0) { - fatal("clock_gettime: %s", strerror(errno)); - } - + bip_clock_gettime(CLOCK_MONOTONIC, &etv); if (etv.tv_sec < btv.tv_sec) mylog(LOG_ERROR, "Time rewinded ! not touching interval"); else { diff --git a/src/util.c b/src/util.c index f86f099..d6e3c26 100644 --- a/src/util.c +++ b/src/util.c @@ -158,6 +158,17 @@ end: return res; } +void bip_clock_gettime(clockid_t clockid, struct timespec *tp) +{ + int err = clock_gettime(clockid, tp); + + if (err != 0) + fatal("clock_gettime: %s", strerror(errno)); + + if (tp->tv_sec < 0 || tp->tv_nsec < 0) + fatal("clock_gettime returned negative time"); +} + /* * ::= { | | } * ::= '-' | '[' | ']' | '\' | '`' | '^' | '{' | '}' diff --git a/src/util.h b/src/util.h index 3ddd3ba..bb84710 100644 --- a/src/util.h +++ b/src/util.h @@ -178,6 +178,7 @@ void *bip_realloc(void *ptr, size_t size); char *bip_strdup(const char *str); char *bip_strcat_fit(size_t *remaining, char *str, const char *str2); char *bip_strcatf_fit(size_t *remaining, char *str, const char *str2, ...); +void bip_clock_gettime(clockid_t clockid, struct timespec *tp); #define array_each(a, idx, ptr) for ((idx) = 0; \ (idx) < (a)->elemc && (((ptr) = array_get((a), (idx))) || 1); \ (idx)++)