mailing list of musl libc
 help / color / mirror / code / Atom feed
From: "Jₑₙₛ Gustedt" <jens.gustedt@inria.fr>
To: "Alex Xu (Hello71)" <alex_y_xu@yahoo.ca>
Cc: musl@lists.openwall.com, Szabolcs Nagy <nsz@port70.net>,
	Markus Wichmann <nullplan@gmx.net>
Subject: Re: [musl] Invalid read of nl_arg in printf_core()
Date: Sat, 5 Nov 2022 13:30:25 +0100	[thread overview]
Message-ID: <20221105133025.36fa0d05@inria.fr> (raw)
In-Reply-To: <1667648794.ymyp54wny2.none@localhost>

[-- Attachment #1: Type: text/plain, Size: 2369 bytes --]

Alex,

on Sat, 05 Nov 2022 07:52:09 -0400 you ("Alex Xu (Hello71)"
<alex_y_xu@yahoo.ca>) wrote:

> Excerpts from Szabolcs Nagy's message of November 5, 2022 6:09 am:
> > * Markus Wichmann <nullplan@gmx.net> [2022-11-03 20:42:16 +0100]:  
> >> Hi all,
> >> 
> >> reading some code today, I noticed undefined behavior in
> >> printf_core(). vfprintf() creates an array called nl_arg
> >> automatically and does not initialize it. That is fine, but it
> >> means that reads from each array member are undefined behavior
> >> until that member gets assigned a value.  
> > 
> > an automatic storage object that was not initialized has
> > indeterminate value, so accessing it is not undefined behaviour.
> > (unless the object could have been declared with the register
> > storage class)
> > 
> > so technically the code is correct (if int has no trap
> > representations) but i think the fix still makes sense: leaving
> > unspecified values around is error prone.  
> 
> N1570 J.2 [Undefined behavior]:
> 
> 1   The behavior is undefined in the following circumstances:
> [ ... ]
> — The value of an object with automatic storage duration is used
> while it is indeterminate (6.2.4, 6.7.9, 6.8).

N1570 is old and annex J is only informative. The version for C23
reads

        The value of an object with automatic storage duration is used while
        the object has an indeterminate representation (6.2.4, 6.7.10, 6.8).

which is already a bit more subtle. Observe that it says "use" and
does not say that it is undefined to do the lvalue (object
representation -> value) conversion.

There have been multiple interpretations and endless discussions over
the years of this kind of question and no unique conclusion about its
"official" status w.r.t to the standard has been acknowledged. But the
inofficial status of this is that it is "bad" and reading
uninitialized values should be avoided.

This holds in particular where the possible gain by optimization
(speed or code size) is marginal, so the gain versus risk balance is
not very favorable.

Thanks
Jₑₙₛ

-- 
:: INRIA Nancy Grand Est ::: Camus ::::::: ICube/ICPS :::
:: :::::::::::::::::::::: gsm France : +33 651400183   ::
:: ::::::::::::::: gsm international : +49 15737185122 ::
:: http://icube-icps.unistra.fr/index.php/Jens_Gustedt ::

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

      reply	other threads:[~2022-11-05 12:30 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-03 19:42 Markus Wichmann
2022-11-05 10:09 ` Szabolcs Nagy
2022-11-05 11:52   ` Alex Xu (Hello71)
2022-11-05 12:30     ` Jₑₙₛ Gustedt [this message]

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=20221105133025.36fa0d05@inria.fr \
    --to=jens.gustedt@inria.fr \
    --cc=alex_y_xu@yahoo.ca \
    --cc=musl@lists.openwall.com \
    --cc=nsz@port70.net \
    --cc=nullplan@gmx.net \
    /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).