diff options
| author | Alejandro Colomar <alx.manpages@gmail.com> | 2022-08-26 22:48:26 +0200 |
|---|---|---|
| committer | Alejandro Colomar <alx@kernel.org> | 2022-11-10 00:44:59 +0100 |
| commit | 1eed67e75deff662dffce3195e55e608809eaafd (patch) | |
| tree | c17d52fa109792e24b551c519856d68a0fe67b28 /man3/string.3 | |
| parent | 6bb53a30af9960b30ca58bb62002da926f853c81 (diff) | |
| download | man-pages-1eed67e75deff662dffce3195e55e608809eaafd.tar.gz | |
Various pages: SYNOPSIS: Use VLA syntax in function parameters
The WG14 charter for C23 added one principle to the ones in
previous standards:
[
15. Application Programming Interfaces (APIs) should be
self-documenting when possible. In particular, the order of
parameters in function declarations should be arranged such that
the size of an array appears before the array. The purpose is to
allow Variable-Length Array (VLA) notation to be used. This not
only makes the code's purpose clearer to human readers, but also
makes static analysis easier. Any new APIs added to the Standard
should take this into consideration.
]
ISO C doesn't allow using VLA syntax when the parameter used for
the size of the array is declared _after_ the parameter that is a
VLa. That's a minor issue that could be easily changed in the
language without backwards-compatibility issues, and in fact it
seems to have been proposed, and not yet discarded, even if it's
not going to change in C23.
Since the manual pages SYNOPSIS are not bounded by strict C legal
syntax, but we already use some "tricks" to try to convey the most
information to the reader even if it might not be the most legal
syntax, we can also make a small compromise in this case, using
illegal syntax (at least not yet legalized) to add important
information to the function prototypes.
If we're lucky, compiler authors, and maybe even WG14 members, may
be satisfied by the syntax used in these manual pages, and may
decide to implement this feature to the language.
It seems to me a sound syntax that isn't ambiguous, even if it
deviates from the common pattern in C that declarations _always_
come before use. But it's a reasonable tradeoff.
This change will make the contract between the programmer and the
implementation clearer just by reading a prototype. For example,
size_t strlcpy(char *restrict dst, const char *restrict src,
size_t size);
vs
size_t strlcpy(char dst[restrict .size], const char *restrict src,
size_t size);
the second prototype above makes it clear that the 'dst' buffer
will be safe from overflow, but the 'src' one clearly needs to be
NUL-terminated, or it might cause UB, since nothing tells the
function how long it is.
Link: <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2611.htm>
Cc: Ingo Schwarze <schwarze@openbsd.org>
Cc: JeanHeyd Meneide <wg14@soasis.org>
Cc: Martin Uecker <uecker@tugraz.at>
Cc: <gcc@gcc.gnu.org>
Signed-off-by: Alejandro Colomar <alx.manpages@gmail.com>
Diffstat (limited to 'man3/string.3')
| -rw-r--r-- | man3/string.3 | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/man3/string.3 b/man3/string.3 index 8cd7940372..32e9d8f5a0 100644 --- a/man3/string.3 +++ b/man3/string.3 @@ -26,7 +26,8 @@ and .I s2 ignoring case. .TP -.BI "int strncasecmp(const char *" s1 ", const char *" s2 ", size_t " n ); +.BI "int strncasecmp(const char " s1 [. n "], const char " s2 [. n "], \ +size_t " n ); Compare the first .I n bytes of the strings @@ -112,8 +113,11 @@ Randomly swap the characters in Return the length of the string .IR s . .TP -.BI "char *strncat(char *restrict " dest ", const char *restrict " src \ -", size_t " n ); +.nf +.BI "char *strncat(char " dest "[restrict ." n "], \ +const char " src "[restrict ." n ], +.BI " size_t " n ); +.fi Append at most .I n bytes from the string @@ -123,7 +127,7 @@ to the string returning a pointer to .IR dest . .TP -.BI "int strncmp(const char *" s1 ", const char *" s2 ", size_t " n ); +.BI "int strncmp(const char " s1 [. n "], const char " s2 [. n "], size_t " n ); Compare at most .I n bytes of the strings @@ -131,8 +135,11 @@ bytes of the strings and .IR s2 . .TP -.BI "char *strncpy(char *restrict " dest ", const char *restrict " src \ -", size_t " n ); +.nf +.BI "char *strncpy(char " dest "[restrict ." n "], \ +const char " src "[restrict ." n ], +.BI " size_t " n ); +.fi Copy at most .I n bytes from string @@ -179,8 +186,11 @@ Extract tokens from the string that are delimited by one of the bytes in .IR delim . .TP -.BI "size_t strxfrm(char *restrict " dst ", const char *restrict " src \ -", size_t " n ); +.nf +.BI "size_t strxfrm(char " dst "[restrict ." n "], \ +const char " src "[restrict ." n ], +.BI " size_t " n ); +.fi Transforms .I src to the current locale and copies the first |
