Dan Cross <cross@spitfire.i.gajendra.net> wrote:
In article <1100g0e$1lt8i$1@kst.eternal-september.org>,
Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
and in fact
it *won't* occur during execution because foo() isn't called.
A compiler can't generate code with arbitrary behavior just
because it can't prove that there will be no UB. If it could,
every signed or floating-point arithmetic operation with unknown
operand values would grant the same permission.
But that's not the situation here. The situation is that the
compiler can prove that something _is_ UB.
In the program quoted at the top of this post, the UB occurs in
a function foo() that's never called. A compiler can replace the
body of foo() with a trap, and it can certainly warn about the UB,
but I don't believe it can reject the entire program. A clever
compiler could prove that the UB never occurs.
So there are two things that are at play here.
First, this notion that UB is _only_ a runtime matter. The text
of the standard contradicting that aside, if a translator can
detect that the behavior of a construct is provably undefined if
executed, then it seems axiomatic that UB is clearly something
that plays a role at translation time, as well.
I think that this paragraph (and several other it this post and
other posts) represent fundamental misanderstanding. This may
be due to the way C standard is written. AFAIK Extended Pascal
standard (once you translate terminalogy) states the same things as
C about UB, but in clearer way. Some relevant parts below:
: 3.1 Dynamic-violation
: A violation by a program of the requirements of this International
: Standard that a processor is permitted to leave undetected up to,
: but not beyond, execution of the declaration, definition, or
: statement that exhibits (see clause 6) the dynamic-violation.
: 3.2 Error
: A violation by a program of the requirements of this International
: Standard that a processor is permitted to leave undetected.
...
: 5.1 Processors
...
: e) be able to determine whether or not the program violates any
: requirements of this International Standard, where such a
violation is : not designated an error or dynamic-violation,
...
: 5.2 Programs
...
: b) if it conforms at level 1, use only those features of the
language : specified in clause 6;
UB in C standard corresponds with 'error' in Pascal standard. [...]
I think that lawyerish style of current C standard is mostly
inertia,
and making standard more mathematical would improve it.
But giving formal semantic in the standard would mean
significantly bigger change.
On 15/06/2026 00:55, Keith Thompson wrote:(I don't expect the complete investigation report on the Ariane 5
[...][...]
Throwing some kind of exception or trap can definitely be helpful at
times.ÿ And I agree that it would make it obvious that there has been a problem detected.ÿ But throwing exceptions or traps can cause more
problems (the Ariane 5 failure was caused by the exception handler, not
the overflow fault).ÿ That does not mean it is better to ignore
overflows - it means there is no appropriate action that is suitable in every situation.ÿ I am far from convinced that there is even a
reasonable choice of default action that could be usefully made.
[...]
On 15/06/2026 19:57, Waldek Hebisch wrote:
[...]
No, ignoring problems is never a good thing.ÿ Writing code that doesn't
run the risk of problems is a good thing.
And I can agree that sometimes leaving traps enabled in released code
can be helpful - there are situations where you can't practically remove
the risk of overflows, and it is better to crash out reliably than risk running on with faulty data.ÿ It is, however, also the case that
sometimes traps will cause far more problems than incorrect data would. (Noting that UB does not guarantee "incorrect data" - it can do
anything.ÿ Wrapping semantics, or unspecified value semantics, would do that.)
[...]
On 2026-06-16 10:10, David Brown wrote:
On 15/06/2026 19:57, Waldek Hebisch wrote:
[...]
No, ignoring problems is never a good thing.ÿ Writing code that
doesn't run the risk of problems is a good thing.
Sure.
And I can agree that sometimes leaving traps enabled in released code
can be helpful - there are situations where you can't practically
remove the risk of overflows, and it is better to crash out reliably
than risk running on with faulty data.ÿ It is, however, also the case
that sometimes traps will cause far more problems than incorrect data
would. (Noting that UB does not guarantee "incorrect data" - it can do
anything.ÿ Wrapping semantics, or unspecified value semantics, would
do that.)
Hmm.. - not sure what you mean (and imply with) "crash out reliably".
Having been engaged in server systems software development a crash
had never been an accepted option. And that's certainly also true
with life-critical applications and costly operations (upthread you
had mentioned Ariane 5). You should always avoid crashes and catch exceptions.
and that depends on the actual application case; report it, retry it,
retry with alternative methods or adapted conditions, emulate the
result, estimate it, ask supervisor process, switch devices, etc.
I'm well aware that wrong data may also be bad, be it from a wrong algorithms, a technical overflow situation, unreliable data sources,
or an unreliable processing (not-excluding effects of UB).
I'm really not sure whether to consider "not handling an exception"
better or worse than "not handling data errors"; usually you don't
want either. So both should prevented (if possible) or acted upon
(if getting a notice about it).
Janis
[...]
"Undefined Behavior", in C, in the manner usually discussed in
this newsgroup, was introduced with the first standard.
Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:[...]
antispam@fricas.org (Waldek Hebisch) writes:
UB in C standard corresponds with 'error' in Pascal standard. [...]
Does it? In C a syntax error is undefined behavior, but it
requires a diagnostic. (I don't mean to single out just syntax
errors; there are other examples.)
I mean typical UB, especialy cases that people complain about.
[...]
cross@spitfire.i.gajendra.net (Dan Cross) writes:
[...]
"Undefined Behavior", in C, in the manner usually discussed in
this newsgroup, was introduced with the first standard.
The term but not the concept, which was there since the
early days of C -- at least since K&R in 1978, and very
likely earlier (I haven't reviewed any of the earlier
descriptions of the language).
| Sysop: | Jacob Catayoc |
|---|---|
| Location: | Pasay City, Metro Manila, Philippines |
| Users: | 4 |
| Nodes: | 4 (0 / 4) |
| Uptime: | 496008:18:10 |
| Calls: | 178 |
| Files: | 605 |
| D/L today: |
7 files (18,398K bytes) |
| Messages: | 70,124 |