|
Message-ID: <20221105133025.36fa0d05@inria.fr>
Date: Sat, 5 Nov 2022 13:30:25 +0100
From: Jₑₙₛ Gustedt <jens.gustedt@...ia.fr>
To: "Alex Xu (Hello71)" <alex_y_xu@...oo.ca>
Cc: musl@...ts.openwall.com, Szabolcs Nagy <nsz@...t70.net>, Markus Wichmann
<nullplan@....net>
Subject: Re: Invalid read of nl_arg in printf_core()
Alex,
on Sat, 05 Nov 2022 07:52:09 -0400 you ("Alex Xu (Hello71)"
<alex_y_xu@...oo.ca>) wrote:
> Excerpts from Szabolcs Nagy's message of November 5, 2022 6:09 am:
> > * Markus Wichmann <nullplan@....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 ::
Content of type "application/pgp-signature" skipped
Powered by blists - more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.