Skip to contentSkip to conversation

rt-tests: queuelat: Fix wraparound in __clock_gettime()

PATCH In review [email protected] 3 participants 3 messages Nov 11, 2024 – Nov 21, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rt-tests: queuelat: Fix wraparound in __clock_gettime()

From: Tan En De <[email protected]>

Include tv_sec in addition to tv_nsec in __clock_gettime(). If the value of variable 'a' wraps around, it may cause 'delta' to be negative, while 'delta' is defined with u64 type.

---
 src/queuelat/queuelat.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
src/queuelat/queuelat.c +2 -1
index c549cd8..c057389 100644
@@ -292,7 +292,8 @@ static inline unsigned long long __clock_gettime(void)
 	if (ret < 0)
 		return 0;
 
-	return now.tv_nsec;
+	// Combine seconds and nanoseconds into a single value in nanoseconds
+	return (unsigned long long)now.tv_sec * 1000000000ULL + now.tv_nsec;
 }
 
 #define gettick(val) do { (val) = __clock_gettime(); } while (0)
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 29 lines · On Mon, Nov 11, 2024 at 09:49:11AM +0800, [email protected] wrote:

From: Tan En De <[email protected]>

Include tv_sec in addition to tv_nsec in __clock_gettime(). If the value of variable 'a' wraps around, it may cause 'delta' to be negative, while 'delta' is defined with u64 type.

---
 src/queuelat/queuelat.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
src/queuelat/queuelat.c +2 -1
index c549cd8..c057389 100644
@@ -292,7 +292,8 @@ static inline unsigned long long __clock_gettime(void)
 	if (ret < 0)
 		return 0;
 
-	return now.tv_nsec;
+	// Combine seconds and nanoseconds into a single value in nanoseconds
+	return (unsigned long long)now.tv_sec * 1000000000ULL + now.tv_nsec;
 }
 
 #define gettick(val) do { (val) = __clock_gettime(); } while (0)
Signature · 1 line
2.34.1
#2 lore raw
In reply to [email protected]
Quoted 30 lines · On Mon, 11 Nov 2024, [email protected] wrote:

From: Tan En De <[email protected]>

Include tv_sec in addition to tv_nsec in __clock_gettime(). If the value of variable 'a' wraps around, it may cause 'delta' to be negative, while 'delta' is defined with u64 type.

---
 src/queuelat/queuelat.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
src/queuelat/queuelat.c +2 -1
index c549cd8..c057389 100644
@@ -292,7 +292,8 @@ static inline unsigned long long __clock_gettime(void)
 	if (ret < 0)
 		return 0;
 
-	return now.tv_nsec;
+	// Combine seconds and nanoseconds into a single value in nanoseconds
+	return (unsigned long long)now.tv_sec * 1000000000ULL + now.tv_nsec;
 }
 
 #define gettick(val) do { (val) = __clock_gettime(); } while (0)
Signature · 1 line
2.34.1