2005-11-11 21:16:11

by George Anzinger

[permalink] [raw]
Subject: [PATCH] Timespec normalize off by one errors

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.

Signed-off-by: George Anzinger<[email protected]>

include/linux/time.h | 2 +-
kernel/posix-timers.c | 10 +++-------
2 files changed, 4 insertions(+), 8 deletions(-)

Index: linux-2.6.15-rc/include/linux/time.h
===================================================================
--- linux-2.6.15-rc.orig/include/linux/time.h
+++ 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;
}
Index: linux-2.6.15-rc/kernel/posix-timers.c
===================================================================
--- linux-2.6.15-rc.orig/kernel/posix-timers.c
+++ 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;
}


Attachments:
time-spec2.patch (1.59 kB)

2005-11-11 22:15:57

by George Anzinger

[permalink] [raw]
Subject: Re: [PATCH] Timespec normalize off by one errors

Joe Perches wrote:
> On Fri, 2005-11-11 at 13:16 -0800, George Anzinger wrote:
>
>>@@ -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;
>> }
>
>
> This is extremely ugly.
>
> 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.
>
> cheers, Joe
>

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

2005-11-11 22:25:34

by George Anzinger

[permalink] [raw]
Subject: Re: [PATCH] Timespec normalize off by one errors

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.

Signed-off-by: George Anzinger<[email protected]>

include/linux/time.h | 2 +-
kernel/posix-timers.c | 10 +++-------
2 files changed, 4 insertions(+), 8 deletions(-)

Index: linux-2.6.15-rc/include/linux/time.h
===================================================================
--- linux-2.6.15-rc.orig/include/linux/time.h
+++ 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;
}
Index: linux-2.6.15-rc/kernel/posix-timers.c
===================================================================
--- linux-2.6.15-rc.orig/kernel/posix-timers.c
+++ 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;
}


Attachments:
time-spec2.patch (1.60 kB)