From: Paul Eggert Date: Sun, 30 Jun 2024 10:31:55 +0000 (+0100) Subject: Update from gnulib X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=9e4e4ba3015c6e187be17ac700ca91c0eb7da19d;p=emacs.git Update from gnulib (cherry picked from commit 7f89fe8a342d7b4e8800d0ef333fb6759b58ccb5) --- diff --git a/lib/strnlen.c b/lib/strnlen.c index 80857ec22b0..5231e4c595d 100644 --- a/lib/strnlen.c +++ b/lib/strnlen.c @@ -1,6 +1,5 @@ /* Find the length of STRING, but scan at most MAXLEN characters. Copyright (C) 2005-2007, 2009-2024 Free Software Foundation, Inc. - Written by Simon Josefsson. This file is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as @@ -19,12 +18,17 @@ #include -/* 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; }