From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/10795 Path: news.gmane.org!.POSTED!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: Handling of L and ll prefixes different from glibc Date: Wed, 14 Dec 2016 11:13:48 -0500 Message-ID: <20161214161348.GU1555@brightrain.aerifal.cx> References: Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: blaine.gmane.org 1481732046 21579 195.159.176.226 (14 Dec 2016 16:14:06 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Wed, 14 Dec 2016 16:14:06 +0000 (UTC) User-Agent: Mutt/1.5.21 (2010-09-15) To: musl@lists.openwall.com Original-X-From: musl-return-10808-gllmg-musl=m.gmane.org@lists.openwall.com Wed Dec 14 17:14:02 2016 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.84_2) (envelope-from ) id 1cHCBs-0004jx-1F for gllmg-musl@m.gmane.org; Wed, 14 Dec 2016 17:14:00 +0100 Original-Received: (qmail 26267 invoked by uid 550); 14 Dec 2016 16:14:01 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 26242 invoked from network); 14 Dec 2016 16:14:01 -0000 Content-Disposition: inline In-Reply-To: Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:10795 Archived-At: On Wed, Dec 14, 2016 at 03:46:40PM +0200, Nadav Har'El wrote: > Hi, > > Posix's printf manual suggests (see > http://pubs.opengroup.org/onlinepubs/9699919799/functions/fprintf.html) > that the "ll" format prefix should only be used for integer types, and "L" > should only be used for long double type. And it seems that indeed, this is > what Musl's printf() supports - the test program > > long double d = 123.456; > printf("Lf: %Lf\n", d); > printf("llf %llf\n", d); > long long int i = 123456; > printf("Ld: %Ld\n", i); > printf("lld: %lld\n", i); > > produces with Musl's printf just two lines of output: > > Lf: 123.456000 > lld: 123456 > > The two other printf()s (with %Ld and %llf) are silently dropped. Not quite silently; printf is returning -1 with errno set to EINVAL. > However, in glibc, it seems that "ll" and "L" are synonyms, and both work > for both integer and floating types. The above program produces with glibc > four lines of output: > > Lf: 123.456000 > llf 123.456000 > Ld: 123456 > lld: 123456 > > If Musl's intention is to be compatible with glibc, not Posix, I guess this > behavior should be fixed, and LL and ll should become synonyms, not > different flags? There is no general "intention to be compatible with glibc". There are a couple related topics you might be thinking of: Widely-used and widely-available extensions: There are written-up guidelines for the criteria for inclusion or exclusion of such interfaces, balancing things like usefulness, cost, and whether there's already a better way to do the same thing portably. ABI compatibility: There is an intent to support use of some glibc-linked code in binary form with musl. From a practical standpoint, this is mainly for libraries without source that some users depend on (like flash and eventually nvidia stuff, maybe). Aside from practical needs like that, the scope of the compatibility goal is purely to support fully POSIX-conforming programs, or programs which use common extension APIs provided by musl, not programs relying on unsupported glibc functionality, doing things gratuitously wrong (like ll vs L here), or depending on glibc bugs. Now back to the topic of printf: As for printf formats specifically, musl avoids defining any of the cases which are undefined behavior in order to avoid getting in a situation where we conflict with future versions of the standard. This happened with glibc's scanf, which took 'a' as an extension flag for auto-allocation, only to have C99 later assign it for floating point (to match printf hex formatting), and glibc had to use hackery of remapping symbols in different conformance profiles to work around the problem. musl does not do that kind of hackery, so we have to be careful not to introduce such problems in the first place. Note that there is one printf extension we have, %m, but this is because POSIX requires %m to be supported by syslog(), making it unlikely that the standards would assign a conflicting meaning in the future. Also %m is very useful, whereas mismatched L/ll is just programmer sloppiness. One thing I'm not happy with now is the way printf returns an error (which the caller usually ignores) on invalid format strings; this hides lots of bugs (for example, a similar issue with some legacy software using %qd for printing long long) and doesn't have any basis in requirements, since invalid format strings invoke undefined behavior. I'm mildly leaning towards causing a crash on invalid format strings so that the location of the incorrect usage can be quickly found with a debugger, but I'd like feedback from users who've debugged this sort of thing on whether that'd actually be helpful. Rich