[lttng-dev] [PATCH lttng-tools] Fix: nsec diff can be negative leading to expeditive connection timeout

Jonathan Rajotte jonathan.rajotte-julien at efficios.com
Thu Nov 8 17:08:05 EST 2018


The nanoseconds part of the timespec struct time_a is not always
bigger than time_b since it wrap around each seconds.

Use the absolute value of the nanosecond difference to perform
unsigned long operation.

Signed-off-by: Jonathan Rajotte <jonathan.rajotte-julien at efficios.com>
---
 src/common/sessiond-comm/inet.c  | 2 +-
 src/common/sessiond-comm/inet6.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/common/sessiond-comm/inet.c b/src/common/sessiond-comm/inet.c
index e0b3e7a96..cb6f45357 100644
--- a/src/common/sessiond-comm/inet.c
+++ b/src/common/sessiond-comm/inet.c
@@ -124,7 +124,7 @@ unsigned long time_diff_ms(struct timespec *time_a,
 	unsigned long result_ms;
 
 	sec_diff = time_a->tv_sec - time_b->tv_sec;
-	nsec_diff = time_a->tv_nsec - time_b->tv_nsec;
+	nsec_diff = labs(time_a->tv_nsec - time_b->tv_nsec);
 
 	result_ms = sec_diff * MSEC_PER_SEC;
 	result_ms += nsec_diff / NSEC_PER_MSEC;
diff --git a/src/common/sessiond-comm/inet6.c b/src/common/sessiond-comm/inet6.c
index dfb5fc5d1..b73802d48 100644
--- a/src/common/sessiond-comm/inet6.c
+++ b/src/common/sessiond-comm/inet6.c
@@ -122,7 +122,7 @@ unsigned long time_diff_ms(struct timespec *time_a,
 	unsigned long result_ms;
 
 	sec_diff = time_a->tv_sec - time_b->tv_sec;
-	nsec_diff = time_a->tv_nsec - time_b->tv_nsec;
+	nsec_diff = labs(time_a->tv_nsec - time_b->tv_nsec);
 
 	result_ms = sec_diff * MSEC_PER_SEC;
 	result_ms += nsec_diff / NSEC_PER_MSEC;
-- 
2.17.1



More information about the lttng-dev mailing list