On Fri, Jul 08, 2011 at 04:51:47PM +0100, Nicholas Marriott wrote:
> Except it isn't sizeof(b) or '\0'.

Ooops, right. It's counting in units of ints.

> This actually passes the right length to mbstowcs as well:

This looks good to me.

> Index: wcsdup.3
> ===================================================================
> RCS file: /cvs/src/lib/libc/string/wcsdup.3,v
> retrieving revision 1.2
> diff -u -p -r1.2 wcsdup.3
> --- wcsdup.3  5 Jul 2011 19:01:31 -0000       1.2
> +++ wcsdup.3  8 Jul 2011 15:50:50 -0000
> @@ -61,9 +61,17 @@ The following will point
>  to an allocated area of memory containing the nul-terminated string
>  .Qq foobar :
>  .Bd -literal -offset indent
> -wchar_t *p;
> +const char   *o = "foobar";
> +wchar_t              *p, b[32];
> +size_t                blen;
>  
> -if ((p = wcsdup(L"foobar")) == NULL) {
> +blen = sizeof(b) / sizeof(b[0]);
> +if (mbstowcs(b, o, blen) == (size_t)-1) {
> +     fprintf(stderr, "Failed to convert string.\en");
> +     exit(1);
> +}
> +b[blen - 1] = 0;
> +if ((p = wcsdup(b)) == NULL) {
>       fprintf(stderr, "Out of memory.\en");
>       exit(1);
>  }

Reply via email to