bug in btowc(0)

Eric Blake eblake@redhat.com
Tue Apr 6 14:54:00 GMT 2010


On 04/06/2010 02:04 AM, Corinna Vinschen wrote:
> On Apr  5 16:49, Eric Blake wrote:
>> On the gnulib list, it was pointed out that cygwin btowc has a bug:
>>
>> +#include <stdio.h>
>> +#include <string.h>
>> +#include <wchar.h>
>> +int main ()
>> +{
>> +  if (btowc ('\0') != 0)
>> +    return 1;
>> +  return 0;
>> +}
>>
>> should have status 0, because NUL is an ASCII byte.  Right now, btowc is
>> mistakenly returning WEOF and this program has status 1.
> 
> That's a newlib problem.  Please send the bug report to the newlib
> list, for the records.  This patch should help:
> 
> Index: libc/stdlib/btowc.c
> ===================================================================
> RCS file: /cvs/src/src/newlib/libc/stdlib/btowc.c,v
> retrieving revision 1.3
> diff -u -p -r1.3 btowc.c
> --- libc/stdlib/btowc.c	18 Nov 2009 09:49:57 -0000	1.3
> +++ libc/stdlib/btowc.c	6 Apr 2010 08:02:37 -0000
> @@ -13,6 +13,9 @@ btowc (int c)
>    wchar_t pwc;
>    unsigned char b;
>  
> +  if (c == EOF)
> +    return WEOF;
> +
>    b = (unsigned char)c;
>  
>    /* Put mbs in initial state. */
> @@ -22,8 +25,8 @@ btowc (int c)
>  
>    retval = __mbtowc (_REENT, &pwc, &b, 1, __locale_charset (), &mbs);
>  
> -  if (c == EOF || retval != 1)
> +  if (retval != 0 && retval != 1)
>      return WEOF;
> -  else
> -    return (wint_t)pwc;
> +
> +  return (wint_t)pwc;
>  }
> 
> 
> Corinna
> 

For what it's worth, Corinna's patch looks good to me.

-- 
Eric Blake   eblake@redhat.com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 323 bytes
Desc: OpenPGP digital signature
URL: <http://sourceware.org/pipermail/newlib/attachments/20100406/0f3765dc/attachment.sig>


More information about the Newlib mailing list