Skip to contentSkip to conversation

Timespec normalize off by one errors

PATCH In review George Anzinger 1 participant 3 messages Nov 11, 2005 1 message not in the archive Timekeeping, clocksource core, NTP, alarmtimer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Timespec normalize off by one errors

Found three places were we use ">" instead of ">=".

Signature · 2 lines
George Anzinger   [email protected]
HRT (High-res-timers):  http://sourceforge.net/projects/high-res-timers/

Source: MontaVista Software, Inc.
Type: Defect Fix

It would appear that the timespec normalize code has an off by one error. Found in three places.

 include/linux/time.h  |    2 +-
 kernel/posix-timers.c |   10 +++-------
 2 files changed, 4 insertions(+), 8 deletions(-)
include/linux/time.h +1 -1
Index: linux-2.6.15-rc/include/linux/time.h
===================================================================
@@ -101,7 +101,7 @@ extern struct timespec timespec_trunc(st
 static inline void
 set_normalized_timespec (struct timespec *ts, time_t sec, long nsec)
 {
-	while (nsec > NSEC_PER_SEC) {
+	while (nsec >= NSEC_PER_SEC) {
 		nsec -= NSEC_PER_SEC;
 		++sec;
 	}
kernel/posix-timers.c +3 -7
Index: linux-2.6.15-rc/kernel/posix-timers.c
===================================================================
@@ -270,7 +270,7 @@ static void tstojiffie(struct timespec *
 	long sec = tp->tv_sec;
 	long nsec = tp->tv_nsec + res - 1;
 
-	if (nsec > NSEC_PER_SEC) {
+	if (nsec >= NSEC_PER_SEC) {
 		sec++;
 		nsec -= NSEC_PER_SEC;
 	}
@@ -1209,13 +1209,9 @@ static int do_posix_clock_monotonic_get(
 
 	do_posix_clock_monotonic_gettime_parts(tp, &wall_to_mono);
 
-	tp->tv_sec += wall_to_mono.tv_sec;
-	tp->tv_nsec += wall_to_mono.tv_nsec;
+	set_normalized_timespec(tp, tp->tv_sec += wall_to_mono.tv_sec,
+				tv_nsec += wall_to_mono.tv_nsec);
 
-	if ((tp->tv_nsec - NSEC_PER_SEC) > 0) {
-		tp->tv_nsec -= NSEC_PER_SEC;
-		tp->tv_sec++;
-	}
 	return 0;
 }
 

A message that is not in the archive (sent off-list or not yet received): 1131744325.9957.20.camel@localhost

#2 lore raw
Quoted 21 lines · Joe Perches
Quoted 5 lines · George Anzinger
kernel/posix-timers.c +0 -1
@@ -1209,13 +1209,9 @@ static int do_posix_clock_monotonic_get(
 
 	do_posix_clock_monotonic_gettime_parts(tp, &wall_to_mono);
 
-	tp->tv_sec += wall_to_mono.tv_sec;

kernel/posix-timers.c static int do_posix_clock_monotonic_get( · line 1216

Quoted 10 lines
kernel/posix-timers.c +2 -5
-	tp->tv_nsec += wall_to_mono.tv_nsec;
+	set_normalized_timespec(tp, tp->tv_sec += wall_to_mono.tv_sec,
+				tv_nsec += wall_to_mono.tv_nsec);
 
-	if ((tp->tv_nsec - NSEC_PER_SEC) > 0) {
-		tp->tv_nsec -= NSEC_PER_SEC;
-		tp->tv_sec++;
-	}
 	return 0;
 }

This is extremely ugly.

Quoted 8 lines

tp->tv_sec += wall_to_mono.tv_sec;
tp->tv_nsec += wall_to_mono.tv_nsec;
set_normalized_timespec(tp, tv->sec, tv_nsec);

is much more intelligible.

Besides, I think you forgot the "tp->" indirection in the last argument of the set_normalized_timespec.

Oh SHIT. That is wrong. Think I will compile the next version... Stand by.

Quoted 3 lines

cheers, Joe

Signature · 2 lines
George Anzinger   [email protected]
HRT (High-res-timers):  http://sourceforge.net/projects/high-res-timers/
#3 lore raw
In reply to George Anzinger
Quoted 2 lines · George Anzinger

Found three places were we use ">" instead of ">=".

Lets just try this again.

Signature · 2 lines
George Anzinger   [email protected]
HRT (High-res-timers):  http://sourceforge.net/projects/high-res-timers/

Source: MontaVista Software, Inc.
Type: Defect Fix

It would appear that the timespec normalize code has an off by one error. Found in three places.

 include/linux/time.h  |    2 +-
 kernel/posix-timers.c |   10 +++-------
 2 files changed, 4 insertions(+), 8 deletions(-)
include/linux/time.h +1 -1
Index: linux-2.6.15-rc/include/linux/time.h
===================================================================
@@ -101,7 +101,7 @@ extern struct timespec timespec_trunc(st
 static inline void
 set_normalized_timespec (struct timespec *ts, time_t sec, long nsec)
 {
-	while (nsec > NSEC_PER_SEC) {
+	while (nsec >= NSEC_PER_SEC) {
 		nsec -= NSEC_PER_SEC;
 		++sec;
 	}
kernel/posix-timers.c +3 -7
Index: linux-2.6.15-rc/kernel/posix-timers.c
===================================================================
@@ -270,7 +270,7 @@ static void tstojiffie(struct timespec *
 	long sec = tp->tv_sec;
 	long nsec = tp->tv_nsec + res - 1;
 
-	if (nsec > NSEC_PER_SEC) {
+	if (nsec >= NSEC_PER_SEC) {
 		sec++;
 		nsec -= NSEC_PER_SEC;
 	}
@@ -1209,13 +1209,9 @@ static int do_posix_clock_monotonic_get(
 
 	do_posix_clock_monotonic_gettime_parts(tp, &wall_to_mono);
 
-	tp->tv_sec += wall_to_mono.tv_sec;
-	tp->tv_nsec += wall_to_mono.tv_nsec;
+	set_normalized_timespec(tp, tp->tv_sec + wall_to_mono.tv_sec,
+				tp->tv_nsec + wall_to_mono.tv_nsec);
 
-	if ((tp->tv_nsec - NSEC_PER_SEC) > 0) {
-		tp->tv_nsec -= NSEC_PER_SEC;
-		tp->tv_sec++;
-	}
 	return 0;
 }