Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.std.c > #6535

Re: Does reading an uninitialized object have undefined behavior?

From Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups comp.std.c
Subject Re: Does reading an uninitialized object have undefined behavior?
Date 2023-08-16 09:19 -0700
Organization A noiseless patient Spider
Message-ID <86v8df55a9.fsf@linuxsc.com> (permalink)
References <87zg3pq1ym.fsf@nosuchdomain.example.com> <864jlfj34p.fsf@linuxsc.com> <871qgjlqe9.fsf@nosuchdomain.example.com>

Show all headers | View raw


Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:

> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>
>> Repeating the question stated in the Subject line:
>>
>> Does reading an uninitialized object [always] have undefined
>> behavior?
>>
>> Background:  Annex J part 2 says (in various phrasings in
>> different revisions of the C standard, with the one below
>> being taken from C90):
>>
>>     The value of an uninitialized object that has automatic
>>     storage duration is used before a value is assigned [is
>>     undefined behavior] (6.5.7)
>>
>> Remembering that Annex J is informative rather than normative,
>> is this statement right even for a type that has no trap
>> representations?  To ask that question another way, is this
>> statement always right or is it just a (perhaps useful)
>> approximation?
>
> [400+ lines deleted]
>
>> Summary:  my reading is that accessing an object that has not
>> been explicitly stored into since its declaration was evaluated
>> is necessarily undefined behavior in C90, but not necessarily
>> undefined behavior in C99 and C11 (and AFAIAA also in C17 and
>> the upcoming C23).  My reasoning is given in detail above.
>>
>>
>> Postscript:  this commentary has taken much longer to write than
>> I thought it would, for the most part because I made an early
>> decision to be systematic and thorough.  I hope the effort has
>> helped the readers gain confidence in the explanations and
>> conclusions stated.  I may return to the deferred topic about
>> pointer types but have no plans at present about when that might
>> be.
>
> Thank you for taking the time to write that.

It's nice to be appreciated.  Thank you.

> I'd like to offer a brief summary of the points you made.  Please let me
> know if my summary is incorrect.

Excellent.  I am writing a reaction directly after each item.

> - An "indeterminate value" is by definition either an "unspecified
>   value" or a "trap representation".

Yes.

> - In C90 (which did not yet define all these terms), accessing the value
>   of an uninitialized object explicitly has undefined behavior.

C90 made "use [...] of indeterminately valued objects" part of the
definition of undefined behavior.  To connect the dots we need to
know that "If an object that has automatic storage duration is not
initialized explicitly, its value is indeterminate."  These two
normative items are combined into one in J.2:  "The value of an
uninitialized object that has automatic storage duration is used
before a value is assigned".

> - In C99 and later, J.2 (which is *not* normative) states that using the
>   value of an object with automatic storage duration while it is
>   indeterminate has undefined behavior.  This implies that:
>     int main(void) {
>         int n;
>         n;
>     }
>   has undefined behavior, even if int has no trap representations.

For the J.2 summary, yes.  I don't think I gave the implied
conclusion, but I agree with you that the J.2 entry does seem to
imply this.

> - Statements in J.2 *should* be supported by normative text.

I don't think I said this at all.  At least for now I offer
no opinion on this recommendation.

> - There is no normative text in any post-C90 edition of the C
>   standard that supports the claim that reading an uninitialized
>   int object actually has undefined behavior if it does not hold
>   a trap representation.  (Pointers raise other issues, which I'll
>   ignore for now.)

Yes, with a very minor correction that it is C99 and later, because
I haven't looked at the editions of the C standard after C90 but
before C99.

> - The cited statement in J.2 is incorrect, or at least imprecise.

I don't think I said this exactly.  I did say or at least imply
that the quoted entry in J.2 is not completely accurate.  Certainly
it allows conclusions that are not supported by normative text, and
looked at from that point of view it is "wrong".

> I agree with you on all the above points.
>
> There is one point on which I think we disagree.  It is a matter
> of opinion, not of fact.  You wrote:
>
>     Remembering that Annex J is informative rather than normative,
>     is this statement right even for a type that has no trap
>     representations?  To ask that question another way, is this
>     statement always right or is it just a (perhaps useful)
>     approximation?
>
> The statement in N1570 J.2 is:
>
>     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).
>
> I get the impression that you're not particularly bothered by the fact
> that the statement in J.2 is merely an "approximation".  In my opinion,
> the statement in J.2 is simply incorrect, and should be fixed.  (That's
> unlikely to be possible at this stage of the C23 process.)  The fact
> that Annex J is, to quote the standard's foreword, "for information
> only", is not an excuse to ignore factual errors.  Readers of the
> standard rely on the informative annexes to provide correct information.
> This particular text is not just a "(perhaps useful) approximation";  it
> is actively misleading.

Like I said before, for now I offer no opinion on this question.  I
wouldn't mind if a footnote were added to help mitigate the problem.

> I'm not criticizing the author of the standard for making this mistake.
> Stuff happens.  It was likely a result of an oversight during the
> transition from C90 to C99.

After reading the various standards carefully, I believe the wording
in the J.2 entry was not just an oversight.  I suspect there is
something deeper going on.  In neither case, however, does it prompt
any specific reaction (ie, in myself) as to what to do about it (if
anything).

Back to comp.std.c | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Does reading an uninitialized object have undefined behavior? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-07-20 22:16 -0700
  Re: Does reading an uninitialized object have undefined behavior? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-07-21 16:33 +0100
    Re: Does reading an uninitialized object have undefined behavior? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-07-21 11:56 -0700
      Re: Does reading an uninitialized object have undefined behavior? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-07-21 20:54 +0100
        Re: Does reading an uninitialized object have undefined behavior? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-07-21 14:26 -0700
          Re: Does reading an uninitialized object have undefined behavior? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-07-21 23:39 +0100
          Re: Does reading an uninitialized object have undefined behavior? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-08-12 17:00 -0700
            Re: Does reading an uninitialized object have undefined behavior? Martin Uecker <ma.uecker@gmail.com> - 2023-08-13 23:41 -0700
              Re: Does reading an uninitialized object have undefined behavior? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-08-15 21:06 -0700
                Re: Does reading an uninitialized object have undefined behavior? Martin Uecker <ma.uecker@gmail.com> - 2023-08-15 22:40 -0700
                Re: Does reading an uninitialized object have undefined behavior? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-08-16 23:13 -0700
                Re: Does reading an uninitialized object have undefined behavior? Kaz Kylheku <864-117-4973@kylheku.com> - 2023-08-17 07:08 +0000
                Re: Does reading an uninitialized object have undefined behavior? Martin Uecker <ma.uecker@gmail.com> - 2023-08-18 12:44 -0700
                Re: Does reading an uninitialized object have undefined behavior? Kaz Kylheku <864-117-4973@kylheku.com> - 2023-08-19 05:04 +0000
                Re: Does reading an uninitialized object have undefined behavior? Martin Uecker <ma.uecker@gmail.com> - 2023-08-19 01:36 -0700
                Re: Does reading an uninitialized object have undefined behavior? Richard Damon <Richard@Damon-Family.org> - 2023-08-19 09:18 -0400
                Re: Does reading an uninitialized object have undefined behavior? Martin Uecker <ma.uecker@gmail.com> - 2023-08-19 11:12 -0700
                Re: Does reading an uninitialized object have undefined behavior? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-08-18 20:20 -0700
                Re: Does reading an uninitialized object have undefined behavior? Kaz Kylheku <864-117-4973@kylheku.com> - 2023-08-19 05:23 +0000
                Re: Does reading an uninitialized object have undefined behavior? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-08-18 22:56 -0700
                Re: Does reading an uninitialized object have undefined behavior? Martin Uecker <ma.uecker@gmail.com> - 2023-08-18 12:52 -0700
                Re: Does reading an uninitialized object have undefined behavior? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-08-26 19:25 -0700
                Re: Does reading an uninitialized object have undefined behavior? Spiros Bousbouras <spibou@gmail.com> - 2023-08-27 08:31 +0000
                Re: Does reading an uninitialized object have undefined behavior? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-08-29 04:35 -0700
                Re: Does reading an uninitialized object have undefined behavior? Spiros Bousbouras <spibou@gmail.com> - 2023-08-30 19:53 +0000
                Re: Does reading an uninitialized object have undefined behavior? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-08-30 17:40 -0700
                Re: Does reading an uninitialized object have undefined behavior? Spiros Bousbouras <spibou@gmail.com> - 2023-08-31 18:18 +0000
                Re: Does reading an uninitialized object have undefined behavior? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-09-05 05:39 -0700
                Re: Does reading an uninitialized object have undefined behavior? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-09-05 17:03 -0700
                Re: Does reading an uninitialized object have undefined behavior? Jakob Bohm <jb-usenet@wisemo.com.invalid> - 2023-09-07 17:09 +0200
                Re: Does reading an uninitialized object have undefined behavior? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-09-07 17:19 +0100
                Re: Does reading an uninitialized object have undefined behavior? Jakob Bohm <jb-usenet@wisemo.com.invalid> - 2023-09-08 23:12 +0200
                Re: Does reading an uninitialized object have undefined behavior? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-09-08 22:31 +0100
    Re: Does reading an uninitialized object have undefined behavior? Kaz Kylheku <864-117-4973@kylheku.com> - 2023-07-22 06:40 +0000
      Re: Does reading an uninitialized object have undefined behavior? Martin Uecker <ma.uecker@gmail.com> - 2023-07-22 06:03 -0700
        Re: Does reading an uninitialized object have undefined behavior? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-07-25 21:53 -0700
      Re: Does reading an uninitialized object have undefined behavior? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-08-16 11:11 -0700
  Re: Does reading an uninitialized object have undefined behavior? Kaz Kylheku <864-117-4973@kylheku.com> - 2023-07-21 17:42 +0000
    Re: Does reading an uninitialized object have undefined behavior? Jakob Bohm <jb-usenet@wisemo.com.invalid> - 2023-07-24 07:53 +0200
      Re: Does reading an uninitialized object have undefined behavior? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-07-25 21:57 -0700
  Re: Does reading an uninitialized object have undefined behavior? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-08-03 13:13 -0700
    Re: Does reading an uninitialized object have undefined behavior? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-08-03 15:20 -0700
      Re: Does reading an uninitialized object have undefined behavior? Martin Uecker <ma.uecker@gmail.com> - 2023-08-05 01:15 -0700
      Re: Does reading an uninitialized object have undefined behavior? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-08-16 09:19 -0700
      Re: Does reading an uninitialized object have undefined behavior? Kaz Kylheku <864-117-4973@kylheku.com> - 2023-08-16 19:51 +0000
      Re: Does reading an uninitialized object have undefined behavior? Kaz Kylheku <864-117-4973@kylheku.com> - 2023-08-16 20:03 +0000
        Re: Does reading an uninitialized object have undefined behavior? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-08-16 13:43 -0700
          Re: Does reading an uninitialized object have undefined behavior? Kaz Kylheku <864-117-4973@kylheku.com> - 2023-08-16 21:08 +0000

csiph-web