From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/11231 Path: news.gmane.org!.POSTED!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: *scanf, wrong types in va_arg, and strict aliasing Date: Mon, 10 Apr 2017 09:42:36 -0400 Message-ID: <20170410134236.GT17319@brightrain.aerifal.cx> References: <467D90DF-11E7-4E58-91A2-C5BFC71EE7F3@trust-in-soft.com> 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 1491831771 10276 195.159.176.226 (10 Apr 2017 13:42:51 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Mon, 10 Apr 2017 13:42:51 +0000 (UTC) User-Agent: Mutt/1.5.21 (2010-09-15) To: musl@lists.openwall.com Original-X-From: musl-return-11246-gllmg-musl=m.gmane.org@lists.openwall.com Mon Apr 10 15:42:46 2017 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 1cxZae-0002VM-BU for gllmg-musl@m.gmane.org; Mon, 10 Apr 2017 15:42:44 +0200 Original-Received: (qmail 3565 invoked by uid 550); 10 Apr 2017 13:42:48 -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 3546 invoked from network); 10 Apr 2017 13:42:48 -0000 Content-Disposition: inline In-Reply-To: <467D90DF-11E7-4E58-91A2-C5BFC71EE7F3@trust-in-soft.com> Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:11231 Archived-At: On Mon, Apr 10, 2017 at 09:31:48AM +0000, Pascal Cuoq wrote: > Hello again, > > > the scanf implementation does the same thing as the printf implementation, in vfscanf.c, line 110: > > https://git.musl-libc.org/cgit/musl/tree/src/stdio/vfscanf.c?id=54807d47acecab778498ced88ce8f62bfa16e379#n110 > > This line is eventually reached, for instance, for the snippet: > > int n; sscanf("0", "%d", &n); > > And the argument being consumed has type int*. This is not a case that 7.16..1.1:2 allows. Indeed, that's wrong, but POSIX makes it hard or even impossible to do right because of the %n$ localization forms. Whereas printf's requirement for these is: "When numbered argument specifications are used, specifying the Nth argument requires that all the leading arguments, from the first to the (N-1)th, are specified in the format string." scanf's requirement is a bit different: "When numbered argument specifications are used, specifying the Nth argument requires that all the leading arguments, from the first to the (N-1)th, are pointers." This means that use of %2$ without a %1$ is permitted as long as a pointer (what kind?!) is passed in the first variadic position after the format string. I think the best we could do is to _try_ to do it right, by tracking the types like we do for printf, and only fallback to generic void* when a conversion specifier is missing. But that's a lot of extra code needed for something with no user-visible improvements. Alternatively, we could write a compiler-barrier in trivial asm to wrap an external va_arg, which is ugly but would also be useful in places like fcntl/ioctl that might read variadic args with unknown type to pass to the kernel. Any opinions on what we should do? Rich