From a2a5af97451cc08b8a6e2fdad3372af9a33f9a94 Mon Sep 17 00:00:00 2001 From: Brian Gianforcaro Date: Mon, 12 Jul 2021 00:20:02 -0700 Subject: [PATCH] AK: Restrict timespec comparison operator overloads in AK::Time The previous implementation was too generic, and would cause conflicting operator overload errors when included in certain code paths. Fix this by restricting the template parameters to types which have the same member names as `struct timespec`. --- AK/Time.h | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/AK/Time.h b/AK/Time.h index 30733b6fb6..1a7447dc36 100644 --- a/AK/Time.h +++ b/AK/Time.h @@ -15,6 +15,14 @@ struct timeval; struct timespec; +// Concept to detect types which look like timespec without requiring the type. +template +concept TimeSpecType = requires(T t) +{ + t.tv_sec; + t.tv_nsec; +}; + namespace AK { // Month and day start at 1. Month must be >= 1 and <= 12. @@ -241,38 +249,39 @@ inline void timespec_to_timeval(const TimespecType& ts, TimevalType& tv) tv.tv_usec = ts.tv_nsec / 1000; } -template -inline bool operator>=(const TimespecType& a, const TimespecType& b) +template +inline bool operator>=(const T& a, const T& b) { return a.tv_sec > b.tv_sec || (a.tv_sec == b.tv_sec && a.tv_nsec >= b.tv_nsec); } -template -inline bool operator>(const TimespecType& a, const TimespecType& b) +template +inline bool operator>(const T& a, const T& b) { return a.tv_sec > b.tv_sec || (a.tv_sec == b.tv_sec && a.tv_nsec > b.tv_nsec); } -template -inline bool operator<(const TimespecType& a, const TimespecType& b) +template +inline bool operator<(const T& a, const T& b) { return a.tv_sec < b.tv_sec || (a.tv_sec == b.tv_sec && a.tv_nsec < b.tv_nsec); } -template -inline bool operator<=(const TimespecType& a, const TimespecType& b) +template +inline bool operator<=(const T& a, const T& b) + { return a.tv_sec < b.tv_sec || (a.tv_sec == b.tv_sec && a.tv_nsec <= b.tv_nsec); } -template -inline bool operator==(const TimespecType& a, const TimespecType& b) +template +inline bool operator==(const T& a, const T& b) { return a.tv_sec == b.tv_sec && a.tv_nsec == b.tv_nsec; } -template -inline bool operator!=(const TimespecType& a, const TimespecType& b) +template +inline bool operator!=(const T& a, const T& b) { return a.tv_sec != b.tv_sec || a.tv_nsec != b.tv_nsec; }