Skip to contentSkip to conversation

tst_timer.h: Make time conversions saturated

RFC PATCH New Cyril Hrubis 1 participant 2 messages Aug 1, 2017 – Aug 2, 2017

lore
Share & tools
lore
#0 lore raw

RFC PATCH 2/2 tst_timer.h: Make time conversions saturated

The conversions of struct timeval and struct timespec to us and ms are now saturated so instead of overflowing/underflowing long long these return LLONG_MAX/LLONG_MIN.

This commit also adds tst_us_to_timespec(), tst_ms_to_timespec() and newlib_tests/tst_timer test to assert that the saturated conversion actually works.

---
 include/tst_timer.h          | 129 ++++++++++++++++++++++++++++++++++++++++---
 lib/newlib_tests/.gitignore  |   1 +
 lib/newlib_tests/tst_timer.c | 126 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 248 insertions(+), 8 deletions(-)
 create mode 100644 lib/newlib_tests/tst_timer.c
include/tst_timer.h +121 -8
index f294b0eb7..b1f595bd4 100644
@@ -33,37 +33,124 @@
 
 #include <sys/time.h>
 #include <time.h>
+#include <limits.h>
+
+#define LLONG_MAX_USEC_OVERFLOW(sec, usec) \
+	(sizeof(time_t) == 8 && ((sec == LLONG_MAX/1000000 && usec > LLONG_MAX%1000000) \
+	 || (sec > LLONG_MAX/1000000)))
+
+#define LLONG_MIN_USEC_UNDERFLOW(sec, usec) \
+	((sec == LLONG_MIN/1000000 && usec < LLONG_MIN%1000000) \
+	 || (sec < LLONG_MIN/1000000))
+
+#define LLONG_MAX_MSEC_OVERFLOW(sec, msec) \
+	((sec == LLONG_MAX/1000 && msec > LLONG_MAX%1000) \
+	 || (sec > LLONG_MAX/1000))
+
+#define LLONG_MIN_MSEC_UNDERFLOW(sec, msec) \
+	((sec == LLONG_MIN/1000 && msec < LLONG_MIN%1000) \
+	 || (sec < LLONG_MIN/1000))
+
+/*
+ * Normalize timespec so that abs(tv_nsec) is smaller than 1s.
+ */
+static inline struct timespec tst_normalize_timespec(struct timespec t)
+{
+	t.tv_sec += t.tv_nsec / 1000000000;
+	t.tv_nsec %= 1000000000;
+
+	return t;
+}
+
+/*
+ * Normalize timeval so that abs(tv_usec) is smaller than 1s.
+ */
+static inline struct timeval tst_normalize_timeval(struct timeval t)
+{
+	t.tv_sec += t.tv_usec / 1000000;
+	t.tv_usec %= 1000000;
+
+	return t;
+}
 
 /*
- * Converts timespec to microseconds.
+ * Saturated conversion from timespec to microseconds.
  */
 static inline long long tst_timespec_to_us(struct timespec t)
 {
-	return t.tv_sec * 1000000 + (t.tv_nsec + 500) / 1000;
+	t = tst_normalize_timespec(t);
+
+	long long usec = (t.tv_nsec + 500) / 1000;
+
+#if LONG_MAX > 0xffffffff
+	if (LLONG_MAX_USEC_OVERFLOW(t.tv_sec, usec))
+		return LLONG_MAX;
+
+	if (LLONG_MIN_USEC_UNDERFLOW(t.tv_sec, usec))
+		return LLONG_MIN;
+#endif
+
+	return t.tv_sec * 1000000 + usec;
 }
 
 /*
- * Converts timespec to miliseconds.
+ * Saturated conversion from timespec to miliseconds.
  */
 static inline long long tst_timespec_to_ms(struct timespec t)
 {
-	return t.tv_sec * 1000 + (t.tv_nsec + 500000) / 1000000;
+	t = tst_normalize_timespec(t);
+
+	long long msec = (t.tv_nsec + 500000) / 1000000;
+
+#if LONG_MAX > 0xffffffff
+	if (LLONG_MAX_MSEC_OVERFLOW(t.tv_sec, msec))
+		return LLONG_MAX;
+
+	if (LLONG_MIN_MSEC_UNDERFLOW(t.tv_sec, msec))
+		return LLONG_MIN;
+#endif
+
+	return t.tv_sec * 1000 + msec;
 }
 
 /*
- * Converts timeval to microseconds.
+ * Saturated conversion from timeval to microseconds.
  */
 static inline long long tst_timeval_to_us(struct timeval t)
 {
-	return t.tv_sec * 1000000 + t.tv_usec;
+	t = tst_normalize_timeval(t);
+
+	long long usec = t.tv_usec;
+
+#if LONG_MAX > 0xffffffff
+	if (LLONG_MAX_USEC_OVERFLOW(t.tv_sec, usec))
+		return LLONG_MAX;
+
+	if (LLONG_MIN_USEC_UNDERFLOW(t.tv_sec, usec))
+		return LLONG_MIN;
+#endif
+
+	return t.tv_sec * 1000000 + usec;
 }
 
 /*
- * Converts timeval to miliseconds.
+ * Saturated conversion from timeval to miliseconds.
  */
 static inline long long tst_timeval_to_ms(struct timeval t)
 {
-	return t.tv_sec * 1000 + (t.tv_usec + 500) / 1000;
+	t = tst_normalize_timeval(t);
+
+	long long msec = (t.tv_usec + 500) / 1000;
+
+#if LONG_MAX > 0xffffffff
+	if (LLONG_MAX_MSEC_OVERFLOW(t.tv_sec, msec))
+		return LLONG_MAX;
+
+	if (LLONG_MIN_MSEC_UNDERFLOW(t.tv_sec, msec))
+		return LLONG_MIN;
+#endif
+
+	return t.tv_sec * 1000 + msec;
 }
 
 /*
@@ -93,6 +180,32 @@ static inline struct timeval tst_us_to_timeval(long long us)
 }
 
 /*
+ * Converts ms to struct timespec
+ */
+static inline struct timespec tst_ms_to_timespec(long long ms)
+{
+	struct timespec ret;
+
+	ret.tv_sec = ms / 1000;
+	ret.tv_nsec = (ms % 1000) * 1000000;
+
+	return ret;
+}
+
+/*
+ * Converts us to struct timespec
+ */
+static inline struct timespec tst_us_to_timespec(long long us)
+{
+	struct timespec ret;
+
+	ret.tv_sec = us / 1000000;
+	ret.tv_nsec = (us % 1000000) * 1000;
+
+	return ret;
+}
+
+/*
  * Comparsions
  */
 static inline int tst_timespec_lt(struct timespec t1, struct timespec t2)
lib/newlib_tests/.gitignore +1 -0
index 8e120074e..0405c3e2e 100644
@@ -15,3 +15,4 @@ test14
 tst_device
 tst_safe_fileops
 tst_res_hexd
+tst_timer
lib/newlib_tests/tst_timer.c new file +126 -0

126 lines not on this page · raw message

Signature · 1 line
2.13.0
#1 lore raw

RFC PATCH 2/2 tst_timer.h: Make time conversions saturated

Hi!

Quoted 7 lines

The conversions of struct timeval and struct timespec to us and ms are now saturated so instead of overflowing/underflowing long long these return LLONG_MAX/LLONG_MIN.

This commit also adds tst_us_to_timespec(), tst_ms_to_timespec() and newlib_tests/tst_timer test to assert that the saturated conversion actually works.

Argh, I've send a wrong patch, sorry.

But I wanted to ping this one anyway.

Signature · 2 lines
Cyril Hrubis
[email protected]