[PATCH] manual: Various fixes to the mbstouwcs example

Florian Weimer fweimer@redhat.com
Thu Apr 5 09:56:00 GMT 2018


On 04/04/2018 04:19 PM, Andreas Schwab wrote:
> On Apr 04 2018, fweimer@redhat.com (Florian Weimer) wrote:
> 
>> 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));
> 
> What is result[len] used for?

Uhm.  The terminating null wide character.  At least that was the 
intent.  I see that it is not what is happening here.

Oh well.  The manual says:

  Use of @code{mbrtowc} is straightforward.

  The use of @code{mbrtowc} should be clear.

  The only non-obvious thing about @code{mbrtowc}

That should have been a clear warning sign.

I've looked at how various corner cases are specified in ISO C11 and 
updated the manual accordingly.  What about the attached patch?

Thanks,
Florian
-------------- next part --------------
A non-text attachment was scrubbed...
Name: mb.patch
Type: text/x-patch
Size: 9139 bytes
Desc: not available
URL: <http://sourceware.org/pipermail/libc-alpha/attachments/20180405/ae1eb3c9/attachment.bin>


More information about the Libc-alpha mailing list