Re: Constants and undefined behavior

Liste des GroupesRevenir à cl c  
Sujet : Re: Constants and undefined behavior
De : antispam (at) *nospam* fricas.org (Waldek Hebisch)
Groupes : comp.lang.c
Date : 15. Jun 2026, 18:57:31
Autres entêtes
Organisation : To protect and to server
Message-ID : <110pee9$2icnv$1@paganini.bofh.team>
References : 1 2 3 4 5 6 7 8 9 10
User-Agent : tin/2.6.2-20221225 ("Pittyvaich") (Linux/6.1.0-9-amd64 (x86_64))
David Brown <david.brown@hesbynett.no> wrote:
On 15/06/2026 12:43, Waldek Hebisch wrote:
David Brown <david.brown@hesbynett.no> wrote:
On 15/06/2026 00:55, Keith Thompson wrote:
David Brown <david.brown@hesbynett.no> writes:
<snip>
[...]
Making this UB is an admission of the blindingly obvious - there is no
correct answer when signed integer overflow occurs.  It tells
programmers that it is a mistake to let your arithmetic overflow, and
it allows tools to help programmers avoid these mistakes, and it
allows compilers to give programmers the most efficient results from
known good code rather than adding unnecessary run-time checks that
are never triggered.
>
Trapping or raising/throwing an exception on overflow would also be an
admission of the blindingly obvious.
>
It is obvious - to me, anyway - that signed overflow is a mistake in the
code.  It is trying to do something that cannot be done.  What is the
single-digit sum of 5 and 8?  There is no answer.  The answer is not 3,
or 9.  Putting your hand in the air and asking the teacher for help
might be appropriate sometimes, but it is not a correct answer.
>
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.
>
>
And a sufficiently clever compiler
can omit some (not all) checks in cases where it can be statically
proved that overflow doesn't occur, and/or hoist some checks out of
loops.
>
Sure - but in practice having strict overflow checks would significantly
reduce optimisation and re-arrangement possibilities, as well as having
to include the checks themselves.  You might allow non-strict checks in
some manner (thus allowing optimisations like "a + b - a" reducing to
just "b"), but I think that might be hard to specify and would reduce
the debugging help of the checks.
 
IMO resonable and easy definition is: computation either delivers
mathematically correct result or traps, and it is not allowed to
trap in cases where naive bottom-up evaluation does not trap.
In more formal way optimization is not allowed to introduce
stronger precondition, but may weaken it.
 
 
It is always the case that an implementation can weaken preconditions
and strengthen postconditions and remain correct - though it might then
be less efficient than you expect.  But if you are /requiring/ a weaker
precondition and /requiring/ a strong postcondition - such as by
insisting on traps on overflow - you are changing the function or
operation specification, and it is not necessarily a good thing.
 
In C, the integer addition operation "c = a + b;" has a precondition :
 
       (a + b) <= INT_MAX, (a + b) >= INT_MIN
 
It has the postcondition :
 
       c == a + b
 
Saying that it must trap if there is overflow weakens the precondition
to any "a" and "b", but makes the postcondition much more complicated.

No.  Precondition is the same.  Postcondition has additional term
"computation finished with no traps".

It means it is no longer true that the result of an addition operation
is the sum of the operands.

Oposite of that: no traps means that regardless of precondition
the result of an addition operation is the sum of the operands.

 Addition is no longer a "pure" function -
now it has side-effects that are completely unpredictable at the site of
use.  Programmers can no longer rely on the timing of the operation,
stack usage, interaction with other code, or even that the operation
ever finishes.

The difference is that without traps programmers do not know if
arithmetic operations give correct result.  With traps they do
not know if program will successfully finish, but if it
finishes they know that arithmetic gave correct results.

If your code is correct, and overflow never happens, then this is all a
big disadvantage in terms of understanding and analysing the code.  And
it does not in any way reduce the effort needed to be sure that your
inputs are appropriate for getting the desired results of the operation.

One needs to use correct formulas, there is no way around that.
Without traps programmer must analyse ranges of all intermetiate
expressions.  That is tedious and error prone.  People work
around that by activating traps during testing, but it is
quite hard to find worst case values, so errors may be
easily missed during testing.  Having traps active during
production runs means that you may discover problem.  You
apparently think that ignoring possible problems at
runtime is good thing.  For simple programs you may analyze
it well enough to be sure that nothing bad happens at
runtime, but in general computing we use a lot of "interesting"
programs which are too complex to analyse.  We hope that
they will run OK, but have no proof.  Sometimes hope is
based on statistical tests and on low probability input
program may fail.  Traps are useful to make sure that
wrong results will not propagate further.

Trapping like this can certainly be useful for debugging.  But as a
general feature it gives a false sense of security, complicates
mathematical analysis, introduces massive additional possible code path
choices which are either real or almost certainly untested in practice,
or not real (because the compiler can see they are not taken) and
untestable.

You get extra code paths only if you attempt to handle traps.
Trapping of overflows gives you assurance that in computation that
you did and which finished with no traps there were no errors of
certain kind (that is wrong results due to overflow).  That is
really not different than insistence on static types.  Neither
assures you of no bugs, but each tells you that some bugs
did not happen.  Of course, trapping at runtime is less
satisfactory than compile time checking, but tight a priori
bounds on ranges are notoriusly hard to obtain, so trapping
is the best we can have for high performance software with
current state of art.

 That is not qualitatively worse than "who knows what will
happen" UB, but it is not significantly better.
 
 
<snip>
 
The correct way to handle the situation is to avoid it - be sure that
you are not dividing by zero in the first place.  Identify and handle
the problem where it occurs - when this zero is created, or the
circumstances leading to that point - rather than trying to do a
post-mortem after the failed division.  And if you are doing that, then
what benefit is there in having trapping for division by zero?  It
becomes just a waste of effort.
 
What is value of certification required for some software?  If
programmer did good job then program will work correctly.
 
Yes.
 
Trap give assurance that programmer indeed correctly handled
tricky problem. 
 
No, it certainly does not.  And one of the reasons to dislike traps is
that it makes people think like that.  A trap can only happen if the
programmer did /not/ handle the problem correctly.

Yes.

 And I expect that if
the programmer is able to write an appropriate specific trap handler for
the failing expression (rather than a program-global "crash with error
message" handler), then he/she would be able to avoid the problem in the
first place.

Rather non-specific trap handler could work as "redo the computation
in arbitrary precision".  If problem (like division by zero) persists,
then there is logic bug, otherwise it means that precision was
inadequate and problem is resolved.

Howver, you should think about such traps similarly to parity error
which can be signaled by some hardware.  There is low but nonzero
probablity that such error can occur.  Parity check gives you
reasonable chance to detect it.  Handling is at least as problematic
as with overflow.  Absence of traps gives you less info: no
overflow traps mean no overflow, no parity traps means that
parity was correct, but intent of parity check it to discover bit
error and they are possible even with correct parity.  So, do you
think that parity check inside MCU-s are useless?

Sometimes, of course, you are trying to write code that has some input
which is supposed to be correct, but you are not sure - and you can't
change the calling code.  How you handle that situation will depend on
the program and the situation.  But I don't see trapping as "correct
handling" unless the whole program is written with the expectation of
traps for error handling.  You might, however, end up deciding that
trapping is the least bad option.
 
 
And once you know that computation works
according to math rules other forms of verification are easier.
 
You also seem to have bias to real time control: if you need
value just at given moment, then it is hard to do something
reasonable.  But at least in some control areas there is
notion of "safe state", for example working heavy machine
is dangerous, stopped one usually is considerd safe.  If
there is safe state, then anything not expected by program
should trigger transition to safe state.
 
I think if you are /not/ concerned with high efficiency in the code,

Well, if efficiency does not matter traps can be implemented as
a software layer above the language.  Or one can use arbitrary
precision arithmetic.  Traps matter when efficiency matters,
so they should be implemented in place giving best efficiency,
at best in CPU and if that is not possible then in optimizing
compiler.

then you should be seriously questioning the choice of C as the language
in the first place.  And even if you use C, there are often things you
can do to avoid having problems in the first place.  The obvious one for
integer overflow is to make more use of bigger types.

Which may be best choice if efficiency is not important.  But
some calculations require surprisingly large accuracy to avoid
overflow.  Worse, in vast majority of cases lower accuracy
may be adequate, so there is pressure to use "sufficient"
accuracy overlooking special cases.
 
In general computation, if you need correct value and have some
time there are options which may involve re-doing computation at
higher precistion, which may get rid of occasional overflows
and divisions by zero due to overflow.  Division by zero may
be due to bad input data, traps allow indentification of
such data (doing it in other way may be computationaly quite
expensive).
 
 

--
                              Waldek Hebisch

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