Re: Constants and undefined behavior

Liste des GroupesRevenir à cl c  
Sujet : Re: Constants and undefined behavior
De : cross (at) *nospam* spitfire.i.gajendra.net (Dan Cross)
Groupes : comp.lang.c
Date : 05. Jun 2026, 00:49:43
Autres entêtes
Organisation : PANIX Public Access Internet and UNIX, NYC
Message-ID : <10vt2un$fuu$1@reader1.panix.com>
References : 1 2 3 4
User-Agent : trn 4.0-test77 (Sep 1, 2010)
In article <10vsnl7$lkmu$1@kst.eternal-september.org>,
Keith Thompson  <Keith.S.Thompson+u@gmail.com> wrote:
cross@spitfire.i.gajendra.net (Dan Cross) writes:
In article <865x3yd21n.fsf@linuxsc.com>,
Tim Rentsch  <tr.17687@z991.linuxsc.com> wrote:
cross@spitfire.i.gajendra.net (Dan Cross) writes:
In article <86ik81cfk5.fsf_-_@linuxsc.com>,
Tim Rentsch  <tr.17687@z991.linuxsc.com> wrote:
[...]
There's an important distinction to make here.  Consider this
program:
>
   #include <limits.h>
>
   int
   foo(){
       int zero = (INT_MAX+1)*0;
       return  zero;
   }
>
   int
   main(){
       return  0;
   }
>
This program does not transgress the bounds of undefined behavior.
>
To clarify, the comments in my posting were meant to be read as
saying the given text is the entire program, and that it is strictly
conforming with respect to conforming hosted implementations.
(Incidentally, given the rules for freestanding implementations, I'm
not sure that it is even possible for any program to be strictly
conforming with respect to conforming freestanding implementations.
In any case my statements were meant only in the context of hosted
implementations.)
>
Ok.
>
[snip]
Perhaps you mean that this is irrelevant because `foo` is not
invoked, but I see no reason why that need be the case in e.g.
a freestanding environment.
>
I explained the context of my previous statements above.  Sorry for
not saying that in the original message.
>
In a hosted environment, I don't
think anything explicitly prevents `foo` from being called after
`main` returns (though I can't imagine that would happen in real
life;  it would be weird if it did).
>
The semantics described in the ISO C standard don't admit that
possibility.
>
Could you please point to where it says this, in the C standard?
>
I cannot find anything that says that arbitrary code cannot run
after `main()` returns, and I don't see how that could possibly
be true.
>
N3220 5.1.2.4, Program semantics.
>
It defines the *observable behavior* of a program, which consists of
accesses to volatile objects, data written to files, and I/O dynamics of
interactive devices.

Yes, but it does so for strictly-conforming programs with no UB.

To understand conformance, we have to jump over to section 4,
which explicitly says that, 'Undefined behavior is otherwise
indicated in this document by the words "undefined behavior" or
by the omission of any explicit definition of behavior.'  As it
does not say that a program with an instance of undefined
behavior in an integer constant expression that is not executed
must otherwise behave in any given manner, what the program does
is undefined.  A constaint violation mandates a diagnostic, but
beyond that, the standard is (AFAICT) silent.

Undefined Behavior, in turn, is not defined as specific only to
execution: the standard simply says that it is "behavior, upon
use of a *nonportable or erroneous program construct*..." for
which there are no requirements, and there are examples of
things that are explicitly UB at translation time, such as
improperly terminated lexemes and so forth.

Furthermore, the expression above is obviously an integer
constant expression as defined by sec 6.6 para 8.  Section 6.6,
para 4, reads in part, "Each constant expression shall evaluate
to a constant that is in the range of representable values for
its type."  The expression, `(INT_MAX+1)*0` violates this
constraint, and so therefore a diagnostic is mandated as per
sec 5.1.1.3 para 1.  That it appears in code that is not
obviously called from `main` doesn't change that.

Morever, sec 6.6 para 17 says that, "the semantic rules for
evaluation of a constant expression are the same as for
nonconstant expressions."  This brings us back to 5.1.2.4,
though I submit that para (4) is a stronger argument for what
you and Tim are saying, as it reads in part, "An actual
implementation is not required to evaluate part of an expression
if it can deduce that its value is not used and that no needed
side effects are produced (including any caused by calling a
function or through volatile access to an object)."  I interpret
this to mean that, if the implementation can determine that
there is no way that `foo` can be called, it does not _have_ to
evaluate the above expression.  However, it must satisfy the
range constraint from section 6.6, so it likely will, and in any
event, the standard does not say that it, "shall not" evaluate
it, or when.

Once the compiler does that, if it does, and observes UB, the
standard is silent on what requirements it imposes, which means
the behavior is undefined.  I see no reason it couldn't arrange
to invoke `foo` at that point.

So no, I do not see how execution according to the rules of the
abstract machine is not guaranteed, here.  I certainly see no
way in which this can be regarded as a strictly conforming
program.

If the usual "Hello, world" program prints "Hello, world" followed
by "Goodbye", the implementation is non-conforming.  If it formats
my hard drive after printing "Goodbye", it's non-conforming and
dangerous.

Two separate things.  My point earlier was that code can
obviously run after `main` terminates.  Moreoever, I can't
imagine what would _prevent_ a runtime system that invokes
`main` from doing something like printing, "PROGRAM STOPPED"
after `main` returned.  C imposes no requirements here.

Whether `foo` could be invoked after, I think, is undefined.

Whether foo() has external linkage or internal
linkage doesn't change that.
>
I disagree.  There's no possible way for the implementation to
know whether a function with external linkage will be ultimately
invoked or not; consider a system that supports loadable shared
modules.  Nothing prevents even this simple program from being
compiled as a shared module, dynamically loaded, the loading
program explicitly searching for and finding the symbol
corresponding to the `foo` function, and invoking it.
>
Remember that linking is translation phase 8.  The compiler is not
the entire implementation.

Exactly my point.  The compiler cannot know how `foo` might be
used, or how the translated object might be exercised.  There's
I don't see how it could possibly know that, given that `foo`
has external linkage.

Hence, the compiler _must_ treat with UB as written, which is
why `ubsan` inserts trapping code in `foo`.
>
I don't know what "_must_ treat with UB" means.
>
foo() has undefined behavior if it's called, so replacing its
body with trapping code is valid.  But (I'm reasonably sure that)
an implementation cannot reject a program just because it can't
prove that it has no undefined behavior during execution.  It can
reject it if it can prove that it *always* has undefined behavior
during execution.

What I'm saying is that, `foo` has undefined behavior _period_.
That's manifest in an integer constant expression, whether it is
executed at runtime or not.  I believe that the standard forces
the expression to be evaluated at translation time, via the
"shall" mandate when checking the constraint on the range in sec
6.6 para 4.  Further, that evaluation must happen in accordance
with the rules of the abstract machine, as per 5.1.2.4 para 17.
The diagnostic is mandated, as is the translation-time
evaluation.  The expression is itself manifestly exhibits UB,
and so therefore the result of the rest of the translation is
undefined.

I could be wrong; this is all excessively pedantic.  And of
course, if an implementation does something silly and emits
garbage for Tim's program, then I argue it should be chucked
onto the dustbin of excessive fawning over the standard.  But
I'm not convinced that the standard _prohibits_ such an extreme
interpretation.

In your example, `foo` clearly exhibits UB; I think your
argument is whether that has a realized effect or not, since the
UB is not invoked.  I'm saying that in general a compiler cannot
possibly know that when it compiles `foo`, and is free to assume
the worst.
>
foo() exhibits UB if and only if it's called during execution.
>
Yes, a compiler can't know whether foo() will be called.
An implementation, particularly a linker, might know, but is not
required to.  No, it is not free to assume the worst.

See above.

I certainly wouldn't want a compiler to reject `1/time(NULL)`
because it can't prove that time(NULL) won't be zero, or reject
`argc+1` because it can't prove that argc < INT_MAX.  Code whose
behavior would be undefined if it were executed has no behavior
(and therefore no UB) if it's not executed.

That's categorically different; what you are describing are what
Regehr calls, "Type-2" functions, and I agree with you for
those.

The program that Tim posted has a "Type-3" function, and
constraints dictate that the UB express must be evaluated at
translation time, and a diagnostic emitted.  In the most
charitable interpretation, it cannot be considered a strictly
conforming program, even if the implementation is smart enough
to avoid evaluating the constant expression, as it is
unspecified whether it's evaluated or not, and strictly
conforming programs shall not rely on unspecified behavior.

- Dan C.


Date Sujet#  Auteur
27 May 26 * this girl calls c ugly365fir
27 May 26 `* Re: this girl calls c ugly364fir
28 May 26  `* Re: this girl calls c ugly363BGB
28 May 26   +* Re: this girl calls c ugly5Lawrence D’Oliveiro
28 May 26   i+* Re: this girl calls c ugly3BGB
29 May 26   ii`* Re: this girl calls c ugly2Lawrence D’Oliveiro
29 May 26   ii `- Re: this girl calls c ugly1BGB
28 May 26   i`- Re: this girl calls c ugly1Bonita Montero
28 May 26   +* Re: this girl calls c ugly19Janis Papanagnou
28 May 26   i+* Re: this girl calls c ugly15BGB
29 May 26   ii+- Re: this girl calls c ugly1Lawrence D’Oliveiro
29 May 26   ii`* Re: this girl calls c ugly13Janis Papanagnou
29 May 26   ii `* Re: this girl calls c ugly12BGB
29 May 26   ii  +* Re: this girl calls c ugly9David Brown
29 May 26   ii  i`* Re: this girl calls c ugly8BGB
30 May 26   ii  i `* Re: this girl calls c ugly7David Brown
30 May 26   ii  i  +* Re: this girl calls c ugly2Janis Papanagnou
30 May 26   ii  i  i`- Re: this girl calls c ugly1David Brown
30 May 26   ii  i  `* Re: this girl calls c ugly4BGB
31 May 26   ii  i   `* Re: this girl calls c ugly3David Brown
31 May 26   ii  i    `* Re: this girl calls c ugly2BGB
31 May 26   ii  i     `- Re: this girl calls c ugly1David Brown
29 May 26   ii  +- Re: this girl calls c ugly1Janis Papanagnou
30 May 26   ii  `- Re: this girl calls c ugly1Lawrence D’Oliveiro
28 May 26   i`* Re: this girl calls c ugly3Chris M. Thomasson
29 May 26   i `* Re: this girl calls c ugly2Janis Papanagnou
29 May 26   i  `- Re: this girl calls c ugly1Chris M. Thomasson
28 May 26   `* Re: this girl calls c ugly338fir
28 May 26    `* Re: this girl calls c ugly337BGB
29 May 26     +* Re: this girl calls c ugly330Lawrence D’Oliveiro
29 May 26     i`* Re: this girl calls c ugly329Janis Papanagnou
29 May 26     i `* Re: this girl calls c ugly328Bart
29 May 26     i  +* Re: this girl calls c ugly312Janis Papanagnou
29 May 26     i  i`* Re: this girl calls c ugly311Bart
29 May 26     i  i +* Re: this girl calls c ugly9Janis Papanagnou
29 May 26     i  i i+* Re: this girl calls c ugly2Bart
29 May 26     i  i ii`- Re: this girl calls c ugly1Janis Papanagnou
29 May 26     i  i i`* Re: this girl calls c ugly6Bart
29 May 26     i  i i +* Re: this girl calls c ugly4Janis Papanagnou
29 May 26     i  i i i`* Re: this girl calls c ugly3Bart
29 May 26     i  i i i `* Re: this girl calls c ugly2Janis Papanagnou
29 May 26     i  i i i  `- Re: this girl calls c ugly1Bart
29 May 26     i  i i `- Re: this girl calls c ugly1Keith Thompson
29 May 26     i  i `* Re: this girl calls c ugly301tTh
29 May 26     i  i  `* Re: this girl calls c ugly300Bart
29 May 26     i  i   +* Re: this girl calls c ugly298Keith Thompson
29 May 26     i  i   i`* Re: this girl calls c ugly297Bart
29 May 26     i  i   i +- Re: this girl calls c ugly1Janis Papanagnou
29 May 26     i  i   i `* Re: this girl calls c ugly295Keith Thompson
29 May 26     i  i   i  `* Re: this girl calls c ugly294Bart
29 May 26     i  i   i   +* Re: this girl calls c ugly5Keith Thompson
30 May 26     i  i   i   i`* Re: this girl calls c ugly4James Kuyper
30 May 26     i  i   i   i `* Re: this girl calls c ugly3Bart
30 May 26     i  i   i   i  `* Re: this girl calls c ugly2Keith Thompson
30 May 26     i  i   i   i   `- Re: this girl calls c ugly1Bart
30 May 26     i  i   i   `* Re: this girl calls c ugly288Dan Cross
30 May 26     i  i   i    +* Re: this girl calls c ugly284Bart
31 May 26     i  i   i    i+* Re: this girl calls c ugly282Keith Thompson
31 May 26     i  i   i    ii+* Re: this girl calls c ugly5Janis Papanagnou
31 May 26     i  i   i    iii+* Re: this girl calls c ugly2Keith Thompson
2 Jun 26     i  i   i    iiii`- Re: this girl calls c ugly1Janis Papanagnou
31 May 26     i  i   i    iii`* Re: this girl calls c ugly2David Brown
2 Jun 26     i  i   i    iii `- Re: this girl calls c ugly1Janis Papanagnou
31 May 26     i  i   i    ii`* Re: this girl calls c ugly276Richard Harnden
31 May 26     i  i   i    ii +* Re: this girl calls c ugly171David Brown
31 May 26     i  i   i    ii i+* Re: this girl calls c ugly168Bart
31 May 26     i  i   i    ii ii+* Re: this girl calls c ugly142David Brown
31 May 26     i  i   i    ii iii`* Re: this girl calls c ugly141James Kuyper
31 May 26     i  i   i    ii iii `* Re: this girl calls c ugly140David Brown
31 May 26     i  i   i    ii iii  +* Re: this girl calls c ugly4James Kuyper
31 May 26     i  i   i    ii iii  i`* Re: this girl calls c ugly3David Brown
31 May 26     i  i   i    ii iii  i `* Re: this girl calls c ugly2James Kuyper
1 Jun 26     i  i   i    ii iii  i  `- Re: this girl calls c ugly1David Brown
31 May 26     i  i   i    ii iii  `* Re: this girl calls c ugly135Keith Thompson
1 Jun 26     i  i   i    ii iii   +* Re: this girl calls c ugly2David Brown
1 Jun 26     i  i   i    ii iii   i`- Re: this girl calls c ugly1Keith Thompson
2 Jun 26     i  i   i    ii iii   +* Re: this girl calls c ugly131Janis Papanagnou
2 Jun 26     i  i   i    ii iii   i+- Re: this girl calls c ugly1James Kuyper
2 Jun 26     i  i   i    ii iii   i+* Constants and undefined behavior84Tim Rentsch
2 Jun 26     i  i   i    ii iii   ii`* Re: Constants and undefined behavior83Dan Cross
4 Jun 26     i  i   i    ii iii   ii `* Re: Constants and undefined behavior82Tim Rentsch
4 Jun 26     i  i   i    ii iii   ii  `* Re: Constants and undefined behavior81Dan Cross
4 Jun 26     i  i   i    ii iii   ii   +* Re: Constants and undefined behavior31Keith Thompson
5 Jun 26     i  i   i    ii iii   ii   i+* Re: Constants and undefined behavior28Dan Cross
5 Jun 26     i  i   i    ii iii   ii   ii+* Re: Constants and undefined behavior24Keith Thompson
6 Jun 26     i  i   i    ii iii   ii   iii+* Re: Constants and undefined behavior19Dan Cross
6 Jun 26     i  i   i    ii iii   ii   iiii`* Re: Constants and undefined behavior18Keith Thompson
8 Jun 26     i  i   i    ii iii   ii   iiii `* Re: Constants and undefined behavior17Dan Cross
8 Jun 26     i  i   i    ii iii   ii   iiii  +* Re: Constants and undefined behavior5Keith Thompson
9 Jun 26     i  i   i    ii iii   ii   iiii  i`* Re: Constants and undefined behavior4Dan Cross
9 Jun 26     i  i   i    ii iii   ii   iiii  i `* Re: Constants and undefined behavior3Keith Thompson
9 Jun 26     i  i   i    ii iii   ii   iiii  i  `* Re: Constants and undefined behavior2Dan Cross
9 Jun 26     i  i   i    ii iii   ii   iiii  i   `- Re: Constants and undefined behavior1Keith Thompson
9 Jun 26     i  i   i    ii iii   ii   iiii  `* Re: Constants and undefined behavior11Waldek Hebisch
9 Jun 26     i  i   i    ii iii   ii   iiii   +* Re: Constants and undefined behavior3James Kuyper
10 Jun 26     i  i   i    ii iii   ii   iiii   i`* Re: Constants and undefined behavior2Keith Thompson
10 Jun 26     i  i   i    ii iii   ii   iiii   i `- Re: Constants and undefined behavior1Dan Cross
11 Jun 26     i  i   i    ii iii   ii   iiii   `* Re: Constants and undefined behavior7Janis Papanagnou
11 Jun 26     i  i   i    ii iii   ii   iiii    +* Re: Constants and undefined behavior2Dan Cross
11 Jun 26     i  i   i    ii iii   ii   iiii    i`- Re: Constants and undefined behavior1Janis Papanagnou
11 Jun 26     i  i   i    ii iii   ii   iiii    `* Re: Constants and undefined behavior4Waldek Hebisch
6 Jun 26     i  i   i    ii iii   ii   iii`* Re: Constants and undefined behavior4Tim Rentsch
5 Jun 26     i  i   i    ii iii   ii   ii`* Re: Constants and undefined behavior3Janis Papanagnou
7 Jun 26     i  i   i    ii iii   ii   i`* Re: Constants and undefined behavior2Tim Rentsch
9 Jun 26     i  i   i    ii iii   ii   `* Re: Constants and undefined behavior49Tim Rentsch
2 Jun 26     i  i   i    ii iii   i`* Re: this girl calls c ugly45Keith Thompson
2 Jun 26     i  i   i    ii iii   `- Re: this girl calls c ugly1Chris M. Thomasson
2 Jun 26     i  i   i    ii ii`* Re: this girl calls c ugly25Dan Cross
31 May 26     i  i   i    ii i`* Re: this girl calls c ugly2James Kuyper
31 May 26     i  i   i    ii +* Re: this girl calls c ugly2Keith Thompson
31 May 26     i  i   i    ii `* Re: this girl calls c ugly102Tim Rentsch
31 May 26     i  i   i    i`- Re: this girl calls c ugly1Dan Cross
1 Jun 26     i  i   i    `* Re: this girl calls c ugly3Tim Rentsch
30 May 26     i  i   `- Re: this girl calls c ugly1David Brown
29 May 26     i  +* Re: this girl calls c ugly6Janis Papanagnou
30 May 26     i  `* Re: this girl calls c ugly9Lawrence D’Oliveiro
29 May 26     `* Re: this girl calls c ugly6Bonita Montero

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal