frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Everything in C is undefined behavior

https://blog.habets.se/2026/05/Everything-in-C-is-undefined-behavior.html
50•lycopodiopsida•53m ago

Comments

dmitrygr•19m ago
I stoped reading about here:

    > bool parse_packet(const uint8_t* bytes) {
    >   const int* magic_intp = (const int*)bytes;   // UB!
Author, if you are reading this, please cite the spec section explaining that this is UB. Dereferencing the produced pointer may be UB, but casting itself is not, since uint8_t is ~ char and char* can be cast to and from any type.

you might try to argue that uint8_t is not necessarily char, and while it is true that implementations of C can exist where CHAR_BIT > 8, but those do not have uint8_t defined (as per spec), so if you have uint8_t, then it is "unsigned char", which makes this cast perfectly safe and defined as far as i can tell. Of course CHAR_BIT is required to be >= 8, so if it is not >8, it is exactly 8. (In any case, whether uint8_t is literally a typedef of unsigned char is implementation-defined and not actually relevant to whether the cast itself is valid -- it is)

weinzierl•18m ago
A fun one that'd fit list be sequence point violations like

    i = i++
radiospiel•6m ago
Fun, sure, but also GCC and Clang will both warn with -Wall (-Wsequence-point / -Wunsequenced).
nokeya•17m ago
Ok, and?
wg0•8m ago
"Rewrite everything in Rust. OMG universe is written in Rust so memory safe with zero allocations"
veltas•17m ago
From the ANSI C standard:

  3.16 undefined behavior: Behavior, upon use of a nonportable or erroneous program construct, of erroneous data, or of indeterminately valued objects, for which this International Standard imposes no requirements.  Permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message).
Is it just me or did compiler writers apply overly legalistic interpretation to the "no requirements" part in this paragraph? The intent here is extremely clear, that undefined behavior means you're doing something not intended or specified by the language, but that the consequence of this should be somewhat bounded or as expected for the target machine. This is closer to our old school understanding of UB.

By 'bounded', this obviously ignores the security consequences of e.g. buffer overflows, but just because UB can be exploited doesn't mean it's appropriate for e.g. the compiler to exploit it too, that clearly violates the intent of this paragraph.

dataflow•14m ago
> but that the consequence of this should be somewhat bounded or as expected for the target machine.

Aren't "unpredictable results" and "no requirements" contrary to the idea that the behavior would be "somewhat bounded"?

veltas•5m ago
Notice though "ignoring the situation" thru "documented manner characteristic of the environment". Even though truly you can read this in an uncharitable way, you could also try and understand the intent of this paragraph, and I think reading it for its intents is always the best way to interpret a language standard when the wording is ambiguous or soft, especially if you're writing a compiler.

I don't think you could sincerely argue that this definition intends to allow the compiler to totally rewrite your code because of one guaranteed UB detected on line 5, just that it would be good to print a diagnostic if it can be detected, and if not to do what's "characteristic of the environment". Does that make sense?

stackghost•16m ago
Anyone who uses the construction "C/C++" doesn't write modern C++, and probably isn't very familiar with the recent revisions despite TFA's claims of writing it every day for decades.

Far from being just "C with classes", modern C++ is very different than C. The language is huge and complex, for sure, but nobody is forced to use all of it.

No HN comment can possibly cover all the use cases of C++ but in general, unless you have a very good reason not to:

- eschewing boomer loops in favor of ranges

- using RAII with smart pointers

- move semantics

- using STL containers instead of raw arrays

- borrowing using spans and string views

These things go a long way towards, shall we say, "safe-ish" code without UB. It is not memory-safe enforced at the language level, like Rust, but the upshot is you never need to deal with the Rust community :^)

rectang•12m ago
> the upshot is you never need to deal with the Rust community

In the end, everything comes down to culture war.

stackghost•11m ago
Perhaps we should rewrite our culture in Rust.
veltas•9m ago
Although some people, like Bjarne Stroustrup, object to the term C/C++, it's a bit like Richard Stallman objecting to the term "Linux". The fact is it can mean "C or C++", and I wouldn't assume the author thinks they're the same, but they're talking about both of them together in the same sentence. This seems reasonable given this is about undefined behavior, and it's trivial to accidentally write UB-inducing code in C++ even with modern style (although I'd say you should catch most trivial cases with e.g. ubsan, and a lot of bad cases would be avoided with e.g. ranges, so I think the article is exaggerating the issue).
stackghost•4m ago
Well, the author explicitly refers to "C/C++" as one language:

>After all, C/C++ is not a memory safe language.

flohofwoe•7m ago
"C/C++" is a useful term for the common C/C++ subset :)

As far as stdlib usage is concerned: that's just your opinion. The stdlib has a lot of footguns and terrible design decisions too, e.g. std::vector pulling in 20k lines of code into each compilation unit is simply bizarre.

Also:

- borrowing using spans and string views

Those are just as unsafe as raw pointers. It's not really "borrowing" when the referenced data can disappear while the "borrow" is active.

gritzko•6m ago
I use a C dialect that has generic containers, slices and buffers, while pointer arithmetic is forbidden. At this point, C stdlib has near zero value, unless it is a syscall or a function specially treated by the compiler (e.g. memcpy).
bestouff•14m ago
The problem of UB is not really that it may crash in some architecture. The real problem is that the compiler expects UB code to NOT happen, so if you write UB code anyway the compiler (and especially the optimizer) is allowed to translate that to anything that's convenient for its happy path. And sometimes that "anything" can be really unexpected (like removing big chunks of code).
raluk•11m ago
In C / C++ there are two kinds of undefined behaviour. One is where there is written in standard what UB is. Another one is everthing else that is not in standard.
llggbbtt•10m ago
I love arguing with idiots.
__0x01•9m ago
> A problem with this is that in order to confirm the findings, you’ll need an expert human. But generally expert humans are busy doing other things.

The article suggests using LLMs to identify and fix UB. However as per the above, I think the issue is that we need more expert humans.

LLM generated code will eventually contain UB.

EDIT: added "eventually"

jraph•9m ago
Yet another push to use LLMs after casting fear. Now it should be illegal not to use LLMs. A good start of the day.

(I hope casting fear is not UB)

wg0•7m ago
The irony is unmistakable.

Everything in C is undefined behavior

https://blog.habets.se/2026/05/Everything-in-C-is-undefined-behavior.html
58•lycopodiopsida•53m ago•25 comments

Railway Blocked by Google Cloud

https://status.railway.com/?date=20260519
418•aarondf•6h ago•203 comments

FiveThirtyEight articles on the Internet Archive

https://fivethirtyeightindex.com/
145•ChocMontePy•5h ago•37 comments

Gemini 3.5 Flash

https://blog.google/innovation-and-ai/models-and-research/gemini-models/gemini-3-5/
727•spectraldrift•13h ago•514 comments

I’ve built a virtual museum with nearly every operating system you can think of

https://virtualosmuseum.org/
719•andreww591•15h ago•163 comments

GitHub Compromised

https://twitter.com/github/status/2056949168208552080
182•claaams•2h ago•52 comments

Remove–AI–Watermarks – CLI and library for removing AI watermarks from images

https://github.com/wiltodelta/remove-ai-watermarks
241•janalsncm•8h ago•130 comments

Google changes its search box

https://blog.google/products-and-platforms/products/search/search-io-2026/
488•berkeleyjunk•12h ago•670 comments

Show HN: Forge – Guardrails take an 8B model from 53% to 99% on agentic tasks

https://github.com/antoinezambelli/forge
415•zambelli•18h ago•161 comments

Nostalgic Electronics Kits Central

https://www.nostalgickitscentral.com/
13•cf100clunk•2d ago•4 comments

OpenAI Adopts Google's SynthID Watermark for AI Images with Verification Tool

https://openai.com/index/advancing-content-provenance/
259•smooke•11h ago•131 comments

Apple unveils new accessibility features

https://www.apple.com/newsroom/2026/05/apple-unveils-new-accessibility-features-and-updates-with-...
654•interpol_p•18h ago•338 comments

Mistral AI acquires Emmi AI

https://www.emmi.ai/news/mistral-ai-acquires-emmi-ai
232•doener•11h ago•63 comments

Ex-Apple engineer says Apple deliberately slows older phones via updates

https://www.macobserver.com/news/ex-apple-software-engineer-claims-apple-deliberately-slows-down-...
28•neya•1h ago•5 comments

In 1979 engineer Hugh Padgham discovered "gated reverb" – by accident

https://producelikeapro.com/blog/how-one-recording-mistake-created-a-musical-phenomenon-in-the-80s/
26•bookofjoe•2d ago•1 comments

Skills in Web, iOS, and Android

https://x.ai/news/grok-skills
15•surprisetalk•1d ago•1 comments

Gemini CLI will stop working from June 18, 2026

https://developers.googleblog.com/an-important-update-transitioning-gemini-cli-to-antigravity-cli/
165•primaprashant•12h ago•72 comments

GitHub is investigating unauthorized access to their internal repositories

https://twitter.com/github/status/2056884788179726685
360•splenditer•6h ago•114 comments

The Mercury logic programming system

https://github.com/Mercury-Language/mercury
56•Antibabelic•1d ago•10 comments

Minnesota becomes first state to ban prediction markets

https://www.npr.org/2026/05/19/nx-s1-5821265/minnesota-ban-prediction-markets
611•ortusdux•11h ago•190 comments

Evals will break

https://wanglun1996.github.io/blog/your-evals-will-break.html
24•rajveerb•3h ago•4 comments

Japan is gripped by mass allergies. A 1950s project is to blame

https://www.bbc.com/future/article/20260515-the-1950s-blunder-which-causes-mass-hay-fever-in-japan
21•ranit•5h ago•0 comments

India's hottest district shuts at 10 am as mercury breaches 48 C mark

https://www.hindustantimes.com/india-news/indias-hottest-district-banda-shuts-at-10-am-as-mercury...
63•rustoo•1h ago•45 comments

Lisp in Web-Based Applications (2001)

https://sep.turbifycdn.com/ty/cdn/paulgraham/bbnexcerpts.txt
73•bschne•1d ago•5 comments

Testing MiniMax M2.7 via API on three real ML and coding workflows

https://andlukyane.com//blog/minimax-m27-workflows
9•Artgor•2h ago•0 comments

Growing Neural Cellular Automata

https://distill.pub/2020/growing-ca/
98•pulkitsh1234•2d ago•11 comments

I’ve joined Anthropic

https://twitter.com/karpathy/status/2056753169888334312
1269•dmarcos•15h ago•525 comments

HTML-in-Canvas Demos

https://github.com/GoogleChromeLabs/css-web-ui-demos/blob/main/html-in-canvas/awesome-html-in-can...
30•simonpure•7h ago•9 comments

Museum of Imaginary Musical Instruments

https://imaginaryinstruments.org/
25•bookofjoe•2d ago•5 comments

The two oldest printing presses

https://museumplantinmoretus.be/en/worlds-two-oldest-printing-presses
38•janpot•2d ago•17 comments