public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [review] manual: Clarify strnlen, wcsnlen, strndup null termination behavior
@ 2019-10-30 10:25 Florian Weimer (Code Review)
  2019-10-30 10:44 ` Andreas Schwab
                   ` (3 more replies)
  0 siblings, 4 replies; 24+ messages in thread
From: Florian Weimer (Code Review) @ 2019-10-30 10:25 UTC (permalink / raw)
  To: libc-alpha

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/glibc/+/444
......................................................................

manual: Clarify strnlen, wcsnlen, strndup null termination behavior

It is required that the inputs are arrays, as reading is not
guaranteed to stop on the first null byte.

Change-Id: Ia3e68bc2d4d7e967df141702fb2f600cbd4a6432
---
M manual/string.texi
1 file changed, 10 insertions(+), 0 deletions(-)



diff --git a/manual/string.texi b/manual/string.texi
index a1c58e5..ba8a588 100644
--- a/manual/string.texi
+++ b/manual/string.texi
@@ -328,6 +328,10 @@
     @result{} 5
 @end smallexample
 
+Note that @var{s} must be an array of at least @var{maxlen} bytes.  It
+is undefined to call @code{strnlen} on a shorter array, even if it is
+known that the shorter array contains a null terminator.
+
 This function is a GNU extension and is declared in @file{string.h}.
 @end deftypefun
 
@@ -336,6 +340,8 @@
 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
 @code{wcsnlen} is the wide character equivalent to @code{strnlen}.  The
 @var{maxlen} parameter specifies the maximum number of wide characters.
+Similar to @code{strnlen}, @var{ws} must point to an array of at least
+@var{maxlen} wide characters.
 
 This function is a GNU extension and is declared in @file{wchar.h}.
 @end deftypefun
@@ -919,6 +925,10 @@
 copies just the first @var{size} bytes and adds a closing null byte.
 Otherwise all bytes are copied and the string is terminated.
 
+Note that @var{s} must be an array of at least @var{size} bytes.  It
+is undefined to call @code{strndup} on a shorter array, even if it is
+known that the shorter array contains a null terminator.
+
 This function differs from @code{strncpy} in that it always terminates
 the destination string.
 

-- 
Gerrit-Project: glibc
Gerrit-Branch: master
Gerrit-Change-Id: Ia3e68bc2d4d7e967df141702fb2f600cbd4a6432
Gerrit-Change-Number: 444
Gerrit-PatchSet: 1
Gerrit-Owner: Florian Weimer <fweimer@redhat.com>
Gerrit-MessageType: newchange

^ permalink raw reply	[flat|nested] 24+ messages in thread

end of thread, other threads:[~2019-11-29 18:20 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-30 10:25 [review] manual: Clarify strnlen, wcsnlen, strndup null termination behavior Florian Weimer (Code Review)
2019-10-30 10:44 ` Andreas Schwab
2019-10-30 10:55   ` Florian Weimer
2019-10-30 11:00     ` Andreas Schwab
2019-10-30 11:03       ` Florian Weimer
2019-10-30 11:10         ` Andreas Schwab
2019-10-30 12:01           ` Zack Weinberg
2019-10-30 16:20             ` Andreas Schwab
2019-10-30 16:31               ` Zack Weinberg
2019-10-30 16:47                 ` Andreas Schwab
2019-10-30 16:58                   ` Zack Weinberg
2019-10-30 17:26                     ` Andreas Schwab
2019-10-30 18:12                       ` Zack Weinberg
2019-10-30 18:36                         ` Florian Weimer
2019-10-30 17:24             ` Joseph Myers
2019-11-28  9:43         ` Florian Weimer
2019-11-28 15:56           ` Carlos O'Donell
2019-11-28 15:58             ` Carlos O'Donell
2019-11-28 18:23               ` Rich Felker
2019-11-28 18:38                 ` Szabolcs Nagy
2019-11-29 18:20                   ` Martin Sebor
2019-11-27 19:08 ` Carlos O'Donell (Code Review)
2019-11-27 19:14 ` Florian Weimer (Code Review)
2019-11-27 22:11 ` Carlos O'Donell (Code Review)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).