From: Marco Atzeri <marco.atzeri@gmail.com>
To: cygwin@cygwin.com
Subject: Re: strxfrm() returns an incorrect value on a short buffer
Date: Tue, 12 Apr 2016 08:35:00 -0000 [thread overview]
Message-ID: <570CB34D.60200@gmail.com> (raw)
In-Reply-To: <20160412050722.GE12445@mars.tony.develop-help.com>
On 12/04/2016 07:07, Tony Cook wrote:
> 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
>
You should check the error output.
Both cygwin and Linux seem IMHO to report an insufficient buffer
and s1 is not guaranteed.
http://pubs.opengroup.org/onlinepubs/9699919799/functions/strxfrm.html
Upon successful completion, strxfrm() [CX] [Option Start] and
strxfrm_l() [Option End] shall return the length of the transformed
string (not including the terminating NUL character). If the value
returned is n or more, the contents of the array pointed to by s1 are
unspecified.
On error, strxfrm() [CX] [Option Start] and strxfrm_l() [Option
End] may set errno but no return value is reserved to indicate an error.
--
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
next prev parent reply other threads:[~2016-04-12 8:35 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-04-12 5:07 Tony Cook
2016-04-12 8:35 ` Marco Atzeri [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=570CB34D.60200@gmail.com \
--to=marco.atzeri@gmail.com \
--cc=cygwin@cygwin.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).