Alejandro Colomar wrote: > > These changes look OK. Can you please send them in "git format-patch" > > format, as a mail attachment (not inline in a mail)? That would make it > > easy for me to apply. > > Sure; please find it attached (it's the patch 2/2). I've also attached > patch 1/2, which are some places I had changed to use strnul(3),
Thanks. I combined them, eliminated changes to files shared with glibc (in order to minimize future sync efforts) and to relocatable.c (not worth the trouble), and added 'strnul' to the respective module dependencies. This is what I'm committing in your name: 2026-02-23 Alejandro Colomar <[email protected]> Use strnul in a few places. * lib/argz.c (argz_next): Use strnul. * lib/cpu-supports.c (hwcap_allowed): Likewise. * lib/file-has-acl.c (aclinfo_has_xattr): Likewise. * lib/inet_ntop.c (inet_ntop6): Likewise. * lib/link.c (link): Likewise. * lib/localename-unsafe.c (enum_locales_fn): Likewise. * lib/mbspcasecmp.c (mbspcasecmp): Likewise. * lib/opendir.c (opendir): Likewise. * lib/parse-duration.c (parse_year_month_day, parse_hour_minute_second, trim): Likewise. * lib/setlocale.c (setlocale_unixlike): Likewise. * lib/strftime.c (__strftime_internal): Likewise. * lib/striconv.c (str_cd_iconv): Likewise. * lib/strncat.c (strncat): Likewise. * lib/term-style-control.c (log_signal_handler_called, tcsetattr_failed): Likewise. * lib/time_rz.c (save_abbr): Likewise. * lib/vc-mtime.c (git_mtime, max_vc_mtime): Likewise. * tests/test-savedir.c (test_savedir_sort_none, test_savedir_sort_name): Likewise. * modules/argz (Depends-on): Add strnul. * modules/cpu-supports (Depends-on): Likewise. * modules/file-has-acl (Depends-on): Likewise. * modules/inet_ntop (Depends-on): Likewise. * modules/link (Depends-on): Likewise. * modules/localename-unsafe (Depends-on): Likewise. * modules/localename-unsafe-limited (Depends-on): Likewise. * modules/mbspcasecmp (Depends-on): Likewise. * modules/opendir (Depends-on): Likewise. * modules/parse-duration (Depends-on): Likewise. * modules/setlocale (Depends-on): Likewise. * modules/nstrftime (Depends-on): Likewise. * modules/nstrftime-limited (Depends-on): Likewise. * modules/c-nstrftime (Depends-on): Likewise. * modules/fprintftime (Depends-on): Likewise. * modules/striconv (Depends-on): Likewise. * modules/strncat (Depends-on): Likewise. * modules/term-style-control (Depends-on): Likewise. * modules/time_rz (Depends-on): Likewise. * modules/vc-mtime (Depends-on): Likewise. * modules/savedir-tests (Depends-on): Likewise. diff --git a/lib/argz.c b/lib/argz.c index 55303dfb19..0d36c741d9 100644 --- a/lib/argz.c +++ b/lib/argz.c @@ -175,7 +175,7 @@ argz_next (const char *argz, size_t argz_len, const char *entry) if (entry) { if (entry < argz + argz_len) - entry = strchr (entry, '\0') + 1; + entry = strnul (entry) + 1; return entry >= argz + argz_len ? NULL : (char *) entry; } diff --git a/lib/cpu-supports.c b/lib/cpu-supports.c index 9057d151eb..a1505f8bd6 100644 --- a/lib/cpu-supports.c +++ b/lib/cpu-supports.c @@ -66,7 +66,7 @@ hwcap_allowed (char const *glibc_hwcap) char const *sentinel = strchr (hwcaps, ':'); if (! sentinel) - sentinel = hwcaps + strlen (hwcaps); + sentinel = strnul (hwcaps); char const *cap = hwcaps; while ((cap = strstr (cap, glibc_hwcap)) && cap < sentinel) { /* Check it's not a partial match. */ diff --git a/lib/file-has-acl.c b/lib/file-has-acl.c index 26d3e358de..3269d7f71b 100644 --- a/lib/file-has-acl.c +++ b/lib/file-has-acl.c @@ -145,7 +145,7 @@ aclinfo_has_xattr (struct aclinfo const *ai, char const *xattr) if (0 < ai->size) { char const *blim = ai->buf + ai->size; - for (char const *b = ai->buf; b < blim; b += strlen (b) + 1) + for (char const *b = ai->buf; b < blim; b = strnul (b) + 1) for (char const *a = xattr; *a == *b; a++, b++) if (!*a) return true; diff --git a/lib/inet_ntop.c b/lib/inet_ntop.c index 599ca4fb72..dff25c5f6d 100644 --- a/lib/inet_ntop.c +++ b/lib/inet_ntop.c @@ -225,7 +225,7 @@ inet_ntop6 (const unsigned char *src, char *dst, socklen_t size) { if (!inet_ntop4 (src + 12, tp, sizeof tmp - (tp - tmp))) return (NULL); - tp += strlen (tp); + tp = strnul (tp); break; } { diff --git a/lib/link.c b/lib/link.c index 9053eef28f..6198929943 100644 --- a/lib/link.c +++ b/lib/link.c @@ -113,7 +113,7 @@ link (const char *file1, const char *file2) return -1; { struct stat st; - char *p = strchr (dir, '\0'); + char *p = strnul (dir); while (dir < p && (*--p != '/' && *p != '\\')); *p = '\0'; if (p != dir && stat (dir, &st) != 0 && errno != EOVERFLOW) diff --git a/lib/localename-unsafe.c b/lib/localename-unsafe.c index 12748cba28..d490fe89be 100644 --- a/lib/localename-unsafe.c +++ b/lib/localename-unsafe.c @@ -2558,7 +2558,7 @@ enum_locales_fn (LPSTR locale_num_str) { strcat (locval, "_"); if (GetLocaleInfo (try_lcid, LOCALE_SENGCOUNTRY, - locval + strlen (locval), LOCALE_NAME_MAX_LENGTH)) + strnul (locval), LOCALE_NAME_MAX_LENGTH)) { size_t locval_len = strlen (locval); diff --git a/lib/mbspcasecmp.c b/lib/mbspcasecmp.c index 696cb89fb2..a72a20d4fe 100644 --- a/lib/mbspcasecmp.c +++ b/lib/mbspcasecmp.c @@ -45,7 +45,7 @@ mbspcasecmp (const char *string, const char *prefix) mbsncasecmp (string, prefix, mbslen (prefix)) just with small optimizations. */ if (string == prefix) - return (char *) (string + strlen (string)); + return (char *) strnul (string); const char *iter1 = string; const char *iter2 = prefix; diff --git a/lib/opendir.c b/lib/opendir.c index 3890206b79..ea3d2ef166 100644 --- a/lib/opendir.c +++ b/lib/opendir.c @@ -111,7 +111,7 @@ opendir (const char *dir_name) { char *p; - p = dir_name_mask + strlen (dir_name_mask); + p = strnul (dir_name_mask); if (p > dir_name_mask && !ISSLASH (p[-1])) *p++ = '\\'; *p++ = '*'; diff --git a/lib/parse-duration.c b/lib/parse-duration.c index 5bea54f4e6..5abf6a1331 100644 --- a/lib/parse-duration.c +++ b/lib/parse-duration.c @@ -192,7 +192,7 @@ parse_year_month_day (cch_t * pz, cch_t * ps) res = parse_scaled_value (res, &pz, ps, SEC_PER_MONTH); pz++; /* over the second '-' */ - ps = pz + strlen (pz); + ps = strnul (pz); return parse_scaled_value (res, &pz, ps, SEC_PER_DAY); } @@ -292,7 +292,7 @@ parse_hour_minute_second (cch_t * pz, cch_t * ps) res = parse_scaled_value (res, &pz, ps, SEC_PER_MIN); pz++; - ps = pz + strlen (pz); + ps = strnul (pz); return parse_scaled_value (res, &pz, ps, 1); } @@ -408,7 +408,7 @@ trim (char * pz) /* trim trailing white space */ { - char * pe = pz + strlen (pz); + char * pe = strnul (pz); while ((pe > pz) && isspace ((unsigned char)pe[-1])) pe--; *pe = NUL; diff --git a/lib/setlocale.c b/lib/setlocale.c index c4f006105d..3871a3fa16 100644 --- a/lib/setlocale.c +++ b/lib/setlocale.c @@ -767,7 +767,7 @@ setlocale_unixlike (int category, const char *locale) const char *territory_start = underscore + 1; const char *territory_end = strchr (territory_start, '@'); if (territory_end == NULL) - territory_end = territory_start + strlen (territory_start); + territory_end = strnul (territory_start); char ll_buf[64]; memcpy (ll_buf, llCC_buf, underscore - llCC_buf); diff --git a/lib/strftime.c b/lib/strftime.c index 8d32023729..33fa2e017b 100644 --- a/lib/strftime.c +++ b/lib/strftime.c @@ -1358,7 +1358,7 @@ __strftime_internal (STREAM_OR_CHAR_T *s, STRFTIME_ARG (size_t maxsize) mbstate_t mbstate = mbstate_zero; if (! format_end) - format_end = f + strlen (f) + 1; + format_end = strnul (f) + 1; size_t fsize = format_end - f; size_t len = 0; diff --git a/lib/striconv.c b/lib/striconv.c index 6aded51e14..bf169822d8 100644 --- a/lib/striconv.c +++ b/lib/striconv.c @@ -258,7 +258,7 @@ str_cd_iconv (const char *src, iconv_t cd) for (;;) { - /* Here inptr + inbytes_remaining = src + strlen (src), + /* Here inptr + inbytes_remaining = strnul (src), outptr + outbytes_remaining = result + result_size - 1. */ size_t res = iconv (cd, (ICONV_CONST char **) &inptr, &inbytes_remaining, diff --git a/lib/strncat.c b/lib/strncat.c index 5e77ea8846..5441ac756b 100644 --- a/lib/strncat.c +++ b/lib/strncat.c @@ -23,7 +23,7 @@ char * strncat (char *dest, const char *src, size_t n) { - char *destptr = dest + strlen (dest); + char *destptr = strnul (dest); for (; n > 0 && (*destptr = *src) != '\0'; src++, destptr++, n--) ; diff --git a/lib/term-style-control.c b/lib/term-style-control.c index 9ab1c8ac08..4ed5bf7e0a 100644 --- a/lib/term-style-control.c +++ b/lib/term-style-control.c @@ -215,7 +215,7 @@ log_signal_handler_called (int sig) { char message[100]; strcpy (message, "Signal handler for signal "); - simple_signal_string (message + strlen (message), sig); + simple_signal_string (strnul (message), sig); strcat (message, " called.\n"); log_message (message); } @@ -434,9 +434,9 @@ tcsetattr_failed (char message[100], const char *caller) int errnum = errno; strcpy (message, caller); strcat (message, ": tcsetattr(fd="); - sprintf_integer (message + strlen (message), active_fd); + sprintf_integer (strnul (message), active_fd); strcat (message, ") failed, errno="); - simple_errno_string (message + strlen (message), errnum); + simple_errno_string (strnul (message), errnum); strcat (message, "\n"); } diff --git a/lib/time_rz.c b/lib/time_rz.c index 811240e6da..0e8ea47e79 100644 --- a/lib/time_rz.c +++ b/lib/time_rz.c @@ -146,7 +146,7 @@ save_abbr (timezone_t tz, struct tm *tm) break; } - zone_copy += strlen (zone_copy) + 1; + zone_copy = strnul (zone_copy) + 1; if (!*zone_copy && tz->next) { tz = tz->next; diff --git a/lib/vc-mtime.c b/lib/vc-mtime.c index ad47ef9e5e..208a3d2c36 100644 --- a/lib/vc-mtime.c +++ b/lib/vc-mtime.c @@ -250,7 +250,7 @@ git_mtime (struct timespec *mtime, const char *filename) char *endptr; unsigned long git_log_time; if (xstrtoul (line, &endptr, 10, &git_log_time, NULL) == LONGINT_OK - && endptr == line + strlen (line)) + && endptr == strnul (line)) { mtime->tv_sec = git_log_time; mtime->tv_nsec = 0; @@ -885,7 +885,7 @@ max_vc_mtime (struct timespec *max_of_mtimes, char *endptr; unsigned long git_log_time; if (!(xstrtoul (line, &endptr, 10, &git_log_time, NULL) == LONGINT_OK - && endptr == line + strlen (line))) + && endptr == strnul (line))) { fprintf (stderr, "vc-mtime: git log output not as expected\n"); goto git_log_fail1; diff --git a/modules/argz b/modules/argz index cb8166fe48..6f5ee3ff8f 100644 --- a/modules/argz +++ b/modules/argz @@ -14,6 +14,7 @@ realloc-posix [test -n "$ARGZ_H"] stpcpy [test -n "$ARGZ_H"] strndup [test -n "$ARGZ_H"] strnlen [test -n "$ARGZ_H"] +strnul [test -n "$ARGZ_H"] strstr [test -n "$ARGZ_H"] configure.ac: diff --git a/modules/c-nstrftime b/modules/c-nstrftime index a0e84882fc..b60b48e154 100644 --- a/modules/c-nstrftime +++ b/modules/c-nstrftime @@ -19,6 +19,7 @@ libc-config locale-h bool stdckdint-h +strnul time_rz configure.ac: diff --git a/modules/cpu-supports b/modules/cpu-supports index 8f5871d46f..1e6723f865 100644 --- a/modules/cpu-supports +++ b/modules/cpu-supports @@ -10,6 +10,7 @@ attribute bool c99 stringeq +strnul configure.ac: AC_REQUIRE([AC_C_INLINE]) diff --git a/modules/file-has-acl b/modules/file-has-acl index a128e64483..d769f2bea4 100644 --- a/modules/file-has-acl +++ b/modules/file-has-acl @@ -19,6 +19,7 @@ free-posix limits-h stdint-h stringeq +strnul malloc-posix ssize_t stat diff --git a/modules/fprintftime b/modules/fprintftime index 3700a3b45e..fd3e7073fa 100644 --- a/modules/fprintftime +++ b/modules/fprintftime @@ -10,6 +10,7 @@ Depends-on: stdio-h sys_types-h nstrftime +strnul time_rz configure.ac: diff --git a/modules/inet_ntop b/modules/inet_ntop index fa6979b2ee..bd80bde32f 100644 --- a/modules/inet_ntop +++ b/modules/inet_ntop @@ -12,6 +12,7 @@ extensions sys_socket-h [test $HAVE_INET_NTOP = 0 || test $REPLACE_INET_NTOP = 1] errno-h [test $HAVE_INET_NTOP = 0 || test $REPLACE_INET_NTOP = 1] netinet_in-h [test $HAVE_INET_NTOP = 0 || test $REPLACE_INET_NTOP = 1] +strnul [test $HAVE_INET_NTOP = 0 || test $REPLACE_INET_NTOP = 1] configure.ac: gl_FUNC_INET_NTOP diff --git a/modules/link b/modules/link index f7097b62d7..41011a9382 100644 --- a/modules/link +++ b/modules/link @@ -10,6 +10,7 @@ unistd-h free-posix [test $HAVE_LINK = 0 || test $REPLACE_LINK = 1] stat [test $HAVE_LINK = 0 || test $REPLACE_LINK = 1] strdup-posix [test $HAVE_LINK = 0 || test $REPLACE_LINK = 1] +strnul [test $HAVE_LINK = 0 || test $REPLACE_LINK = 1] sys_stat-h [test $HAVE_LINK = 0 || test $REPLACE_LINK = 1] configure.ac: diff --git a/modules/localename-unsafe b/modules/localename-unsafe index 5785dd4462..4545c347ea 100644 --- a/modules/localename-unsafe +++ b/modules/localename-unsafe @@ -19,6 +19,7 @@ locale-h strdup stringeq strncpy +strnul windows-mutex getlocalename_l-unsafe setlocale-null-unlocked diff --git a/modules/localename-unsafe-limited b/modules/localename-unsafe-limited index 1593ce2784..701170ebcf 100644 --- a/modules/localename-unsafe-limited +++ b/modules/localename-unsafe-limited @@ -18,6 +18,7 @@ extensions locale-h strdup strncpy +strnul windows-mutex getlocalename_l-unsafe-limited setlocale-null-unlocked diff --git a/modules/mbspcasecmp b/modules/mbspcasecmp index 29a5f05172..519829ce3f 100644 --- a/modules/mbspcasecmp +++ b/modules/mbspcasecmp @@ -8,6 +8,7 @@ Depends-on: c32tolower mbuiterf string-h +strnul configure.ac: gl_STRING_MODULE_INDICATOR([mbspcasecmp]) diff --git a/modules/nstrftime b/modules/nstrftime index 4cb87d28af..761f1483c0 100644 --- a/modules/nstrftime +++ b/modules/nstrftime @@ -26,6 +26,7 @@ localename-unsafe [case "$host_os" in darwin*) false ;; *) true ;; esac] bool stdckdint-h stdint-h +strnul time_rz configure.ac: diff --git a/modules/nstrftime-limited b/modules/nstrftime-limited index 18fc11a51a..c1bfef9116 100644 --- a/modules/nstrftime-limited +++ b/modules/nstrftime-limited @@ -26,6 +26,7 @@ localcharset localename-unsafe-limited [case "$host_os" in darwin*) false ;; *) true ;; esac] bool stdckdint-h +strnul time_rz configure.ac: diff --git a/modules/opendir b/modules/opendir index b9a6dfe2d7..631d138022 100644 --- a/modules/opendir +++ b/modules/opendir @@ -13,6 +13,7 @@ filename [test $HAVE_OPENDIR = 0 || test $REPLACE_OPENDIR = 1] unistd-h [test $HAVE_OPENDIR = 0 || test $REPLACE_OPENDIR = 1] closedir [test $HAVE_OPENDIR = 0 || test $REPLACE_OPENDIR = 1] dirfd [test $HAVE_OPENDIR = 0 || test $REPLACE_OPENDIR = 1] +strnul [test $HAVE_OPENDIR = 0 || test $REPLACE_OPENDIR = 1] configure.ac: gl_FUNC_OPENDIR diff --git a/modules/parse-duration b/modules/parse-duration index 6184dc4315..4aa4fb2b90 100644 --- a/modules/parse-duration +++ b/modules/parse-duration @@ -7,6 +7,7 @@ lib/parse-duration.c Depends-on: intprops +strnul configure.ac: diff --git a/modules/savedir-tests b/modules/savedir-tests index 964d5be20a..8e5433770d 100644 --- a/modules/savedir-tests +++ b/modules/savedir-tests @@ -7,6 +7,7 @@ bool creat close mkdir +strnul configure.ac: diff --git a/modules/setlocale b/modules/setlocale index 5f71838011..fd74033495 100644 --- a/modules/setlocale +++ b/modules/setlocale @@ -11,6 +11,7 @@ stringeq setlocale-fixes [test $NEED_SETLOCALE_IMPROVED = 1] localename [test $NEED_SETLOCALE_IMPROVED = 1] localename-environ [test $NEED_SETLOCALE_IMPROVED = 1] +strnul [test $NEED_SETLOCALE_IMPROVED = 1] setlocale-null [test $NEED_SETLOCALE_MTSAFE = 1] configure.ac: diff --git a/modules/striconv b/modules/striconv index 53b4b55ea5..22ff07ecb0 100644 --- a/modules/striconv +++ b/modules/striconv @@ -10,6 +10,7 @@ iconv iconv_open free-posix strdup +strnul c-strcasecmp configure.ac: diff --git a/modules/strncat b/modules/strncat index 7902267c04..b2213ec5f1 100644 --- a/modules/strncat +++ b/modules/strncat @@ -8,6 +8,7 @@ m4/mmap-anon.m4 Depends-on: string-h +strnul configure.ac: gl_FUNC_STRNCAT diff --git a/modules/term-style-control b/modules/term-style-control index 7159604ad1..d5c1529930 100644 --- a/modules/term-style-control +++ b/modules/term-style-control @@ -14,6 +14,7 @@ sigprocmask full-write fstat same-inode +strnul xalloc-die configure.ac: diff --git a/modules/time_rz b/modules/time_rz index 2479f04d17..187c477bb2 100644 --- a/modules/time_rz +++ b/modules/time_rz @@ -16,6 +16,7 @@ idx [test $HAVE_TZALLOC = 0 || test $REPLACE_LOCALTIME_RZ = 1 || test setenv [test $HAVE_TZALLOC = 0 || test $REPLACE_LOCALTIME_RZ = 1 || test $REPLACE_MKTIME_Z = 1] bool [test $HAVE_TZALLOC = 0 || test $REPLACE_LOCALTIME_RZ = 1 || test $REPLACE_MKTIME_Z = 1] stringeq [test $HAVE_TZALLOC = 0 || test $REPLACE_LOCALTIME_RZ = 1 || test $REPLACE_MKTIME_Z = 1] +strnul [test $HAVE_TZALLOC = 0 || test $REPLACE_LOCALTIME_RZ = 1 || test $REPLACE_MKTIME_Z = 1] time_r [test $HAVE_TZALLOC = 0 || test $REPLACE_LOCALTIME_RZ = 1 || test $REPLACE_MKTIME_Z = 1] timegm [test $HAVE_TZALLOC = 0 || test $REPLACE_LOCALTIME_RZ = 1 || test $REPLACE_MKTIME_Z = 1] tzset [test $HAVE_TZALLOC = 0 || test $REPLACE_LOCALTIME_RZ = 1 || test $REPLACE_MKTIME_Z = 1] diff --git a/modules/vc-mtime b/modules/vc-mtime index 44f56a4a2c..b40ef32e6e 100644 --- a/modules/vc-mtime +++ b/modules/vc-mtime @@ -10,6 +10,7 @@ time-h bool spawn-pipe stringeq +strnul wait-process execute safe-read diff --git a/tests/test-savedir.c b/tests/test-savedir.c index 95abc7833b..c95408c52f 100644 --- a/tests/test-savedir.c +++ b/tests/test-savedir.c @@ -44,7 +44,7 @@ test_savedir_sort_none (void) memset (seen, 0, sizeof seen); /* Scan through the file names. */ - for (char *namep = name_space; *namep != '\0'; namep += strlen (namep) + 1) + for (char *namep = name_space; *namep != '\0'; namep = strnul (namep) + 1) { int index = *namep - 'a'; ASSERT (strlen (namep) == 1); @@ -68,7 +68,7 @@ test_savedir_sort_name (void) /* Check that files "a" to "z" appear in order. */ for (char *namep = name_space; *namep != '\0'; - namep += strlen (namep) + 1, i += 1) + namep = strnul (namep) + 1, i += 1) { ASSERT (strlen (namep) == 1); ASSERT (*namep - 'a' == i);
