Article View: news.software.nn
Article #9441nn 6.5.5 pack_date mishandles some dates
From: Paul Eggert
Date: Fri, 04 Feb 2000 00:00
Date: Fri, 04 Feb 2000 00:00
164 lines
5851 bytes
5851 bytes
[Also mailed to mtpins@visi.com.]
nn 6.5.5 has a new version of pack_date, but it mishandles some dates,
and this causes messages to be displayed out of order. For example,
pack_date("31 Jan 2000 10:46:25 -0500") yields 951493585, but
pack_date("01 Feb 2000 17:19:14 -0500") yields 951430754.
While fixing this problem, I noticed some other problems:
* Time stamps near the end of a leap-year are mishandled.
* pack_date uses `isdigit((int) ch)', where `ch' is a character, in
the apparent belief that the cast to int is needed. But that cast
doesn't help: if ch is negative, the result is undefined (and can
dump core). The correct way to use ctype macros on chars is to cast
their arguments to unsigned char, e.g. `isdigit((unsigned char) ch)'.
* The code could be sped up (I couldn't resist doing this :-).
2000-02-04 Paul Eggert <eggert@twinsun.com>
* pack_date.c (IS_C_DIGIT): New macro.
(numeric_zone, tzone, next_int): Prefer IS_C_DIGIT to isdigit,
as it's safer and faster.
(numeric_zone, tzone): Don't copy argument to static storage;
there's no need.
(tzone, pack_date): Cast char arg of <ctype.h> macro to
unsigned char, to avoid incorrect answers (and possible core dumps).
(pack_date): Fix bug when handling time stamps near the end of
a leap year. Don't add the number of days in the current month
to the time stamp.
===================================================================
RCS file: RCS/pack_date.c,v
retrieving revision 6.5.5.0
retrieving revision 6.5.5.1
diff -pu -r6.5.5.0 -r6.5.5.1
--- pack_date.c 2000/01/04 00:14:40 6.5.5.0
+++ pack_date.c 2000/02/04 19:26:09 6.5.5.1
@@ -18,6 +18,8 @@
/* pack_date.c */
+#define IS_C_DIGIT(c) ((unsigned) (c) - '0' <= 9)
+
#undef W
#undef E
#undef DST
@@ -139,7 +141,6 @@ static int month_days(int year, int
static int numeric_zone(register char *date)
{
register int n;
- static char num[MAXZ];
int adjust = 0, sign;
switch (*date) {
@@ -154,22 +155,19 @@ static int numeric_zone(register ch
break;
}
- for (n = 0; n < MAXZ && *date && isdigit((int) *date);)
- num[n++] = *date++;
- num[n] = '\0';
+ for (n = 0; n < MAXZ && IS_C_DIGIT(date[n]);)
+ n++;
switch (n) {
case 4: /* +HHMM */
- adjust = atoi(num + 2);
- num[2] = '\0';
+ adjust = (date[2] - '0') * 10 + date[3] - '0';
case 2: /* +HH *//* FALLTHROUGH */
- adjust += atoi(num) * 60;
+ adjust += ((date[0] - '0') * 10 + date[1] - '0') * 60;
break;
case 3: /* +HMM */
- adjust = atoi(num + 1);
- num[1] = '\0';
+ adjust = (date[1] - '0') * 10 + date[2] - '0';
case 1: /* +H *//* FALLTHROUGH */
- adjust += atoi(num) * 60;
+ adjust += (date[0] - '0') * 60;
break;
default: /* bad form */
break;
@@ -190,30 +188,32 @@ static int numeric_zone(register ch
static int tzone(register char *date)
{
register int i = 0;
- static char zone[MAXZ];
+ char zone[MAXZ];
register struct zonetab *z;
- while (*date && isspace((int) *date))
+ while (*date && isspace((unsigned char) *date))
date++;
- if (*date == '+' || *date == '-' || isdigit((int) *date))
+ if (*date == '+' || *date == '-' || IS_C_DIGIT(*date))
return numeric_zone(date);
for (; *date && isascii(*date); date++) {
- if (isspace((int) *date))
+ if (isspace((unsigned char) *date))
break;
- if (!isalnum((int) *date))
+ if (!isalnum((unsigned char) *date))
continue; /* p.s.t. -> pst */
if (i == MAXZ)
continue;
- zone[i++] = islower((int) *date) ? toupper((int) *date) : *date;
+ zone[i++] = (islower((unsigned char) *date)
+ ? toupper((unsigned char) *date)
+ : *date);
}
- while (*date && isspace((int) *date))
+ while (*date && isspace((unsigned char) *date))
date++;
if (i == 0)
return 0;
- if (*date == '+' || *date == '-' || isdigit((int) *date))
+ if (*date == '+' || *date == '-' || IS_C_DIGIT(*date))
return numeric_zone(date);
zone[i] = '\0';
@@ -244,11 +244,11 @@ static int next_int(char **dp)
register char *str = *dp;
register int i;
- while (*str && !isdigit((int) *str))
+ while (!IS_C_DIGIT(*str))
str++;
i = atoi(str);
- while (*str && isdigit((int) *str))
+ while (IS_C_DIGIT(*str))
str++;
*dp = str;
@@ -271,7 +271,7 @@ time_stamp pack_date(char *date)
if (date == NULL || (day = next_int(&date)) == 0)
return 0;
- while (*date && isspace((int) *date))
+ while (*date && isspace((unsigned char) *date))
date++;
if (date[0] == '\0' || date[1] == '\0' || date[2] == '\0')
@@ -360,10 +360,9 @@ time_stamp pack_date(char *date)
}
day = 1;
}
- day += ((year - 1970) * 365) + leap_year(year);;
- for (i = 0; i <= month; i++)
+ day += (year - 1970) * 366;
+ for (i = 0; i < month; i++)
day += month_days(year, i);
- --day;
return (day * 24 * 60 * 60) + (min * 60) + sec;
}
Technical Details
Message-ID:
<87fa3a$7j9$1@shade.twinsun.com>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nn.mbox.zip!not-for-mailShow Full Headers
Complete Article Headers
Message-ID: <87fa3a$7j9$1@shade.twinsun.com> Subject: nn 6.5.5 pack_date mishandles some dates Date: Fri, 04 Feb 2000 00:00:00 +0000 From: eggert@twinsun.com (Paul Eggert) Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nn.mbox.zip!not-for-mail X-Deja-AN: 581645211 X-Trace: tattoo.twinsun.com 949693354 6585 192.54.239.27 (4 Feb 2000 19:42:34 GMT) Organization: Twin Sun Inc, El Segundo, CA, USA NNTP-Posting-Date: 4 Feb 2000 19:42:34 GMT Newsgroups: news.software.nn X-Complaints-To: news@twinsun.com