Here are a couple of proposed patches, to make use of idx_t instead of ptrdiff_t when the value is always nonnegative. This helps understanding the code.
2020-12-05 Bruno Haible <br...@clisp.org> filenamecat-tests: Use idx_t for nonnegative ptrdiff_t variables. * tests/test-filenamecat.c: Include idx.h. (main): Mark prefixlen as nonnegative. * modules/filenamecat-tests (Depends-on): Add idx. 2020-12-05 Bruno Haible <br...@clisp.org> time_rz: Use idx_t for nonnegative ptrdiff_t variables. * lib/time_rz.c: Include idx.h. (save_abbr): Mark zone_size as nonnegative. * modules/time_rz (Depends-on): Add idx. 2020-12-05 Bruno Haible <br...@clisp.org> parse-datetime: Use idx_t for nonnegative ptrdiff_t variables. * lib/parse-datetime.y: Include idx.h. (textint): Mark digits as nonnegative. (parser_control): Mark dates_seen, days_seen, local_zones_seen, dsts_seen, times_seen, zones_seen as nonnegative. (lookup_word): Mark wordlen as nonnegative. (yylex): Mark count as nonnegative. (parse_datetime2): Mark tzsize as nonnegative. * modules/parse-datetime (Depends-on): Add idx. 2020-12-05 Bruno Haible <br...@clisp.org> fnmatch: Use idx_t for nonnegative ptrdiff_t variables. * lib/fnmatch.c: Include idx.h. In glibc, define idx_t directly. * lib/fnmatch_loop.c (EXT): Mark slen, new_used, plensize as nonnegative. * modules/fnmatch (Depends-on): Add idx. 2020-12-05 Bruno Haible <br...@clisp.org> c-stack: Use idx_t for nonnegative ptrdiff_t variables. * lib/c-stack.c: Include idx.h. (die): Mark buflen as nonnegative. * modules/c-stack (Depends-on): Add idx. 2020-12-05 Bruno Haible <br...@clisp.org> backupfile: Use idx_t for nonnegative ptrdiff_t variables. * lib/backupfile.c: Include idx.h. (numbered_backup): Mark base_offset as nonnegative. (backupfile_internal): Likewise. * modules/backup-rename (Depends-on): Add idx. * modules/backupfile (Depends-on): Likewise.
>From 873bdd8d5275e41f107daed3efcae16084f69d95 Mon Sep 17 00:00:00 2001 From: Bruno Haible <br...@clisp.org> Date: Sat, 5 Dec 2020 11:43:25 +0100 Subject: [PATCH 1/6] backupfile: Use idx_t for nonnegative ptrdiff_t variables. * lib/backupfile.c: Include idx.h. (numbered_backup): Mark base_offset as nonnegative. (backupfile_internal): Likewise. * modules/backup-rename (Depends-on): Add idx. * modules/backupfile (Depends-on): Likewise. --- ChangeLog | 9 +++++++++ lib/backupfile.c | 17 +++++++++-------- modules/backup-rename | 1 + modules/backupfile | 1 + 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3de6368..3281f74 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2020-12-05 Bruno Haible <br...@clisp.org> + + backupfile: Use idx_t for nonnegative ptrdiff_t variables. + * lib/backupfile.c: Include idx.h. + (numbered_backup): Mark base_offset as nonnegative. + (backupfile_internal): Likewise. + * modules/backup-rename (Depends-on): Add idx. + * modules/backupfile (Depends-on): Likewise. + 2020-12-04 Bruno Haible <br...@clisp.org> utime: Fix a test failure on macOS 10.13. diff --git a/lib/backupfile.c b/lib/backupfile.c index 2a88248..dfc29e8 100644 --- a/lib/backupfile.c +++ b/lib/backupfile.c @@ -22,12 +22,6 @@ #include "backup-internal.h" -#include "attribute.h" -#include "basename-lgpl.h" -#include "opendirat.h" -#include "renameatu.h" -#include "xalloc-oversized.h" - #include <errno.h> #include <fcntl.h> #include <stdbool.h> @@ -36,6 +30,13 @@ #include <string.h> #include <unistd.h> +#include "attribute.h" +#include "basename-lgpl.h" +#include "idx.h" +#include "opendirat.h" +#include "renameatu.h" +#include "xalloc-oversized.h" + #ifndef _D_EXACT_NAMLEN # define _D_EXACT_NAMLEN(dp) strlen ((dp)->d_name) #endif @@ -198,7 +199,7 @@ enum numbered_backup_result static enum numbered_backup_result numbered_backup (int dir_fd, char **buffer, size_t buffer_size, size_t filelen, - ptrdiff_t base_offset, DIR **dirpp, int *pnew_fd) + idx_t base_offset, DIR **dirpp, int *pnew_fd) { enum numbered_backup_result result = BACKUP_IS_NEW; DIR *dirp = *dirpp; @@ -307,7 +308,7 @@ char * backupfile_internal (int dir_fd, char const *file, enum backup_type backup_type, bool rename) { - ptrdiff_t base_offset = last_component (file) - file; + idx_t base_offset = last_component (file) - file; size_t filelen = base_offset + strlen (file + base_offset); if (! simple_backup_suffix) diff --git a/modules/backup-rename b/modules/backup-rename index cdc96e6..d4bd339 100644 --- a/modules/backup-rename +++ b/modules/backup-rename @@ -17,6 +17,7 @@ closedir d-ino dirent-safer fcntl +idx memcmp opendirat readdir diff --git a/modules/backupfile b/modules/backupfile index 2ca1e54..fd15ec5 100644 --- a/modules/backupfile +++ b/modules/backupfile @@ -17,6 +17,7 @@ closedir d-ino dirent-safer fcntl +idx memcmp opendirat readdir -- 2.7.4
>From 0eec786d489ebaed246d6dbdc48c131742c90e42 Mon Sep 17 00:00:00 2001 From: Bruno Haible <br...@clisp.org> Date: Sat, 5 Dec 2020 11:45:15 +0100 Subject: [PATCH 2/6] c-stack: Use idx_t for nonnegative ptrdiff_t variables. * lib/c-stack.c: Include idx.h. (die): Mark buflen as nonnegative. * modules/c-stack (Depends-on): Add idx. --- ChangeLog | 7 +++++++ lib/c-stack.c | 10 ++++++---- modules/c-stack | 1 + 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3281f74..6d32664 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,12 @@ 2020-12-05 Bruno Haible <br...@clisp.org> + c-stack: Use idx_t for nonnegative ptrdiff_t variables. + * lib/c-stack.c: Include idx.h. + (die): Mark buflen as nonnegative. + * modules/c-stack (Depends-on): Add idx. + +2020-12-05 Bruno Haible <br...@clisp.org> + backupfile: Use idx_t for nonnegative ptrdiff_t variables. * lib/backupfile.c: Include idx.h. (numbered_backup): Mark base_offset as nonnegative. diff --git a/lib/c-stack.c b/lib/c-stack.c index 3aea16a..c5bb74a 100644 --- a/lib/c-stack.c +++ b/lib/c-stack.c @@ -37,9 +37,6 @@ #include "c-stack.h" -#include "gettext.h" -#define _(msgid) gettext (msgid) - #include <errno.h> #include <inttypes.h> @@ -64,6 +61,11 @@ typedef struct sigaltstack stack_t; # include <stdio.h> #endif +#include "idx.h" + +#include "gettext.h" +#define _(msgid) gettext (msgid) + /* Use libsigsegv only if needed; kernels like Solaris can detect stack overflow without the overhead of an external library. */ #define USE_LIBSIGSEGV (!HAVE_XSI_STACK_OVERFLOW_HEURISTIC && HAVE_LIBSIGSEGV) @@ -134,7 +136,7 @@ die (int signo) size_t messagelen = strlen (message); static char const separator[] = {':', ' '}; char buf[sizeof alternate_signal_stack / 16 + sizeof separator]; - ptrdiff_t buflen; + idx_t buflen; if (prognamelen + messagelen < sizeof buf - sizeof separator) { char *p = mempcpy (buf, progname, prognamelen); diff --git a/modules/c-stack b/modules/c-stack index 77cf6aa..4c80f31 100644 --- a/modules/c-stack +++ b/modules/c-stack @@ -14,6 +14,7 @@ exitfail getprogname gettext-h havelib +idx ignore-value intprops inttypes -- 2.7.4
>From 1e75316d5a581ad87d659bd6f4c22153d8640cc0 Mon Sep 17 00:00:00 2001 From: Bruno Haible <br...@clisp.org> Date: Sat, 5 Dec 2020 11:47:51 +0100 Subject: [PATCH 3/6] fnmatch: Use idx_t for nonnegative ptrdiff_t variables. * lib/fnmatch.c: Include idx.h. In glibc, define idx_t directly. * lib/fnmatch_loop.c (EXT): Mark slen, new_used, plensize as nonnegative. * modules/fnmatch (Depends-on): Add idx. --- ChangeLog | 8 ++++++++ lib/fnmatch.c | 6 ++++++ lib/fnmatch_loop.c | 6 +++--- modules/fnmatch | 1 + 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index 6d32664..797f6d0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,13 @@ 2020-12-05 Bruno Haible <br...@clisp.org> + fnmatch: Use idx_t for nonnegative ptrdiff_t variables. + * lib/fnmatch.c: Include idx.h. In glibc, define idx_t directly. + * lib/fnmatch_loop.c (EXT): Mark slen, new_used, plensize as + nonnegative. + * modules/fnmatch (Depends-on): Add idx. + +2020-12-05 Bruno Haible <br...@clisp.org> + c-stack: Use idx_t for nonnegative ptrdiff_t variables. * lib/c-stack.c: Include idx.h. (die): Mark buflen as nonnegative. diff --git a/lib/fnmatch.c b/lib/fnmatch.c index 3014428..c80809e 100644 --- a/lib/fnmatch.c +++ b/lib/fnmatch.c @@ -76,6 +76,12 @@ extern int fnmatch (const char *pattern, const char *string, int flags); #include <intprops.h> #include <flexmember.h> +#ifdef _LIBC +typedef ptrdiff_t idx_t; +#else +# include "idx.h" +#endif + /* We often have to test for FNM_FILE_NAME and FNM_PERIOD being both set. */ #define NO_LEADING_PERIOD(flags) \ ((flags & (FNM_FILE_NAME | FNM_PERIOD)) == (FNM_FILE_NAME | FNM_PERIOD)) diff --git a/lib/fnmatch_loop.c b/lib/fnmatch_loop.c index d9be16f..c533107 100644 --- a/lib/fnmatch_loop.c +++ b/lib/fnmatch_loop.c @@ -1036,9 +1036,9 @@ EXT (INT opt, const CHAR *pattern, const CHAR *string, const CHAR *string_end, struct patternlist *newp; \ size_t plen = (opt == L_('?') || opt == L_('@') \ ? pattern_len : (p - startp + 1UL)); \ - ptrdiff_t slen = FLEXSIZEOF (struct patternlist, str, 0); \ - ptrdiff_t new_used = alloca_used + slen; \ - ptrdiff_t plensize; \ + idx_t slen = FLEXSIZEOF (struct patternlist, str, 0); \ + idx_t new_used = alloca_used + slen; \ + idx_t plensize; \ if (INT_MULTIPLY_WRAPV (plen, sizeof (CHAR), &plensize) \ || INT_ADD_WRAPV (new_used, plensize, &new_used)) \ { \ diff --git a/modules/fnmatch b/modules/fnmatch index f547670..2cb69a7 100644 --- a/modules/fnmatch +++ b/modules/fnmatch @@ -14,6 +14,7 @@ attribute [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1] btowc [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1] builtin-expect [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1] flexmember [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1] +idx [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1] intprops [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1] isblank [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1] iswctype [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1] -- 2.7.4
>From 08d7f5cb9d2d087d17541256868219384693b645 Mon Sep 17 00:00:00 2001 From: Bruno Haible <br...@clisp.org> Date: Sat, 5 Dec 2020 11:51:23 +0100 Subject: [PATCH 4/6] parse-datetime: Use idx_t for nonnegative ptrdiff_t variables. * lib/parse-datetime.y: Include idx.h. (textint): Mark digits as nonnegative. (parser_control): Mark dates_seen, days_seen, local_zones_seen, dsts_seen, times_seen, zones_seen as nonnegative. (lookup_word): Mark wordlen as nonnegative. (yylex): Mark count as nonnegative. (parse_datetime2): Mark tzsize as nonnegative. * modules/parse-datetime (Depends-on): Add idx. --- ChangeLog | 12 ++++++++++++ lib/parse-datetime.y | 21 +++++++++++---------- modules/parse-datetime | 1 + 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index 797f6d0..57dea1d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,17 @@ 2020-12-05 Bruno Haible <br...@clisp.org> + parse-datetime: Use idx_t for nonnegative ptrdiff_t variables. + * lib/parse-datetime.y: Include idx.h. + (textint): Mark digits as nonnegative. + (parser_control): Mark dates_seen, days_seen, local_zones_seen, + dsts_seen, times_seen, zones_seen as nonnegative. + (lookup_word): Mark wordlen as nonnegative. + (yylex): Mark count as nonnegative. + (parse_datetime2): Mark tzsize as nonnegative. + * modules/parse-datetime (Depends-on): Add idx. + +2020-12-05 Bruno Haible <br...@clisp.org> + fnmatch: Use idx_t for nonnegative ptrdiff_t variables. * lib/fnmatch.c: Include idx.h. In glibc, define idx_t directly. * lib/fnmatch_loop.c (EXT): Mark slen, new_used, plensize as diff --git a/lib/parse-datetime.y b/lib/parse-datetime.y index e8ed691..2b56db4 100644 --- a/lib/parse-datetime.y +++ b/lib/parse-datetime.y @@ -35,6 +35,7 @@ #include "parse-datetime.h" +#include "idx.h" #include "intprops.h" #include "timespec.h" #include "verify.h" @@ -139,7 +140,7 @@ typedef struct { bool negative; intmax_t value; - ptrdiff_t digits; + idx_t digits; } textint; /* An entry in the lexical lookup table. */ @@ -212,12 +213,12 @@ typedef struct /* Presence or counts of nonterminals of various flavors parsed so far. */ bool timespec_seen; bool rels_seen; - ptrdiff_t dates_seen; - ptrdiff_t days_seen; - ptrdiff_t local_zones_seen; - ptrdiff_t dsts_seen; - ptrdiff_t times_seen; - ptrdiff_t zones_seen; + idx_t dates_seen; + idx_t days_seen; + idx_t local_zones_seen; + idx_t dsts_seen; + idx_t times_seen; + idx_t zones_seen; bool year_seen; /* Print debugging output to stderr. */ @@ -1334,7 +1335,7 @@ lookup_word (parser_control const *pc, char *word) { char *p; char *q; - ptrdiff_t wordlen; + idx_t wordlen; table const *tp; bool period_found; bool abbrev; @@ -1514,7 +1515,7 @@ yylex (union YYSTYPE *lvalp, parser_control *pc) if (c != '(') return to_uchar (*pc->input++); - ptrdiff_t count = 0; + idx_t count = 0; do { c = *pc->input++; @@ -1750,7 +1751,7 @@ parse_datetime2 (struct timespec *result, char const *p, if (strncmp (p, "TZ=\"", 4) == 0) { char const *tzbase = p + 4; - ptrdiff_t tzsize = 1; + idx_t tzsize = 1; char const *s; for (s = tzbase; *s; s++, tzsize++) diff --git a/modules/parse-datetime b/modules/parse-datetime index a16b90f..820bc6c 100644 --- a/modules/parse-datetime +++ b/modules/parse-datetime @@ -15,6 +15,7 @@ c-ctype stdbool gettime gettext-h +idx intprops inttypes mktime -- 2.7.4
>From f09bd3c4dfa248da10dbec6ac5708f61239dca09 Mon Sep 17 00:00:00 2001 From: Bruno Haible <br...@clisp.org> Date: Sat, 5 Dec 2020 11:53:00 +0100 Subject: [PATCH 5/6] time_rz: Use idx_t for nonnegative ptrdiff_t variables. * lib/time_rz.c: Include idx.h. (save_abbr): Mark zone_size as nonnegative. * modules/time_rz (Depends-on): Add idx. --- ChangeLog | 7 +++++++ lib/time_rz.c | 3 ++- modules/time_rz | 1 + 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 57dea1d..9c0fef9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,12 @@ 2020-12-05 Bruno Haible <br...@clisp.org> + time_rz: Use idx_t for nonnegative ptrdiff_t variables. + * lib/time_rz.c: Include idx.h. + (save_abbr): Mark zone_size as nonnegative. + * modules/time_rz (Depends-on): Add idx. + +2020-12-05 Bruno Haible <br...@clisp.org> + parse-datetime: Use idx_t for nonnegative ptrdiff_t variables. * lib/parse-datetime.y: Include idx.h. (textint): Mark digits as nonnegative. diff --git a/lib/time_rz.c b/lib/time_rz.c index a33b807..6e70ca9 100644 --- a/lib/time_rz.c +++ b/lib/time_rz.c @@ -33,6 +33,7 @@ #include <string.h> #include "flexmember.h" +#include "idx.h" #include "time-internal.h" /* The approximate size to use for small allocation requests. This is @@ -120,7 +121,7 @@ save_abbr (timezone_t tz, struct tm *tm) { if (! (*zone_copy || (zone_copy == tz->abbrs && tz->tz_is_set))) { - ptrdiff_t zone_size = strlen (zone) + 1; + idx_t zone_size = strlen (zone) + 1; if (zone_size < tz->abbrs + ABBR_SIZE_MIN - zone_copy) extend_abbrs (zone_copy, zone, zone_size); else diff --git a/modules/time_rz b/modules/time_rz index 699a61d..6a2b23c 100644 --- a/modules/time_rz +++ b/modules/time_rz @@ -22,6 +22,7 @@ c99 extensions time flexmember [test $HAVE_TIMEZONE_T = 0] +idx [test $HAVE_TIMEZONE_T = 0] setenv [test $HAVE_TIMEZONE_T = 0] stdbool [test $HAVE_TIMEZONE_T = 0] time_r [test $HAVE_TIMEZONE_T = 0] -- 2.7.4
>From fe6ed193c3eb8bf99aa6440158bbd4f7ae8122f0 Mon Sep 17 00:00:00 2001 From: Bruno Haible <br...@clisp.org> Date: Sat, 5 Dec 2020 11:54:20 +0100 Subject: [PATCH 6/6] filenamecat-tests: Use idx_t for nonnegative ptrdiff_t variables. * tests/test-filenamecat.c: Include idx.h. (main): Mark prefixlen as nonnegative. * modules/filenamecat-tests (Depends-on): Add idx. --- ChangeLog | 7 +++++++ modules/filenamecat-tests | 1 + tests/test-filenamecat.c | 4 +++- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 9c0fef9..5364c15 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,12 @@ 2020-12-05 Bruno Haible <br...@clisp.org> + filenamecat-tests: Use idx_t for nonnegative ptrdiff_t variables. + * tests/test-filenamecat.c: Include idx.h. + (main): Mark prefixlen as nonnegative. + * modules/filenamecat-tests (Depends-on): Add idx. + +2020-12-05 Bruno Haible <br...@clisp.org> + time_rz: Use idx_t for nonnegative ptrdiff_t variables. * lib/time_rz.c: Include idx.h. (save_abbr): Mark zone_size as nonnegative. diff --git a/modules/filenamecat-tests b/modules/filenamecat-tests index 14d3c17..7b2a98f 100644 --- a/modules/filenamecat-tests +++ b/modules/filenamecat-tests @@ -2,6 +2,7 @@ Files: tests/test-filenamecat.c Depends-on: +idx stdbool configure.ac: diff --git a/tests/test-filenamecat.c b/tests/test-filenamecat.c index a6f236e..40259dc 100644 --- a/tests/test-filenamecat.c +++ b/tests/test-filenamecat.c @@ -21,6 +21,8 @@ #include "filenamecat.h" +#include "idx.h" + #include <stdbool.h> #include <stddef.h> #include <stdio.h> @@ -54,7 +56,7 @@ main (int argc _GL_UNUSED, char *argv[]) char *base_in_result; char const *const *t = tests[i]; char *res = file_name_concat (t[0], t[1], &base_in_result); - ptrdiff_t prefixlen = base_in_result - res; + idx_t prefixlen = base_in_result - res; size_t t0len = strlen (t[0]); size_t reslen = strlen (res); if (strcmp (res, t[2]) != 0) -- 2.7.4