Bug in gtime(3) in 4.xbsd

gorlick at trwrb.UUCP gorlick at trwrb.UUCP
Thu May 10 06:10:52 AEST 1984


1984-0020             TRW/UNIX Modification             1984-0020

NAME
     gmtime - may return incorrect day of the month

DATE ENACTED
     May 9, 1984

KEYWORDS
     ctime(3), localtime(3), gmtime(3)

PROBLEM DESCRIPTION
     Under rare circumstances gmtime(3) could return the
     incorrect day of the month.

BACKGROUND
     In determining the day of the month for a leap year (the
     field tm_mday in the structure tm defined in <time.h>) the
     routine gmtime() will modify the static global table dmsize
     which contains the number of days in each month of the year.
     Once a short calculation is completed dmsize is reset to its
     original values (for a non-leap year).

     If a signal occurs and is fielded between the modification
     and the reset the table dmsize can be left in a modified
     state.  Potentially the next call to gmtime(3) can return a
     value in the field tm_mday that is off by 1.

RESOLUTION
     Change the declaration for dmsize from

          static    int  dmsize[12] =

     to

          static    short     dmsize[12] =

     and create a companion table, dmleap, for leap years

          static    short     dmleap[12] = {
               31,29,31,30,31,30,31,31,30,31,30,31
          };

     In the routine gmtime() change the code

          if (dysize(d1)==366)
               dmsize[1] = 29;
          for(d1=0; d0 >= dmsize[d1]; d1++)
               d0 -= dmsize[d1];
          dmsize[1] = 28;

     to

          if (dysize(d1)==366)
               for (d1=0; d0 >= dmleap[d1]; d1++)
                    d0 -= dmleap[d1];
          else
               for (d1=0; d0 >= dmsize[d1]; d1++)
                    d0 -= dmsize[d1];

FILES
     /usr/src/libc/gen/ctime.c

REQUESTOR
     Michael Gorlick

AUTHOR
     Michael Gorlick {decvax, ucbvax}!trwrb!gorlick


This flaw exists in V7 and System V as well.



More information about the Comp.unix.wizards mailing list