Get the latest tech news

How GCC and Clang handle statically known undefined behaviour


UB or not UB: How gcc and clang handle statically known undefined behaviour25 June 2024 Recently, we had a discussion in our team about undefined behaviour (UB) in C. For those unfamiliar: We say that a program has undefined behaviour when we write code where the language specification doesn't define what should happen during execution.

2) Why are clang (and sometimes gcc) lenient when handling UB, compiling (and running) code instead of making it crash (e.g. by inserting an illegal instruction)? The goal was to show a difference in philosophies when handling UB: LLVM just carries on compiling when it can, crossing its fingers that this won't cause problems later on, in an attempt to to make more programs run and to closer match what it believes a developer, unaware of undefined behaviour in their code, might expect. Neither approach is objectively better than the other and both are equally valid in the face of UB, and which one to choose ultimately comes down to personal preference of the compiler developers and their users.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of gcc

gcc

Photo of Clang

Clang

Related news:

News photo

GCC 12.4 Compiler Released With 84+ Bugs Fixed

News photo

Zhaoxin "Shijidadao" x86_64 CPU Support Merged Into The GCC 15 Compiler

News photo

IBM Ready With POWER11 Support "-mcpu=power11" For GCC