aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Eggert2024-06-30 11:31:55 +0100
committerPaul Eggert2024-06-30 11:31:55 +0100
commit7f89fe8a342d7b4e8800d0ef333fb6759b58ccb5 (patch)
treeba5387098f92400c668d802efdd1480c5fd89ec3
parentc6a052f2fe53a26cdb0f3624a0b9af5201f3c487 (diff)
downloademacs-7f89fe8a342d7b4e8800d0ef333fb6759b58ccb5.tar.gz
emacs-7f89fe8a342d7b4e8800d0ef333fb6759b58ccb5.zip
Update from gnulib
-rw-r--r--lib/strnlen.c16
1 files changed, 10 insertions, 6 deletions
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 @@
1/* Find the length of STRING, but scan at most MAXLEN characters. 1/* Find the length of STRING, but scan at most MAXLEN characters.
2 Copyright (C) 2005-2007, 2009-2024 Free Software Foundation, Inc. 2 Copyright (C) 2005-2007, 2009-2024 Free Software Foundation, Inc.
3 Written by Simon Josefsson.
4 3
5 This file is free software: you can redistribute it and/or modify 4 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as 5 it under the terms of the GNU Lesser General Public License as
@@ -19,12 +18,17 @@
19 18
20#include <string.h> 19#include <string.h>
21 20
22/* Find the length of STRING, but scan at most MAXLEN characters. 21/* Find the length of S, but scan at most MAXLEN bytes.
23 If no '\0' terminator is found in that many characters, return MAXLEN. */ 22 S must be a string if it starts with fewer than MAXLEN initialized bytes.
23 If no '\0' terminator is found in that many bytes, return MAXLEN. */
24 24
25size_t 25size_t
26strnlen (const char *string, size_t maxlen) 26strnlen (const char *s, size_t maxlen)
27{ 27{
28 const char *end = memchr (string, '\0', maxlen); 28 /* Do not use memchr, because on some platforms memchr has
29 return end ? (size_t) (end - string) : maxlen; 29 undefined behavior if MAXLEN exceeds the number of bytes in S. */
30 size_t i;
31 for (i = 0; i < maxlen && s[i]; i++)
32 continue;
33 return i;
30} 34}