Re: newbie y2038 bug, patch

Dan Kegel (dank@alumni.caltech.edu)
Fri, 19 Nov 1999 09:04:47 -0800


Alexander Viro wrote:
> On Fri, 19 Nov 1999, Dan Kegel wrote:
> > Reading "Linux Core Kernel Commentary" at bedtime,
> > I noticed what looks like a wraparound bug in
> > do_timer_interrupt() that could cause set_rtc_mmss
> > to get called once per second (instead of once
> > every 11 minutes) for an 11 minute interval near
> > the wraparound of xtime.tv_sec in the year 2038.
>
> Erm... x86 boxen... 2038... You know, there will be one hell of
> wraparound problems in 2038 on the surviving specimen of x86. Check the
> size of time_t.

Yep, this is one of those bugs. IMHO, the kernel should
continue operating normally on 32-bit-int machines, even
if user programs that format stuff in ASCII get confused.

As a general principle, code that looks like this
if (time > threshold)
should be shot if there's any chance of wraparound, ever.
It's better to use an idiom like
if (((long)(time - threshold)) > 0)
which simply keeps on working in the face of wraparound.

The kernel provides nice macros for this already in timer.h:
* These inlines deal with timer wrapping correctly. You are
* strongly encouraged to use them
* 1. Because people otherwise forget
* 2. Because if the timer wrap changes in future you wont have to
* alter your driver code. ...
*/
#define time_after(a,b) ((long)(b) - (long)(a) < 0)

I suspect there aren't too places in the kernel that
screw up and don't use these macros; it would be nice
to clean them all up, as a matter of principle. It won't
bloat the kernel at all, and it brings it one step
closer to making 100 year uptimes possible.

- Dan

-- 
(The above is just my personal opinion; I don't speak for my employer,
 except on the occasional talk show.)

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.tux.org/lkml/