public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* strxfrm() returns an incorrect value on a short buffer
@ 2016-04-12  5:07 Tony Cook
  2016-04-12  8:35 ` Marco Atzeri
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Tony Cook @ 2016-04-12  5:07 UTC (permalink / raw)
  To: cygwin

strxfrm() returns an incorrect value if you supply an output buffer
and that buffer is too short for the result.

With the code following:

#include <string.h>
#include <locale.h>
#include <stdio.h>

int main() {
  char xbuf[5] = "";
  char *lc = setlocale(LC_ALL, "en_AU.utf8");
  if (!lc) {
    perror("setlocale");
    return 1;
  }
  size_t sza = strxfrm(xbuf, "alphabet", sizeof(xbuf));
  printf("sz: %zd\n", sza);
  size_t szb = strxfrm(NULL, "alphabet", 0);
  printf("sz: %zd\n", szb);

  return 0;
}

On cygwin:

tony@phobos ~
$ gcc -ostrxfrmtest strxfrmtest.c

tony@phobos ~
$ ./strxfrmtest
sz: 5
sz: 20

tony@phobos ~
$ uname -a
CYGWIN_NT-6.1-WOW phobos 2.5.0(0.297/5/3) 2016-04-11 09:55 i686 Cygwin

On Linux:

tony@mars:~$ gcc -ostrxfrmtest strxfrmtest.c 
tony@mars:~$ ./strxfrmtest
sz: 26
sz: 26
tony@mars:~$ uname -a
Linux mars 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt20-1+deb8u3 (2016-01-17) x86_64 GNU/Linux

From looking at the source:

https://cygwin.com/git/gitweb.cgi?p=newlib-cygwin.git;a=blob;f=winsup/cygwin/nlsfuncs.cc;h=9dbd9b16d53094c60aa835756c967c054ced8e62;hb=HEAD#l1286

It appears that strxfrm() is just returning the size of the output
buffer on an overflow error rather than calling LCMapString() again
with cchDest set to zero to get the required buffer length that
strxfrm() is meant to return on a short buffer.

This came out of the discussion in:

https://rt.perl.org/Ticket/Display.html?id=121734

(not that I've reproduced that issue.)

Tony

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2016-04-13  8:10 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-04-12  5:07 strxfrm() returns an incorrect value on a short buffer Tony Cook
2016-04-12  8:35 ` Marco Atzeri
2016-04-12 10:20 ` Achim Gratz
2016-04-12 10:57   ` Tony Cook
2016-04-12 13:31 ` Corinna Vinschen
2016-04-13  0:46   ` Tony Cook
2016-04-13  8:10     ` Corinna Vinschen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).