🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: sci.electronics.design
Article #975

Re: Arms race

#975
From: Waldek Hebisch
Date: Mon, 09 Nov 2026 21:14
134 lines
7987 bytes
Don Y <blockedofcourse@foo.invalid> wrote:
> On 11/8/2026 12:27 PM, Waldek Hebisch wrote:
>> You clearly go into area were you are not an expert.  Google a
>> bit about "undefined behaviour" in C and you will learn that
>> when source code makes no sense, then C compier can do
>> essentially anything, which in not so rare cases means that
>> compiler simply deletes what programmer wrote.  Explicit nop-s
> 
> The compiler doesn't have THAT much license.  It can't decide to
> implement arbitrary operations just because you APPEAR to be
> relying on a behavior that is undefined.

Compiler just uses rules, so writing about appearence makes no
sense.  When optimizing compier performs a lot of transformations.
Validity of transformations may depend on conditions.  When
checking conditions compiler assumes that program does not
contain undefined behaviour.

And yes, C standard gives such freedom to authors of C
compilers:

: undefined behavior
: behavior, upon use of a nonportable or erroneous program construct
: or of erroneous data, for which this document imposes no requirements

Do I need to explain to you what "no requirements" mean?

> E.g., memcpy() is free to walk past the end of the address space
> if the source/destination address -- plus the length -- moves it
> to that point.  Or, it could opt to be more reasonable and inhibit
> such action.  Either solution is an acceptable behavior -- regardless
> of the behavior that you *hoped* would occur.
> 
> But, it can't decide to divide every value stored in EVERY memory
> location by 3 -- unless the value stored there is a 7 in which case
> it replaces it with a 42.  A compiler/library designer wouldn't
> deliberately come up with a bizarre action to perform when the required
> action isn't clear.  He'll do what seems logical in a normal case,
> even if that isn't what the developer intended (developer broke the
> contract by trying to do something that wasn't defined; we don't
> punish him by killing his first-born...)

What you write is typical fallacy.  Of course compiler writers
will not deliberatly generate malicious code.  But once your
part of contract is broken there is no reasonable way to
limit possible damage.  If compiler recognizes one of common
errors it can warn and generate code that say signals
error at runtime.  But it is pretty commmon situation that
compiler can not check if code is free of undefined behaviour.
In such cases compiler transforms the code assuming that
code is free of undefined behaviour.  Elementary logic
says that from false assumption you can prove anything.
So, when there is undefined behaviour, there is no limit
on wrong reasonings that compiler can make and consequently
no limit of possible transformations.  Of course, any
given compiler contains only finitely many transformations
and logical reasonings in the compiler are rather simple.
But one of simplest transformations is "delete code that
is never executed".  And related "if condition of 'if'
statement is always true, then 'else' branch is never
executed'.  So, one you have undefined behaviour compiler
may easily delete large part of your program.  In effect
if control passes to a function which should be not
called execution may "fall trough" to following code.
If that code is doing something weird, you will see this
as effect of your program.

This was already understood in seventies: either you define
what happens or you get potentially arbitrary conseqences.
Defining things may get you one step ahead, but in many
cases defined behaviour is wrong.  PL/1 tried hard to define
effects of various operations, users found out that many
cases operations were well defined, but did different thing
than what was needed and programs behaved in unexpected ways.
Catching errors at runtime could help, but in the past
it was deemed to costly.  And currently, in C (which is
widely used) catching errors at runtime is much more
costly than in other languages like Pascal or Ada
(or modern Fortran if you prefer that).  C++ is better,
as long as you avoid constructs inherited from C.
Anyway, except for cases like Rust and Ada current system
programming languages basically force programmers to
use potentially unsafe constructs with possible
consequences equvalent to undefined behaviour in C.

>> are rarely emited by compilers, but one can get 'return'
>> (if compiler decides that garbage source is doing nothing
>> useful, but it will eventually finish) or executing whatever
>> happens to be after deleted code (if compiler decides that
>> code should be never executed because it performed illegal
>> operation).
> 
> What "illegal operation" -- beyond a SIGSEGV?  The compiler
> doesn't prevent me from dividing by zero (though many are
> astute enough to notice that and WARN me).  Or, from overwriting
> a table of interrupt vectors.  Or, the software itself!

Actually, at '-O2' version of 'gcc' that I use replaces explicit
division by 0 by the 'ud2' instruction.  But that missed the point,
when you derefernce a pointer gcc assumes that it is nonzero,
when you divide that divisor is nonzero.  Importantly, gcc
assumes that operations on integers do not overflow.  Typically
compiler can not prove at compile that if anything above
happens, so in first approximation it just generates corresponding
machine instrictions.  But implied conditions may lead to
conseqences, like deleting code which is unreachable by
valid execution.

If you want list of "illegal operations", then appendix in C
standard contains rather long list of things which are undefined
behaviour, trying any of them is illegal from standard point
of view.  gcc uses only some of them for optimization, like
the few I mention above and the type based aliasing rules.
But new release/different compiler may take advantage of
a different undefined behaviour.  So, if you use a single
compiler look at what promises it makes, if you want
wide portability then you should avoid all undefiend
behaviour.

> It can *advise* against things that seem likely to be errors
> or result in anomalous behaviors.  But, it's the developer who opts
> to override those warnings.  No rule says you have to check malloc()'s
> return value.  And, /in some cases/, you may be 100% confident that
> it is *NOT* NULL.
> 
> [That's why folks who don't address every warning are shoddy
> developers -- make it explicitly clear to me and the compiler
> that you actually WANT this behavior!]
>> It is much nicer when you share things that you do know.
> 
> 

-- 
                              Waldek Hebisch
Technical Details
Message-ID: <11ctd4b$3rem1$1@paganini.bofh.team>
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!paganini.bofh.team!not-for-mail
References: <11bp6d1$16i93$1@dont-email.me> <a8n6elh26hc5cg98pn84idqp7rrel2l47o@4ax.com> <11bvnp8$3ehgg$1@dont-email.me> <11c0065$3hr4g$1@dont-email.me> <39e9elp8cqum5iatg21ltbf06hc4p4dgec@4ax.com> <eig9ellto4nl35n0cd8elct4boi2018mcp@4ax.com> <11ck9s9$2dmvp$1@dont-email.me> <fe8selhl8um5p168shk979cms2elurlhh1@4ax.com> <11cma06$34o3u$2@dont-email.me> <4rbtelp38pp2qnaqha6cq7csbfafpgk9i9@4ax.com> <11cmffm$36ee0$1@dont-email.me> <11cqieb$3h7vq$2@paganini.bofh.team> <11cqnt0$mtaq$1@dont-email.me>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!paganini.bofh.team!not-for-mail
From: antispam@fricas.org (Waldek Hebisch)
Newsgroups: sci.electronics.design
Subject: Re: Arms race
Date: Mon, 9 Nov 2026 21:14:53 -0000 (UTC)
Organization: bofh.team Usenet service
Message-ID: <11ctd4b$3rem1$1@paganini.bofh.team>
References: <11bp6d1$16i93$1@dont-email.me>   <a8n6elh26hc5cg98pn84idqp7rrel2l47o@4ax.com> <11bvnp8$3ehgg$1@dont-email.me> <11c0065$3hr4g$1@dont-email.me> <39e9elp8cqum5iatg21ltbf06hc4p4dgec@4ax.com> <eig9ellto4nl35n0cd8elct4boi2018mcp@4ax.com> <11ck9s9$2dmvp$1@dont-email.me> <fe8selhl8um5p168shk979cms2elurlhh1@4ax.com> <11cma06$34o3u$2@dont-email.me> <4rbtelp38pp2qnaqha6cq7csbfafpgk9i9@4ax.com> <11cmffm$36ee0$1@dont-email.me> <11cqieb$3h7vq$2@paganini.bofh.team> <11cqnt0$mtaq$1@dont-email.me>
Injection-Date: Mon, 9 Nov 2026 21:14:53 -0000 (UTC)
Injection-Info: paganini.bofh.team; logging-data="4045505"; mail-complaints-to="usenet@bofh.team"; posting-host="d028f9ad2fa2c51f22dbfed0d81d7daf"; posting-account="69e66f1681224ab92235b464ce7a90b6"
User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (Linux/6.1.0-9-amd64 (x86_64))
X-Postfilter: Postfilter-NG 2026.10.2