mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Pascal Cuoq <cuoq@trust-in-soft.com>
To: "musl@lists.openwall.com" <musl@lists.openwall.com>
Subject: *scanf, wrong types in va_arg, and strict aliasing
Date: Mon, 10 Apr 2017 09:31:48 +0000	[thread overview]
Message-ID: <467D90DF-11E7-4E58-91A2-C5BFC71EE7F3@trust-in-soft.com> (raw)

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.


Besides, since I am reviewing this file, and since I was originally interested in analyzing it for strict aliasing violations (although the analyzer is not ready to handle this file), I should point out the function store_int at line 22 and the way it is used at line 146:

https://git.musl-libc.org/cgit/musl/tree/src/stdio/vfscanf.c?id=54807d47acecab778498ced88ce8f62bfa16e379#n22

https://git.musl-libc.org/cgit/musl/tree/src/stdio/vfscanf.c?id=54807d47acecab778498ced88ce8f62bfa16e379#n146

This will not go down well with the strict aliasing analyzer, when it is finally ready for this sort of code. And indeed, compiling the previous scanf snippet together with musl's source code while enabling link-time optimization could plausibly produce non-working binary code because of the type-based alias analysis.

For this latter problem, use of types with the may_alias attribute will fix it for GCC and Clang. For the problem with va_arg, I do not see any easy way out. It might be possible to write a longer version that a modern compiler miraculously recognizes as doing the same thing over and over and folds back into a single sequence of instructions, but without having tried it yet, this seems a bit far-fetched.


Pascal



             reply	other threads:[~2017-04-10  9:31 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-04-10  9:31 Pascal Cuoq [this message]
2017-04-10  9:51 ` Pascal Cuoq
2017-04-10 13:43   ` Rich Felker
2017-04-10 13:42 ` Rich Felker

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=467D90DF-11E7-4E58-91A2-C5BFC71EE7F3@trust-in-soft.com \
    --to=cuoq@trust-in-soft.com \
    --cc=musl@lists.openwall.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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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 NNTP newsgroup(s).