From: "Alejandro Colomar (man-pages)" <alx.manpages@gmail.com>
To: Stefan Kanthak <stefan.kanthak@nexgo.de>
Cc: linux-man@vger.kernel.org, mtk.manpages@gmail.com
Subject: Re: wcstok(3) code sample
Date: Sun, 25 Jul 2021 22:25:54 +0200 [thread overview]
Message-ID: <69a547a9-7d25-73bf-e1f4-7cfbfdcb79aa@gmail.com> (raw)
In-Reply-To: <8514C525E9C345FEA377AC5912010871@H270>
Hi Stefan,
On 7/12/21 11:07 AM, Stefan Kanthak wrote:
> Hi,
>
> the examples section of wcstok(3) shows the following code
> which exhibits undefined behaviour and typically segfaults:
>
> <https://man7.org/linux/man-pages/man3/wcstok.3.html#EXAMPLES>
>
> | wchar_t *wcs = ...;
> | wchar_t *token;
> | wchar_t *state;
> | for (token = wcstok(wcs, " \t\n", &state);
> | token != NULL;
> | token = wcstok(NULL, " \t\n", &state)) {
> | ...
> | }
>
> The string literal pointed to by wcs is read-only, and an
> attempt to modify a string literal results in undefined
> behaviour; wcstok() but writes NULs into its input string.
>
> FIX: replace the first line with either
>
> | wchar_t *wcs = strdup(...);
>
> or
>
> | wchar_t wcs[] = ...;
That code is a bit unfortunate. It is not a complete program, so it can
be interpreted in different ways, one of them the one you said, which
results in UB.
I guess the intent of the code was that wcs was assigned a pointer to a
wchar_t * (not a literal), and therefore, it would be correct. The code
predates version control, so we'll never know...
Would you mind sending a complete example?
Thanks,
Alex
>
> regards
> Stefan
>
--
Alejandro Colomar
Linux man-pages comaintainer; https://www.kernel.org/doc/man-pages/
http://www.alejandro-colomar.es/
next prev parent reply other threads:[~2021-07-25 20:26 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-07-12 9:07 wcstok(3) code sample Stefan Kanthak
2021-07-12 16:36 ` Jakub Wilk
2021-07-25 20:25 ` Alejandro Colomar (man-pages) [this message]
2021-07-26 13:13 ` Stefan Kanthak
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=69a547a9-7d25-73bf-e1f4-7cfbfdcb79aa@gmail.com \
--to=alx.manpages@gmail.com \
--cc=linux-man@vger.kernel.org \
--cc=mtk.manpages@gmail.com \
--cc=stefan.kanthak@nexgo.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox