1
0
mirror of https://git.savannah.gnu.org/git/gnulib.git synced 2025-08-08 17:22:05 +03:00

stdckdint: use in parse-datetime

* lib/parse-datetime.y (apply_relative_time, zone, date)
(iso_8601_date, relunit, relunit_snumber, time_zone_hhmm)
(to_tm_year, yylex, parse_datetime_body):
Prefer ckd_add to INT_ADD_WRAPV etc., and include stdckdint.h.
* modules/parse-datetime (Depends-on): Add stdckdint.
This commit is contained in:
Paul Eggert
2023-05-17 12:38:28 -07:00
parent d959e39cbd
commit 29bf76b33d
3 changed files with 60 additions and 55 deletions

View File

@@ -2,12 +2,15 @@
stdckdint: use in more modules stdckdint: use in more modules
* lib/nstrftime.c (__strftime_internal): * lib/nstrftime.c (__strftime_internal):
* lib/parse-datetime.y (apply_relative_time, zone, date)
(iso_8601_date, relunit, relunit_snumber, time_zone_hhmm)
(to_tm_year, yylex, parse_datetime_body):
* lib/timespec-add.c (timespec_add): * lib/timespec-add.c (timespec_add):
* lib/timespec-sub.c (timespec_sub): * lib/timespec-sub.c (timespec_sub):
* lib/xstrtol.c (bkm_scale): * lib/xstrtol.c (bkm_scale):
Prefer ckd_add to INT_ADD_WRAPV etc., and include stdckdint.h. Prefer ckd_add to INT_ADD_WRAPV etc., and include stdckdint.h.
* modules/nstrftime, modules/timespec-add, modules/timespec-sub: * modules/nstrftime, modules/parse-datetime:
* modules/xstrtol: * modules/timespec-add, modules/timespec-sub, modules/xstrtol:
(Depends-on): Add stdckdint. (Depends-on): Add stdckdint.
nstrftime: suggest to glibc how to avoid alloca nstrftime: suggest to glibc how to avoid alloca

View File

@@ -54,6 +54,7 @@
#include <inttypes.h> #include <inttypes.h>
#include <c-ctype.h> #include <c-ctype.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdckdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -293,20 +294,20 @@ static bool
apply_relative_time (parser_control *pc, relative_time rel, int factor) apply_relative_time (parser_control *pc, relative_time rel, int factor)
{ {
if (factor < 0 if (factor < 0
? (INT_SUBTRACT_WRAPV (pc->rel.ns, rel.ns, &pc->rel.ns) ? (ckd_sub (&pc->rel.ns, pc->rel.ns, rel.ns)
| INT_SUBTRACT_WRAPV (pc->rel.seconds, rel.seconds, &pc->rel.seconds) | ckd_sub (&pc->rel.seconds, pc->rel.seconds, rel.seconds)
| INT_SUBTRACT_WRAPV (pc->rel.minutes, rel.minutes, &pc->rel.minutes) | ckd_sub (&pc->rel.minutes, pc->rel.minutes, rel.minutes)
| INT_SUBTRACT_WRAPV (pc->rel.hour, rel.hour, &pc->rel.hour) | ckd_sub (&pc->rel.hour, pc->rel.hour, rel.hour)
| INT_SUBTRACT_WRAPV (pc->rel.day, rel.day, &pc->rel.day) | ckd_sub (&pc->rel.day, pc->rel.day, rel.day)
| INT_SUBTRACT_WRAPV (pc->rel.month, rel.month, &pc->rel.month) | ckd_sub (&pc->rel.month, pc->rel.month, rel.month)
| INT_SUBTRACT_WRAPV (pc->rel.year, rel.year, &pc->rel.year)) | ckd_sub (&pc->rel.year, pc->rel.year, rel.year))
: (INT_ADD_WRAPV (pc->rel.ns, rel.ns, &pc->rel.ns) : (ckd_add (&pc->rel.ns, pc->rel.ns, rel.ns)
| INT_ADD_WRAPV (pc->rel.seconds, rel.seconds, &pc->rel.seconds) | ckd_add (&pc->rel.seconds, pc->rel.seconds, rel.seconds)
| INT_ADD_WRAPV (pc->rel.minutes, rel.minutes, &pc->rel.minutes) | ckd_add (&pc->rel.minutes, pc->rel.minutes, rel.minutes)
| INT_ADD_WRAPV (pc->rel.hour, rel.hour, &pc->rel.hour) | ckd_add (&pc->rel.hour, pc->rel.hour, rel.hour)
| INT_ADD_WRAPV (pc->rel.day, rel.day, &pc->rel.day) | ckd_add (&pc->rel.day, pc->rel.day, rel.day)
| INT_ADD_WRAPV (pc->rel.month, rel.month, &pc->rel.month) | ckd_add (&pc->rel.month, pc->rel.month, rel.month)
| INT_ADD_WRAPV (pc->rel.year, rel.year, &pc->rel.year))) | ckd_add (&pc->rel.year, pc->rel.year, rel.year)))
return false; return false;
pc->rels_seen = true; pc->rels_seen = true;
return true; return true;
@@ -760,7 +761,7 @@ zone:
} }
| tZONE tSNUMBER o_colon_minutes | tZONE tSNUMBER o_colon_minutes
{ if (! time_zone_hhmm (pc, $2, $3)) YYABORT; { if (! time_zone_hhmm (pc, $2, $3)) YYABORT;
if (INT_ADD_WRAPV (pc->time_zone, $1, &pc->time_zone)) YYABORT; } if (ckd_add (&pc->time_zone, pc->time_zone, $1)) YYABORT; }
| tDAYZONE | tDAYZONE
{ pc->time_zone = $1 + 60 * 60; } { pc->time_zone = $1 + 60 * 60; }
| tZONE tDST | tZONE tDST
@@ -836,15 +837,15 @@ date:
/* E.g., 17-JUN-1992. */ /* E.g., 17-JUN-1992. */
pc->day = $1.value; pc->day = $1.value;
pc->month = $2; pc->month = $2;
if (INT_SUBTRACT_WRAPV (0, $3.value, &pc->year.value)) YYABORT; if (ckd_sub (&pc->year.value, 0, $3.value)) YYABORT;
pc->year.digits = $3.digits; pc->year.digits = $3.digits;
} }
| tMONTH tSNUMBER tSNUMBER | tMONTH tSNUMBER tSNUMBER
{ {
/* E.g., JUN-17-1992. */ /* E.g., JUN-17-1992. */
pc->month = $1; pc->month = $1;
if (INT_SUBTRACT_WRAPV (0, $2.value, &pc->day)) YYABORT; if (ckd_sub (&pc->day, 0, $2.value)) YYABORT;
if (INT_SUBTRACT_WRAPV (0, $3.value, &pc->year.value)) YYABORT; if (ckd_sub (&pc->year.value, 0, $3.value)) YYABORT;
pc->year.digits = $3.digits; pc->year.digits = $3.digits;
} }
| tMONTH tUNUMBER | tMONTH tUNUMBER
@@ -877,8 +878,8 @@ iso_8601_date:
{ {
/* ISO 8601 format. YYYY-MM-DD. */ /* ISO 8601 format. YYYY-MM-DD. */
pc->year = $1; pc->year = $1;
if (INT_SUBTRACT_WRAPV (0, $2.value, &pc->month)) YYABORT; if (ckd_sub (&pc->month, 0, $2.value)) YYABORT;
if (INT_SUBTRACT_WRAPV (0, $3.value, &pc->day)) YYABORT; if (ckd_sub (&pc->day, 0, $3.value)) YYABORT;
} }
; ;
@@ -906,10 +907,10 @@ relunit:
{ $$ = RELATIVE_TIME_0; $$.month = 1; } { $$ = RELATIVE_TIME_0; $$.month = 1; }
| tORDINAL tDAY_UNIT | tORDINAL tDAY_UNIT
{ $$ = RELATIVE_TIME_0; { $$ = RELATIVE_TIME_0;
if (INT_MULTIPLY_WRAPV ($1, $2, &$$.day)) YYABORT; } if (ckd_mul (&$$.day, $1, $2)) YYABORT; }
| tUNUMBER tDAY_UNIT | tUNUMBER tDAY_UNIT
{ $$ = RELATIVE_TIME_0; { $$ = RELATIVE_TIME_0;
if (INT_MULTIPLY_WRAPV ($1.value, $2, &$$.day)) YYABORT; } if (ckd_mul (&$$.day, $1.value, $2)) YYABORT; }
| tDAY_UNIT | tDAY_UNIT
{ $$ = RELATIVE_TIME_0; $$.day = $1; } { $$ = RELATIVE_TIME_0; $$.day = $1; }
| tORDINAL tHOUR_UNIT | tORDINAL tHOUR_UNIT
@@ -944,7 +945,7 @@ relunit_snumber:
{ $$ = RELATIVE_TIME_0; $$.month = $1.value; } { $$ = RELATIVE_TIME_0; $$.month = $1.value; }
| tSNUMBER tDAY_UNIT | tSNUMBER tDAY_UNIT
{ $$ = RELATIVE_TIME_0; { $$ = RELATIVE_TIME_0;
if (INT_MULTIPLY_WRAPV ($1.value, $2, &$$.day)) YYABORT; } if (ckd_mul (&$$.day, $1.value, $2)) YYABORT; }
| tSNUMBER tHOUR_UNIT | tSNUMBER tHOUR_UNIT
{ $$ = RELATIVE_TIME_0; $$.hour = $1.value; } { $$ = RELATIVE_TIME_0; $$.hour = $1.value; }
| tSNUMBER tMINUTE_UNIT | tSNUMBER tMINUTE_UNIT
@@ -1214,10 +1215,10 @@ time_zone_hhmm (parser_control *pc, textint s, intmax_t mm)
n_minutes = (s.value / 100) * 60 + s.value % 100; n_minutes = (s.value / 100) * 60 + s.value % 100;
else else
{ {
overflow |= INT_MULTIPLY_WRAPV (s.value, 60, &n_minutes); overflow |= ckd_mul (&n_minutes, s.value, 60);
overflow |= (s.negative overflow |= (s.negative
? INT_SUBTRACT_WRAPV (n_minutes, mm, &n_minutes) ? ckd_sub (&n_minutes, n_minutes, mm)
: INT_ADD_WRAPV (n_minutes, mm, &n_minutes)); : ckd_add (&n_minutes, n_minutes, mm));
} }
if (overflow || ! (-24 * 60 <= n_minutes && n_minutes <= 24 * 60)) if (overflow || ! (-24 * 60 <= n_minutes && n_minutes <= 24 * 60))
@@ -1277,8 +1278,8 @@ to_tm_year (textint textyear, bool debug, int *tm_year)
} }
if (year < 0 if (year < 0
? INT_SUBTRACT_WRAPV (-TM_YEAR_BASE, year, tm_year) ? ckd_sub (tm_year, -TM_YEAR_BASE, year)
: INT_SUBTRACT_WRAPV (year, TM_YEAR_BASE, tm_year)) : ckd_sub (tm_year, year, TM_YEAR_BASE))
{ {
if (debug) if (debug)
dbg_printf (_("error: out-of-range year %"PRIdMAX"\n"), year); dbg_printf (_("error: out-of-range year %"PRIdMAX"\n"), year);
@@ -1434,9 +1435,9 @@ yylex (union YYSTYPE *lvalp, parser_control *pc)
time_t value = 0; time_t value = 0;
do do
{ {
if (INT_MULTIPLY_WRAPV (value, 10, &value)) if (ckd_mul (&value, value, 10))
return '?'; return '?';
if (INT_ADD_WRAPV (value, sign < 0 ? '0' - c : c - '0', &value)) if (ckd_add (&value, value, sign < 0 ? '0' - c : c - '0'))
return '?'; return '?';
c = *++p; c = *++p;
} }
@@ -1473,7 +1474,7 @@ yylex (union YYSTYPE *lvalp, parser_control *pc)
negative. */ negative. */
if (sign < 0 && ns) if (sign < 0 && ns)
{ {
if (INT_SUBTRACT_WRAPV (s, 1, &s)) if (ckd_sub (&s, s, 1))
return '?'; return '?';
ns = BILLION - ns; ns = BILLION - ns;
} }
@@ -1799,7 +1800,7 @@ parse_datetime_body (struct timespec *result, char const *p,
#ifdef GNULIB_PARSE_DATETIME2 #ifdef GNULIB_PARSE_DATETIME2
pc.parse_datetime_debug = (flags & PARSE_DATETIME_DEBUG) != 0; pc.parse_datetime_debug = (flags & PARSE_DATETIME_DEBUG) != 0;
#endif #endif
if (INT_ADD_WRAPV (tmp.tm_year, TM_YEAR_BASE, &pc.year.value)) if (ckd_add (&pc.year.value, tmp.tm_year, TM_YEAR_BASE))
{ {
if (debugging (&pc)) if (debugging (&pc))
dbg_printf (_("error: initial year out of range\n")); dbg_printf (_("error: initial year out of range\n"));
@@ -1846,7 +1847,7 @@ parse_datetime_body (struct timespec *result, char const *p,
for (quarter = 1; quarter <= 3; quarter++) for (quarter = 1; quarter <= 3; quarter++)
{ {
time_t probe; time_t probe;
if (INT_ADD_WRAPV (Start, quarter * (90 * 24 * 60 * 60), &probe)) if (ckd_add (&probe, Start, quarter * (90 * 24 * 60 * 60)))
break; break;
struct tm probe_tm; struct tm probe_tm;
if (localtime_rz (tz, &probe, &probe_tm) && probe_tm.tm_zone if (localtime_rz (tz, &probe, &probe_tm) && probe_tm.tm_zone
@@ -1965,8 +1966,8 @@ parse_datetime_body (struct timespec *result, char const *p,
} }
if (! to_tm_year (pc.year, debugging (&pc), &tm.tm_year) if (! to_tm_year (pc.year, debugging (&pc), &tm.tm_year)
|| INT_ADD_WRAPV (pc.month, -1, &tm.tm_mon) || ckd_add (&tm.tm_mon, pc.month, -1)
|| INT_ADD_WRAPV (pc.day, 0, &tm.tm_mday)) || ckd_add (&tm.tm_mday, pc.day, 0))
{ {
if (debugging (&pc)) if (debugging (&pc))
dbg_printf (_("error: year, month, or day overflow\n")); dbg_printf (_("error: year, month, or day overflow\n"));
@@ -2072,13 +2073,13 @@ parse_datetime_body (struct timespec *result, char const *p,
{ {
intmax_t dayincr; intmax_t dayincr;
tm.tm_yday = -1; tm.tm_yday = -1;
if (! (INT_MULTIPLY_WRAPV ((pc.day_ordinal intmax_t day_ordinal = (pc.day_ordinal
- (0 < pc.day_ordinal - (0 < pc.day_ordinal
&& tm.tm_wday != pc.day_number)), && tm.tm_wday != pc.day_number));
7, &dayincr) if (! (ckd_mul (&dayincr, day_ordinal, 7)
|| INT_ADD_WRAPV ((pc.day_number - tm.tm_wday + 7) % 7, || ckd_add (&dayincr, (pc.day_number - tm.tm_wday + 7) % 7,
dayincr, &dayincr) dayincr)
|| INT_ADD_WRAPV (dayincr, tm.tm_mday, &tm.tm_mday))) || ckd_add (&tm.tm_mday, dayincr, tm.tm_mday)))
{ {
tm.tm_isdst = -1; tm.tm_isdst = -1;
Start = mktime_z (tz, &tm); Start = mktime_z (tz, &tm);
@@ -2135,9 +2136,9 @@ parse_datetime_body (struct timespec *result, char const *p,
} }
int year, month, day; int year, month, day;
if (INT_ADD_WRAPV (tm.tm_year, pc.rel.year, &year) if (ckd_add (&year, tm.tm_year, pc.rel.year)
|| INT_ADD_WRAPV (tm.tm_mon, pc.rel.month, &month) || ckd_add (&month, tm.tm_mon, pc.rel.month)
|| INT_ADD_WRAPV (tm.tm_mday, pc.rel.day, &day)) || ckd_add (&day, tm.tm_mday, pc.rel.day))
{ {
if (debugging (&pc)) if (debugging (&pc))
dbg_printf (_("error: %s:%d\n"), __FILE__, __LINE__); dbg_printf (_("error: %s:%d\n"), __FILE__, __LINE__);
@@ -2234,9 +2235,9 @@ parse_datetime_body (struct timespec *result, char const *p,
: (overflow = true, 0)); : (overflow = true, 0));
#endif #endif
intmax_t delta; intmax_t delta;
overflow |= INT_SUBTRACT_WRAPV (pc.time_zone, utcoff, &delta); overflow |= ckd_sub (&delta, pc.time_zone, utcoff);
time_t t1; time_t t1;
overflow |= INT_SUBTRACT_WRAPV (Start, delta, &t1); overflow |= ckd_sub (&t1, Start, delta);
if (overflow) if (overflow)
{ {
if (debugging (&pc)) if (debugging (&pc))
@@ -2270,12 +2271,12 @@ parse_datetime_body (struct timespec *result, char const *p,
int d4 = (sum_ns - normalized_ns) / BILLION; int d4 = (sum_ns - normalized_ns) / BILLION;
intmax_t d1, t1, d2, t2, t3; intmax_t d1, t1, d2, t2, t3;
time_t t4; time_t t4;
if (INT_MULTIPLY_WRAPV (pc.rel.hour, 60 * 60, &d1) if (ckd_mul (&d1, pc.rel.hour, 60 * 60)
|| INT_ADD_WRAPV (Start, d1, &t1) || ckd_add (&t1, Start, d1)
|| INT_MULTIPLY_WRAPV (pc.rel.minutes, 60, &d2) || ckd_mul (&d2, pc.rel.minutes, 60)
|| INT_ADD_WRAPV (t1, d2, &t2) || ckd_add (&t2, t1, d2)
|| INT_ADD_WRAPV (t2, pc.rel.seconds, &t3) || ckd_add (&t3, t2, pc.rel.seconds)
|| INT_ADD_WRAPV (t3, d4, &t4)) || ckd_add (&t4, t3, d4))
{ {
if (debugging (&pc)) if (debugging (&pc))
dbg_printf (_("error: adding relative time caused an " dbg_printf (_("error: adding relative time caused an "

View File

@@ -22,6 +22,7 @@ inttypes
mktime mktime
nstrftime nstrftime
setenv setenv
stdckdint
unsetenv unsetenv
time-h time-h
time_r time_r