Thread View: news.software.nn
2 messages
2 total messages
Started by Paul Eggert
Fri, 04 Feb 2000 00:00
nn 6.5.5 pack_date mishandles some dates
Author: 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;
}
Re: nn 6.5.5 pack_date mishandles some dates
Author: Michael T Pins
Date: Sat, 05 Feb 2000 00:00
Date: Sat, 05 Feb 2000 00:00
53 lines
2992 bytes
2992 bytes
eggert@twinsun.com (Paul Eggert) writes:
>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.
*ack*
This bug was your basic off-by-one bug. ( "<=" instead of "<" )
>While fixing this problem, I noticed some other problems:
>* Time stamps near the end of a leap-year are mishandled.
This was a remnant of an attempt to turn it into a real timestamp for
something else in nn7. A course I abandoned, but obviously neglected to
fully clean up after. Thanks for the pointers.
>* 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)'.
According to the C standard, a cast to int *is* required.
SYNOPSIS
#include <ctype.h>
int isdigit(int c);
While all characters must be representable as an unsigned char, EOF is not
so constrained, and is, in fact, (-1) on most systems.
The correct way to do it is to cast it to int, which is what I'm doing.
>* The code could be sped up (I couldn't resist doing this :-).
Well, sort of.
I ran both your version and my version on a group of 2000 entries. On my
old Sparc5, they both finished so fast that the micro-second counter never
registered. On my ancient 486, on 2000 entries, my version took 0.29 sec,
yours took 0.25 sec. While technically yours is faster, 4/100 seconds
(on 2000 entries) isn't really perceivable.
I've put nn-6.5.6 up, which includes both the fix for the time-sort bugs
(gads that's embarrassing) and the workaround for the new brain-damage
in Solaris7's header files.
--
**************************************************************************
* Michael T Pins | mtpins@visi.com *
* keeper of the nn sources | mtpins@icaen.uiowa.edu *
* ftp://ftp.visi.com/users/mtpins | #include <std.disclaimer> *
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads