7 ms·
Why oh why does "undefined behavior in C" come up constantly here? I wrote C every day for decades. I was aware there's such a thing as undefined behavior. I ca
by dboreham 6y ago
Why oh why does "undefined behavior in C" come up constantly here? I wrote C every day for decades. I was aware there's such a thing as undefined behavior. I can't remember it ever being even a miniscule factor in my daily work. What's changed?
- codys 6y agoOne piece of this is probably that compilers competing to generate the best code have increasingly incorporated understanding of undefined behavior into their optimizers, leading to things that were previously undefined but happened to behave in some particular way to instead behave in ways that break programs using undefined behavior. Compilers incorporate inferences that can be made by assuming UB doesn't occur into their optimization because it improves code generation of correct programs.
- ryandrake 6y agoThis is one of the humps that C programmers eventually just need to get past: That UB means literally anything is possible[1]. Not just what you currently see happening with the current version of your compiler on your current platform. Anything! We've all run into a few programmers who would insist that their particular invocation of undefined behavior was fine, because as you said, it "behaves in some particular way". Now that we have more aggressively optimizing compilers we don't run into those folks so much anymore. I wouldn't say it's a minuscule factor in my daily work, as OP put it, but it's not something to agonize about if you stay mindful of avoiding common undefined behavior pitfalls. It would be very helpful if compilers were better about warning you when you're venturing into UB, but that's a different topic. 1: You can even get the shirt: https://teespring.com/shop/undefined-behavior-shirt https://teespring.com/shop/undefined-behavior-shirt
- perl4ever 6y ago>That UB means literally anything is possible Many people say this but nobody "literally" believes it. It's a way of expressing the righteous dominance of one person or group's attitude towards reasonableness over another's. In a way that forestalls argument. "Literally anything" could literally mean global thermonuclear war.
- abainbridge 6y agoCompilers started to generate surprising code more regularly. Here's an example of a function taking void* parameters, that are cast to uint64_t* and then used. Upto gcc 6.3, you get a "sensible" result. After gcc 7, with -O2, you get a surprising result. https://godbolt.org/z/ugSDmr https://godbolt.org/z/ugSDmr If you get Godbolt to run the program and show the output, you'll see it change with GCC version. Obviously what is "sensible" and what is "surprising" is subjective.
- abainbridge 6y agoPeople tell me that the problem with this code is that casting one pointer type to another and then dereferencing is undefined behaviour. The problem is that C code seems to do this all the time. For example, fread() in the standard library takes a void* as its first parameter. So at first glance, most code that uses fread() must exhibit undefined behaviour. eg this function: int foo(FILE *f) { int val; fread(&val, sizeof(val), 1, f); return val; } Closer reading of the spec says something about it being fine to cast, say, an int* to a float* and then cast back before dereferencing it. In this example, we're OK, because we don't reference via the void* . But the fread implementation must do, right? I'm left feeling like it is impossible to implement fread() in C and that the standard library's API is no longer considered a good example of how to construct an API. I guess the original motivation for it being UB to cast is to do with alignment constraints on some hardware. For example, a machine might be able to read an int8_t from any alignment but might require 4-byte alignment for int32_t. If so, then casting a non-aligned int8_t* to int32_t* and deferencing it would indeed fail on that hardware. But some would argue that it is not right that GCC 7 breaks such code on a machine where that problem doesn't exist.
- chrisseaton 6y ago> The problem is that C code seems to do this all the time. People should write these bits in assembly (as little as possible, but doing the casting and things), and then keep their C code well-defined using these low-level library routines.
- 6y ago
- bluetomcat 6y agoUB is blown out of proportion by compiler vendors and language-lawyer types concerned with portability across exotic ranges of machines, including those, where, for example, CHAR_BIT != 8. For the most part, avoiding UB is common sense when you grasp the basic abstract C memory model - types of storage and their lifetime, avoiding out of bounds array access, keeping track of the lifetime of all allocations, etc.
- srtjstjsj 6y agoThat's not the issue. The issue that simple bugs become global program transformations into nonsense due to aggressive optimizations, when they should instead be compile-time errors.
- armitron 6y agoIt's actually a big problem and anything but common sense. Do all C programmers know what strict aliasing means? How about pointer provenance? Have they memorized integer promotion rules? These are all potential sources of undefined behavior that can lead to catastrophic results. I bet most experienced C programmers with decades under their belt would fail one or more of these tests.
- oconnor663 6y agoOther folks have mentioned that optimizing compilers are more aggressive these days. To that I'd add: - More applications are talking to the internet and being exposed to malicious input. There's also more money to be made in finding exploits, and more people and governments looking. At the same time, fuzzing techniques are getting dramatically better. - Applications are more likely to run across different architectures (mobile) and different compiler versions (faster pace of compiler development), leading to more opportunities for "something weird" to happen. Multithreading is also more common. - Memory-safe languages have taken a larger market share, and the popular attitude towards memory safety issues is changing from "bugs are a fact of life" to "this is a drawback of specific languages".
- perl4ever 6y agoHow exactly do you know it wasn't a factor? Do you have an infallible method of identifying UB in C?