Re: Constants and undefined behavior

Liste des GroupesRevenir à cl c  
Sujet : Re: Constants and undefined behavior
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.c
Date : 08. Jun 2026, 20:39:04
Autres entêtes
Organisation : None to speak of
Message-ID : <11075os$3fm4u$1@kst.eternal-september.org>
References : 1 2 3 4 5
User-Agent : Gnus/5.13 (Gnus v5.13)
cross@spitfire.i.gajendra.net (Dan Cross) writes:
In article <1100g0e$1lt8i$1@kst.eternal-september.org>,
Keith Thompson  <Keith.S.Thompson+u@gmail.com> wrote:
[...]
A naive compiler that performs no optimizations would generate
code for foo() that attempts to compute (INT_MAX+1)*0 step by
step, without recognizing the overflow, and that code would never
be executed.
>
Sure.  But a far more sophisticated translator (and I would
argue a nefarious one) could emulate that code, decide it was
UB, and immediately fail translation with an error.

I disagree.  That's not a sensible interpretation of what the
standard says.

A call to a foo() would have undefined behavior if it occurred.  There
is no call to foo().

Similarly:

    int a = ..., b = ...;
    int c;
    if (b != 0) {
        c = a / b;
    }
    else {
        c = 0;
    }

A division by zero would have undefined behavior if it occurred,
but it never occurs.  A compiler cannot reject the above code
because of UB that never happens.

[...]

It returns a status of 0 from main and does nothing else.
A conforming implementation *must* generate code that implements
that behavior.
>
I have yet to find or be shown a way in which the standard
actually guarantees that.

How does the standard guarantee *anything*?

This strictly conforming program:

    int main(void) { return 0; }

when executed returns a status of 0 from main and does nothing else.
Adding an uncalled function to the same source file doesn't change
that.

[...]

There was, once, a view that was almost universally shared that
UB was meant for things that could not be precisely described
because hardware was too varied.  We're well past that; now it's
a vehicle for compiler writers to make benchmarks faster, but is
(generally) hostile to programmers.  A lot of hay is made about
it in this group, but at the core, it's just (ironically) not
well-defined.

The standard does say what UB is meant for.  It says what UB
*is*, and what constructs lead to it (by omission in some cases).
Any optimization tricks played by compiler implementers must be
based on that specification.

[...]

I agree.  printf("hello, world\n") must write that string to standard
output, which may be a file or an interactive device.  Just what
that means is unspecified or implementation-defined.  It might be
printed in EBCDIC or incised into clay tablets.  Closing stdout,
which occurs when main() terminates, might involve firing the tablet
or emitting control sequences for a screen reader.
>
Exactly.  It could also emit the string, "GOODBYE WORLD."

No, it couldn't.  It must emit "hello, world\n" in some form.
It must emit the character 'h' as represented in the execution
character set, followed by 'e', and so on.

[...]

This presupposes that the program is strictly conforming, but
in the limit, the standard can be interpreted in such a way that
if any statement in the program is proveably UB (as this one is)
then the program cannot said to be strictly conforming.

It's not UB if it's never called.  Behavior that doesn't happen is
not behavior.

I did not presuppose that the program is strictly conforming.
I read the source code and determined that it meets the standard's
definition of a strictly conforming program.

[...]

Ok, so in that case, would we say that "`foo` has undefined
behavior?"  The qualification, "...if called" seems superfluous,
and I don't see anything in the standard that explicitly
disagrees.

The qualification "if called" is the whole point.

[...]

UB can time-travel, however.  Because it's undefined, the
compiler is free to assume that it never executes, or that it
always executes.

"UB can time-travel" is perhaps an oversimplification.  An example is
a bug that occurred in the Linux kernel, something like:

    void func(int *ptr) {
        do_something_with(*ptr);
        if (ptr != NULL) {
            blah();
        }
    }

The compiler, on seeing the expression `*ptr`, assumed that `ptr` is
not null, and elided the test on the following line.

But even assuming that's valid, a compiler absolutely cannot assume that
an instance UB always executes when, according to the semantics of the
program, it provably never executes.

[...]

So any program that produces no output at all is strictly
conforming?  Then what about this?
>
#include <limits.h>
>
int
zero(void)
{
return (INT_MAX + 1) * 0;
}
>
int
main(void)
{
(void)zero();
return 0;
}

That's an interesting point.  A more terse example:

#include <limits.h>
int main(void) {
    int unused = INT_MAX + 1;
}

This program produces no output, yet clearly executes a function
that contains an expression that induces undefined behavior when
evaluated.  I suppose an argument could be made that it _might_
generate output due to UB, as UB imposes no requirements Not to
do so, so perhaps the _absence_ of output depends on UB.

The program clearly has undefined behavior when executed, but no
output depends on that undefined behavior.  In my humble opinion,
this demonstrates a flaw in the standard's definition of "strictly
conforming program".  (As a programmer: Don't do that.)

[...]

In my ideal world, C would be rigorously defined with a precise
operational semantics.  That would be accompanied by an
explanatory document that presented those semantics in lay
terms in prose, similar to the standard now, for those who did
not want to drive Coq or something similar.  But at least we'd
have something definitive to define the language, so that when
there was apparent ambiguity, we had some objective metric by
which to judge.  The C standard, as written, is nowhere close as
precise as it should be.
 
I do not think that this will ever happen: not only would it be
very difficult to produce (as you noted elsethread), I think the
compiler writers would rebel if they felt that their UB hands
were tied by a formal specification.

"There are only two kinds of languages: the ones people complain
about and the ones nobody uses."
-- Bjarne Stroustrup


--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */

Date Sujet#  Auteur
27 May 26 * this girl calls c ugly363fir
27 May 26 `* Re: this girl calls c ugly362fir
28 May 26  `* Re: this girl calls c ugly361BGB
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 ugly336fir
28 May 26    `* Re: this girl calls c ugly335BGB
29 May 26     +* Re: this girl calls c ugly328Lawrence D’Oliveiro
29 May 26     i`* Re: this girl calls c ugly327Janis Papanagnou
29 May 26     i `* Re: this girl calls c ugly326Bart
29 May 26     i  +* Re: this girl calls c ugly310Janis Papanagnou
29 May 26     i  i`* Re: this girl calls c ugly309Bart
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 ugly299tTh
29 May 26     i  i  `* Re: this girl calls c ugly298Bart
29 May 26     i  i   +* Re: this girl calls c ugly296Keith Thompson
29 May 26     i  i   i`* Re: this girl calls c ugly295Bart
29 May 26     i  i   i +- Re: this girl calls c ugly1Janis Papanagnou
29 May 26     i  i   i `* Re: this girl calls c ugly293Keith Thompson
29 May 26     i  i   i  `* Re: this girl calls c ugly292Bart
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 ugly286Dan Cross
30 May 26     i  i   i    +* Re: this girl calls c ugly282Bart
31 May 26     i  i   i    i+* Re: this girl calls c ugly280Keith 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 ugly274Richard Harnden
31 May 26     i  i   i    ii +* Re: this girl calls c ugly169David Brown
31 May 26     i  i   i    ii i+* Re: this girl calls c ugly166Bart
31 May 26     i  i   i    ii ii+* Re: this girl calls c ugly140David Brown
31 May 26     i  i   i    ii iii`* Re: this girl calls c ugly139James Kuyper
31 May 26     i  i   i    ii iii `* Re: this girl calls c ugly138David 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 ugly133Keith 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 ugly129Janis 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 behavior82Tim Rentsch
2 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 behavior80Tim Rentsch
4 Jun 26     i  i   i    ii iii   ii  `* Re: Constants and undefined behavior79Dan 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 Jun10:46     i  i   i    ii iii   ii   iiii  i  `* Re: Constants and undefined behavior2Dan Cross
9 Jun23:07     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 Jun23:29     i  i   i    ii iii   ii   iiii   +* Re: Constants and undefined behavior3James Kuyper
10 Jun00:01     i  i   i    ii iii   ii   iiii   i`* Re: Constants and undefined behavior2Keith Thompson
10 Jun13:36     i  i   i    ii iii   ii   iiii   i `- Re: Constants and undefined behavior1Dan Cross
11 Jun15:49     i  i   i    ii iii   ii   iiii   `* Re: Constants and undefined behavior7Janis Papanagnou
11 Jun16:20     i  i   i    ii iii   ii   iiii    +* Re: Constants and undefined behavior2Dan Cross
11 Jun17:08     i  i   i    ii iii   ii   iiii    i`- Re: Constants and undefined behavior1Janis Papanagnou
11 Jun17:30     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 Jun07:05     i  i   i    ii iii   ii   `* Re: Constants and undefined behavior47Tim 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