| ▲ | hn_submit 4 hours ago | |||||||||||||
I believe this is barking up the wrong tree since IMHO C is just "high level assembly" for systems programming. As soon as you add runtime behavior to combat Undefined Behavior (UB) you're blowing up execution times. And static analysis can only go so far without blowing up compile times. C is "the right tool for the right job" which is operating systems and its code which is called thousands of times per second. You cannot afford even one iota of runtime checks in that code. The developer must know what he's doing or he should get out of the kitchen. We should discourage the usage of C in application programming and prod application developers towards memory safe languages like Rust or Go. And I'm not even sure if Rust solves this case as far as UB is concerned. | ||||||||||||||
| ▲ | flohofwoe 8 minutes ago | parent | next [-] | |||||||||||||
> "high level assembly" That's already a fundamentally wrong assumption :) With today's compilers, C is a high level language like all the others. The optimizations happening to C code are not fundamentally different than for any other compiled high level language. > We should discourage the usage of C in application programming and prod application developers towards memory safe languages like Rust or Go. No that's rubbish, just as it would be rubbish trying to 'discourage' people from writing programs in assembly code or any other programming language. Ultimately, memory safety is the job of the sandbox your untrusted code is running in (e.g. the browser, operating system or VM). The only difference between Rust and any "unsafe" language should be that one fails already at compilation time and the other at runtime (because the sandbox killed your rogue process). E.g. if an operating system is exploitable because it allows untrusted programs to reach out of the sandbox, then that problem must be fixed in the operating system. | ||||||||||||||
| ▲ | SkiFire13 3 hours ago | parent | prev | next [-] | |||||||||||||
> C is just "high level assembly" for systems programming It is not, and that's the issue. If it was just "high level assembly" there would be no UB, and no need to have UB. Instead you have UB (although arguably not all of it is really needed) because you need optimizations, which in turn you need because otherwise C would be too slow for that "operating system" job. > code which is called thousands of times per second Scripting languages can easily have loops running thousand of times per second and even more. You're off by some order of magnitudes here if you want to describe operations that happen in operating systems. | ||||||||||||||
| ▲ | gizmo686 3 hours ago | parent | prev | next [-] | |||||||||||||
A high level assembly would not have a UB problem, because the generated machine code would closely map to your source code, so even technically undefined behaviour would end up doing the expected thing for the given hardware. The problem with C is that modern compilers do a lot of transformations between your source code and the final machine code, so the actual behavior could be very far afield from what you would expect. > And I'm not even sure if Rust solves this case as far as UB is concerned. If your entire program is inside unsafe, then Rust is actually worse than C as far as UB is concerned. On the other hand, no one writes Rust like that, and Rust restricts all UB to unsafe blocks. | ||||||||||||||
| ||||||||||||||
| ▲ | chasil 3 hours ago | parent | prev | next [-] | |||||||||||||
C is also famously bad at floating point optimization. Fortran has historcally led this realm (see the Numerical Recipes book). Julia is a newer option, and I understand that both are commonly used in Python objects. "Read the older 2nd ed. book in Fortran online for free." | ||||||||||||||
| ||||||||||||||
| ▲ | weinzierl an hour ago | parent | prev | next [-] | |||||||||||||
"And static analysis can only go so far without blowing up compile times." Compile time is not the main issue with static analysis. It's that C doesn't provide enough information and not the right information to make it efficient and effective. If you add this info you unavoidably will end up with something looking like Rust. (Whose long compile times are not caused by its static analysis parts BTW) | ||||||||||||||
| ▲ | jandrewrogers 3 hours ago | parent | prev | next [-] | |||||||||||||
C is not “high level assembly”. You still need a model of what the code will create. This does not naturally follow from C code. Modern C++ is the most powerful systems language if you care about performance. While I don’t condone writing new code in C, there are major conceptions about its relationship to performance and C++. | ||||||||||||||
| ▲ | mdspan 3 hours ago | parent | prev | next [-] | |||||||||||||
Operating systems code can definitely afford runtime checking if it's not too expensive in terms of performance. The Linux kernel has a WARN_ON macro that does exactly this. The tradeoff is worth it in a lot of cases if you're exchanging a small amount of performance for greater debug-ability or security. | ||||||||||||||
| ▲ | imtringued 10 minutes ago | parent | prev [-] | |||||||||||||
Hot take: The vast majority of UB optimizations in C are just hacks to get around mutable aliasing being the default. | ||||||||||||||