printf: %ls or %S does not work when string is of length 1.

Jeff Johnston jjohnstn@redhat.com
Wed Aug 27 22:02:00 GMT 2008


Corinna Vinschen wrote:
>
>
> I tracked this down to a problem in the function _wcsrtombs_r.
>
> The condition to write a converted character into the destination string
> and to continue the loop is (wcsrtombs.c, line 48):
>
>   if (n <= len - bytes && bytes < len)
>
> The second half of this condition is wrong, AFAICS.  First of all, it
> should be bytes <= len instead of bytes < len.  Second, this part of the
> condition is already catched by the first part of the condition:
>
>   n <= len - bytes  <==>  n + bytes <= len
>
> with n always >= 0, bytes is always <= len if n + bytes is <= len.  So
> the condition is redundant.
>
>   
Thanks for looking at this Corinna.

Actually, the second part is needed because the first equation is 
unsigned.  Let's say len is 1 but bytes is 2.  len - bytes is 
(unsigned)-1.  We don't want to write at this point.  Thus, we make the 
additional check that bytes is actually <= len.  I just modified your 
change and checked it in.
>
> Thanks,
> Corinna
>
>   



More information about the Newlib mailing list