X-Recipient: archive-cygwin AT delorie DOT com DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:subject:to:references:from:message-id:date :mime-version:in-reply-to:content-type :content-transfer-encoding; q=dns; s=default; b=NBUa+WjRF0zpliKl NN+pP3Q23ftmeDKyqawfceQbkSvcdZCw2aJ/COgAlsSdIX9n0/ZzwPUuCc2Bz+jC eVPv4wDvc8G22jWLyR/NlYTgHnybUg94K6Gv3JeD1pyB7sGqT+f7ZFXY10k9/KlE TNBHYso5dVNs1LJk92Pj42FrVZg= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:subject:to:references:from:message-id:date :mime-version:in-reply-to:content-type :content-transfer-encoding; s=default; bh=+M+2VVoon1ixhjC6iC93je ysoIM=; b=QigUXnk5T6pn2wK8qlx/BxgyRPmvS9qXWbwLrq6s3WTBfsQio3lPjn C1Efkbqv0FeWOn+gHVtcoLdy9tzxTNH/RCwJs+DMFSLmg9IW9Yn1vJCq0UB0VHqK qrTKEmpOAHHGbbdwIR3xj5QS0hW6Mr41PYizm/c3Xo2d8xPAiaA0M= Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.2 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,SPF_PASS autolearn=ham version=3.3.2 spammy=Upon, Tony, cook, Cook X-HELO: mail-wm0-f49.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=6iczzzA6LRb5G/MtLQhJ7+KExWouA9sFyxOGTO5xCBs=; b=THEdsSaukDGJVHte+VSEe7/l2k8dR3pJF7kN4OZJ2NeLNGybnkL349GRw6QWedfKwn RRP3pBmdh3XTV/m0NCGQeLlnxovVgj4RvgUnS67OTwX7LM58gcefb4EptBOtlkzt6pv5 pk2TRNL/a1ZewN3R2ftC3GpxtqVfFQfC/n32XQ/TwKehbujb3W+/8jnyknMTnaZVULOT fHEyyN9XOMySXTJGoGjWuqB0wpgRuM7omnsyfjqZKL7dq5T1Fuz1GBAK6qmseKc9Z5bj 51S5AsirCj1eRX6+kv235FmnYVJQLIHon1GxLgd4GZdm6nM/zCnjxdJrBUdTgSl8kBEt OtWg== X-Gm-Message-State: AD7BkJITAlUI7fkr3FiUQPZal7W8JKnBMNfxAHhCRPM1y5U3UdI39QQcVx2QFUIiqZBqNQ== X-Received: by 10.28.175.201 with SMTP id y192mr23940779wme.54.1460450126801; Tue, 12 Apr 2016 01:35:26 -0700 (PDT) Subject: Re: strxfrm() returns an incorrect value on a short buffer To: cygwin AT cygwin DOT com References: <20160412050722 DOT GE12445 AT mars DOT tony DOT develop-help DOT com> From: Marco Atzeri Message-ID: <570CB34D.60200@gmail.com> Date: Tue, 12 Apr 2016 10:35:25 +0200 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.7.2 MIME-Version: 1.0 In-Reply-To: <20160412050722.GE12445@mars.tony.develop-help.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-IsSubscribed: yes 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 > #include > #include > > 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 AT phobos ~ > $ gcc -ostrxfrmtest strxfrmtest.c > > tony AT phobos ~ > $ ./strxfrmtest > sz: 5 > sz: 20 > > tony AT 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 AT mars:~$ gcc -ostrxfrmtest strxfrmtest.c > tony AT mars:~$ ./strxfrmtest > sz: 26 > sz: 26 > tony AT 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