delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2008/08/27/10:37:27

X-Recipient: archive-cygwin AT delorie DOT com
X-Spam-Check-By: sourceware.org
Date: Wed, 27 Aug 2008 16:36:34 +0200
From: Corinna Vinschen <corinna-cygwin AT cygwin DOT com>
To: cygwin AT cygwin DOT com
Subject: Re: printf: %ls or %S does not work when string is of length 1.
Message-ID: <20080827143634.GA15253@calimero.vinschen.de>
Reply-To: cygwin AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
References: <e27efe130808270654p515fce76y8356edbfec8b7830 AT mail DOT gmail DOT com>
MIME-Version: 1.0
In-Reply-To: <e27efe130808270654p515fce76y8356edbfec8b7830@mail.gmail.com>
User-Agent: Mutt/1.5.16 (2007-06-09)
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Unsubscribe: <mailto:cygwin-unsubscribe-archive-cygwin=delorie DOT com AT cygwin DOT com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

Hi,

the following bug report has been send to the Cygwin list a couple of
minutes ago.

On Aug 27 15:54, Amaury Forgeot d'Arc wrote:
> Hello,
> it seems that cygwin does not correctly handle the %ls format when the
> given string
> has only one character.
> 
> The following program should print
>     Test 1 (T)
> and that's what it does on Linux 64bit and Windows, when compiled with VS8.0
> 
> But cygwin's output is
>     Test 1
> 
> I've seen this only for 1-wchar strings.
> This is will be a problem with the upcoming python 3.0 interpreter, as
> reported here:
> http://bugs.python.org/issue3626
> 
> Is there a problem with cygwin's implementation, or did I miss
> something obvious?
> 
> 
> /* ======================================== */
> #include <stdio.h>
> int main()
> {
>   wchar_t text[] = L"T";
>   printf("Test %d (%ls)\n", wcslen(text), text);
> }
> /* ======================================== */

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.

I propose the following simple fix:

	* libc/stdlib/wcsrtombs.c (_wcsrtombs_r): Fix condition for
	accepting a converted character and continuing the loop.

Index: wcsrtombs.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdlib/wcsrtombs.c,v
retrieving revision 1.6
diff -u -p -r1.6 wcsrtombs.c
--- wcsrtombs.c	23 Apr 2004 21:44:22 -0000	1.6
+++ wcsrtombs.c	27 Aug 2008 14:33:34 -0000
@@ -45,7 +45,7 @@ _DEFUN (_wcsrtombs_r, (r, dst, src, len,
 	  ps->__count = 0;
 	  return (size_t)-1;
 	}
-      if (n <= len - bytes && bytes < len)
+      if (n <= len - bytes)
 	{
           n += bytes;
 	  if (dst)

Ok to apply?


Thanks,
Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Project Co-Leader          cygwin AT cygwin DOT com
Red Hat

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019