]> Savannah Git Hosting - gnulib.git/commitdiff
strnlen: avoid undefined memcmp behavior stable-202307
authorPaul Eggert <eggert@cs.ucla.edu>
Sun, 30 Jun 2024 10:27:48 +0000 (11:27 +0100)
committerBruno Haible <bruno@clisp.org>
Sun, 30 Jun 2024 22:36:53 +0000 (00:36 +0200)
Problem reported by Po Lu in:
https://lists.gnu.org/r/bug-gnulib/2024-06/msg00288.html
* config/srclist.txt: Don’t mention strnlen.c even in a comment,
as the Gnulib and glibc implementations have diverged for
portability reasons, and they’re never likely to merge.
* lib/strnlen.c (strnlen): Avoid undefined behavior if
the array S points to has fewer than MAXLEN bytes.

ChangeLog
config/srclist.txt
lib/strnlen.c

index 210595ae2437429d6beca40c2fb010caeb6badd7..6c77deeda4b6e39e1dc4c2989bbf56dc1d6e32b0 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2024-06-30  Paul Eggert  <eggert@cs.ucla.edu>
+
+       strnlen: avoid undefined memcmp behavior
+       Problem reported by Po Lu in:
+       https://lists.gnu.org/r/bug-gnulib/2024-06/msg00288.html
+       * config/srclist.txt: Don’t mention strnlen.c even in a comment,
+       as the Gnulib and glibc implementations have diverged for
+       portability reasons, and they’re never likely to merge.
+       * lib/strnlen.c (strnlen): Avoid undefined behavior if
+       the array S points to has fewer than MAXLEN bytes.
+
 2024-06-28  Bruno Haible  <bruno@clisp.org>
 
        time: Fix test failure on FreeBSD.
index 700312805893086fd805e30afb3709c4b81d167e..9f60b4abc9239a9ce85a84f4db0ff5400cad6719 100644 (file)
@@ -190,7 +190,6 @@ $LIBCSRC time/mktime-internal.h             lib
 #$LIBCSRC string/strcasecmp.c                  lib gpl
 #$LIBCSRC string/strchrnul.c                   lib gpl
 #$LIBCSRC string/strerror.c                    lib gpl
-#$LIBCSRC string/strnlen.c                     lib gpl
 #$LIBCSRC sysdeps/posix/gettimeofday.c         lib gpl
 #$LIBCSRC sysdeps/posix/rename.c               lib gpl
 #$LIBCSRC sysdeps/unix/mkdir.c                 lib gpl
index 09c010eac42ff827a496be94a71132c8bab00d46..5231e4c595da709406f215a4c9dbd9619e670e61 100644 (file)
@@ -1,6 +1,5 @@
 /* Find the length of STRING, but scan at most MAXLEN characters.
-   Copyright (C) 2005-2007, 2009-2023 Free Software Foundation, Inc.
-   Written by Simon Josefsson.
+   Copyright (C) 2005-2007, 2009-2024 Free Software Foundation, Inc.
 
    This file is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as
 
 #include <string.h>
 
-/* Find the length of STRING, but scan at most MAXLEN characters.
-   If no '\0' terminator is found in that many characters, return MAXLEN.  */
+/* Find the length of S, but scan at most MAXLEN bytes.
+   S must be a string if it starts with fewer than MAXLEN initialized bytes.
+   If no '\0' terminator is found in that many bytes, return MAXLEN.  */
 
 size_t
-strnlen (const char *string, size_t maxlen)
+strnlen (const char *s, size_t maxlen)
 {
-  const char *end = memchr (string, '\0', maxlen);
-  return end ? (size_t) (end - string) : maxlen;
+  /* Do not use memchr, because on some platforms memchr has
+     undefined behavior if MAXLEN exceeds the number of bytes in S.  */
+  size_t i;
+  for (i = 0; i < maxlen && s[i]; i++)
+    continue;
+  return i;
 }