Path: csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c++
Subject: Re: Has "stack overflow" specified behavior?
Date: Mon, 13 Dec 2021 13:13:42 -0800
Organization: A noiseless patient Spider
Lines: 44
Message-ID: <86o85k5htl.fsf@linuxsc.com>
References:
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="bcf38487a73cf7e14a790d07f34a4966"; logging-data="7973"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ZSLCE5exll2saxPedRpjktWRMtFpLaT4="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:Ns3jeHAfXt+iqOQui/CiJlINziI= sha1:x6UhqJUvMDkXikneCt3J7FjLQzU=
Xref: csiph.com comp.lang.c++:82612
wij writes:
> void t() {
> int a;
> ++a;
> t();
> };
>
> int main()
> {
> t();
> }
>
> ---
> Has "stack overflow" specified behavior?
The expression '++a' tries to read an uninitialized variable.
After correcting for that oversight (for example, by giving a
value to 'a' at its declaration by 'int a = 0;'), the program has
defined behavior. To be more specific, each of the operations
asked for in the program has a well-defined description of what
is to happen in the abstract machine, which means the program as
a whole has defined behavior.
Note that this conclusion is about what will take place in the
/abstract/ machine, and not about what occurs if and when the
program is run in an /actual/ machine. The C++ standard
explicitly lets executing a program in an actual machine off the
hook for running out of any kind of limited resource, including
but not limited to "stack space". Section 4.1 paragraph 2.1 of
n4860 says this:
If a program contains no violations of the rules in this
document, a conforming implementation shall, within its
resource limits, accept and correctly execute that program.
So even though the program has defined behavior, it may very
well fail due to running out of stack space when executed.
Moreover that applies to all programs, for any kind of
resource the implementation might depend on.
Short summary: the program (not counting the uninitialized
access) has defined behavior, but may fail because of stack
overflow during an actual execution.