public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: fweimer@redhat.com (Florian Weimer)
To: libc-alpha@sourceware.org
Subject: [PATCH] manual: Various fixes to the mbstouwcs example
Date: Wed, 04 Apr 2018 13:57:00 -0000	[thread overview]
Message-ID: <20180404135740.A283E406F5A23@oldenburg.str.redhat.com> (raw)

The example did not work because the NUL byte was not converted, and
mbrtowc was called with a zero-length input string.  This results in a
(size_t) -2 return value, so the function always returns NULL.

The size computation for the heap allocation of the result was
incorrect because it did not deal with integer overflow.

Error checking was missing, and the allocated memory was not freed on
error paths.  All error returns now set errno.  (Note that there is an
assumption that free does not clobber errno.)

The slightly unportable comparision against (size_t) -2 to catch both
(size_t) -1 and (size_t) -2 return values is gone as well.

2018-04-04  Florian Weimer  <fweimer@redhat.com>

	* manual/examples/mbstouwcs.c (mbstouwcs): Fix loop termination,
	integer overflow, memory leak on error, and indeterminate errno
	value.
	* manual/charset.texi (Converting a Character): Adjust.

diff --git a/manual/charset.texi b/manual/charset.texi
index b37fac4df1..270995f602 100644
--- a/manual/charset.texi
+++ b/manual/charset.texi
@@ -681,9 +681,7 @@ is declared in @file{wchar.h}.
 
 Use of @code{mbrtowc} is straightforward.  A function that copies a
 multibyte string into a wide character string while at the same time
-converting all lowercase characters into uppercase could look like this
-(this is not the final version, just an example; it has no error
-checking, and sometimes leaks memory):
+converting all lowercase characters into uppercase could look like this:
 
 @smallexample
 @include mbstouwcs.c.texi
diff --git a/manual/examples/mbstouwcs.c b/manual/examples/mbstouwcs.c
index 3a8b9a65f9..4012606bf1 100644
--- a/manual/examples/mbstouwcs.c
+++ b/manual/examples/mbstouwcs.c
@@ -7,8 +7,11 @@
 wchar_t *
 mbstouwcs (const char *s)
 {
-  size_t len = strlen (s);
-  wchar_t *result = malloc ((len + 1) * sizeof (wchar_t));
+  /* Include the NUL terminator in the conversion.  */
+  size_t len = strlen (s) + 1;
+  wchar_t *result = reallocarray (NULL, len + 1, sizeof (wchar_t));
+  if (result == NULL)
+    return NULL;
   wchar_t *wcp = result;
   wchar_t tmp[1];
   mbstate_t state;
@@ -17,9 +20,19 @@ mbstouwcs (const char *s)
   memset (&state, '\0', sizeof (state));
   while ((nbytes = mbrtowc (tmp, s, len, &state)) > 0)
     {
-      if (nbytes >= (size_t) -2)
-        /* Invalid input string.  */
-        return NULL;
+      if (nbytes == (size_t) -2)
+        {
+          /* Truncated input string.  */
+          errno = EILSEQ;
+          free (result);
+          return NULL;
+        }
+      if (nbytes >= (size_t) -1)
+        {
+          /* Some other error (including EILSEQ).  */
+          free (result);
+          return NULL;
+        }
       *wcp++ = towupper (*tmp);
       len -= nbytes;
       s += nbytes;

             reply	other threads:[~2018-04-04 13:57 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-04-04 13:57 Florian Weimer [this message]
2018-04-04 14:19 ` Andreas Schwab
2018-04-05  9:56   ` Florian Weimer
2018-04-05 10:08     ` Andreas Schwab
2018-04-05 10:13       ` Florian Weimer
2018-04-05 10:49         ` Andreas Schwab

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=20180404135740.A283E406F5A23@oldenburg.str.redhat.com \
    --to=fweimer@redhat.com \
    --cc=libc-alpha@sourceware.org \
    /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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).