Re: Constants and undefined behavior

Liste des GroupesRevenir à cl c  
Sujet : Re: Constants and undefined behavior
De : tr.17687 (at) *nospam* z991.linuxsc.com (Tim Rentsch)
Groupes : comp.lang.c
Date : 04. Jun 2026, 11:37:24
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <865x3yd21n.fsf@linuxsc.com>
References : 1 2 3 4 5
User-Agent : Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
cross@spitfire.i.gajendra.net (Dan Cross) writes:

In article <86ik81cfk5.fsf_-_@linuxsc.com>,
Tim Rentsch  <tr.17687@z991.linuxsc.com> wrote:
>
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
>
On 2026-06-01 00:54, Keith Thompson wrote:
>
[...]
>
Yes, a compiler can reduce (a + b) * 0 to just 0.  But it's not
required to do so, and (INT_MAX + 1) * 0 still has undefined
behavior.  Undefined behavior is determined by the rules of the
abstract machine *without* any adjustments permitted by the as-if
rule.
>
This is something I really don't get in the actual C-logic...
>
Using constants that can be determined at compile time is UB here,
despite the '* 0' mathematically indicating an IMO clear semantics,
but using variables is only UB possibly at runtime?  [...]
>
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.)

Given that `foo` has external linkage, I find this hard to
believe, and `clang -fsanitize=undefined` agrees with me,
both emitting a diagnostic about the overflow and generating
code in `foo` to call into the sanitizer machinery.

A conforming implementation is free to emit a diagnostic whenever it
chooses, for any reason at all, regardless of whether the program
source is legal C or not.  (I feel obliged to point out that, if a
preprocessing #error directive is encountered, then there may be an
exception to that statement;  however, there is no such #error in
the program shown above.)

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.  Whether foo() has external linkage or internal
linkage doesn't change that.  Only those actions initiated by
statements in main() are ever elaborated.

But I'm not sure what _you_ mean by "transgress the bounds of
undefined behavior" here.

It's a grammatical fine point.  I think for present purposes it's
okay to gloss over the distinction, and say this statement may be
read as saying "the program does not have undefined behavior".

Even more than that, the program is strictly conforming, and must be
accepted by a conforming implementation.
>
See above.
>
Now let's change the program slightly:
>
   #include <limits.h>
>
   int
   foo(){
       static int zero = (INT_MAX+1)*0;
       return  zero;
   }
>
   int
   main(){
       return  0;
   }
>
This program does transgress the bounds of undefined behavior.  The
reason for the difference is that in the first program the semantics
of foo() is to evaluate the expression to be stored in 'zero' only
at runtime, whereas in the second program the semantics of foo() is
to evaluate the expression to be stored in 'zero' before program
startup (informally, "at compile time").  What matters is not
whether the offending expression /might/ be evaluated "at compile
time", but whether the offending expression /must/ be evaluated "at
compile time".  Only in the second case is undefined behavior
inevitable (and thus it does not occur in the first program).
>
Fine point:  strictly speaking, I believe the C standard allows even
the second program to complete translation phase 8 successfully, and
for any offending behavior to occur only when we actually try to run
the program.  To say that another way, there is no requirement that
possible nasal demons be made manifest at any point before an actual
attempted execution.  On the other hand, because that possibility is
there lurking in the background, there is no requirement that the
program be accepted, and could be rejected by a conforming compiler.
>
Indeed.  Further, I believe that the same is true for the first
program, as well.

It isn't.  In the first program the offending expression is never
evaluated, because foo() is never called.

Date Sujet#  Auteur
27 May 26 * this girl calls c ugly371fir
27 May 26 `* Re: this girl calls c ugly370fir
28 May 26  `* Re: this girl calls c ugly369BGB
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 ugly344fir
28 May 26    `* Re: this girl calls c ugly343BGB
29 May 26     +* Re: this girl calls c ugly336Lawrence D’Oliveiro
29 May 26     i`* Re: this girl calls c ugly335Janis Papanagnou
29 May 26     i `* Re: this girl calls c ugly334Bart
29 May 26     i  +* Re: this girl calls c ugly318Janis Papanagnou
29 May 26     i  i`* Re: this girl calls c ugly317Bart
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 ugly307tTh
29 May 26     i  i  `* Re: this girl calls c ugly306Bart
29 May 26     i  i   +* Re: this girl calls c ugly304Keith Thompson
29 May 26     i  i   i`* Re: this girl calls c ugly303Bart
29 May 26     i  i   i +- Re: this girl calls c ugly1Janis Papanagnou
29 May 26     i  i   i `* Re: this girl calls c ugly301Keith Thompson
29 May 26     i  i   i  `* Re: this girl calls c ugly300Bart
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 ugly294Dan Cross
30 May 26     i  i   i    +* Re: this girl calls c ugly290Bart
31 May 26     i  i   i    i+* Re: this girl calls c ugly288Keith 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 ugly282Richard Harnden
31 May 26     i  i   i    ii +* Re: this girl calls c ugly175David Brown
31 May 26     i  i   i    ii i+* Re: this girl calls c ugly172Bart
31 May 26     i  i   i    ii ii+* Re: this girl calls c ugly146David Brown
31 May 26     i  i   i    ii iii`* Re: this girl calls c ugly145James Kuyper
31 May 26     i  i   i    ii iii `* Re: this girl calls c ugly144David 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 ugly139Keith 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 ugly135Janis 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 behavior88Tim Rentsch
2 Jun 26     i  i   i    ii iii   ii`* Re: Constants and undefined behavior87Dan Cross
4 Jun 26     i  i   i    ii iii   ii `* Re: Constants and undefined behavior86Tim Rentsch
4 Jun 26     i  i   i    ii iii   ii  `* Re: Constants and undefined behavior85Dan Cross
4 Jun 26     i  i   i    ii iii   ii   +* Re: Constants and undefined behavior35Keith Thompson
5 Jun 26     i  i   i    ii iii   ii   i+* Re: Constants and undefined behavior32Dan Cross
5 Jun 26     i  i   i    ii iii   ii   ii+* Re: Constants and undefined behavior28Keith Thompson
6 Jun 26     i  i   i    ii iii   ii   iii+* Re: Constants and undefined behavior23Dan Cross
6 Jun 26     i  i   i    ii iii   ii   iiii`* Re: Constants and undefined behavior22Keith Thompson
8 Jun 26     i  i   i    ii iii   ii   iiii `* Re: Constants and undefined behavior21Dan 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 behavior15Waldek 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 behavior9Janis Papanagnou
11 Jun 26     i  i   i    ii iii   ii   iiii   i+* Re: Constants and undefined behavior2Dan Cross
11 Jun 26     i  i   i    ii iii   ii   iiii   ii`- Re: Constants and undefined behavior1Janis Papanagnou
11 Jun 26     i  i   i    ii iii   ii   iiii   i`* Re: Constants and undefined behavior6Waldek Hebisch
21 Jun22:26     i  i   i    ii iii   ii   iiii   `* Re: Constants and undefined behavior2Tim Rentsch
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 ugly104Tim 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