4 ms·
And yet, they managed to write the Linux kernel in it, as well as thousands of other things we use every day.
by fortran77 6y ago
And yet, they managed to write the Linux kernel in it, as well as thousands of other things we use every day.
- tinus_hn 6y agoObviously the most dangerous form of undefined behavior is not that which always crashes but that which for some unlikely inputs cases crashes.
- staticassertion 6y agoI'm not sure what your point is. The Linux kernel is riddled with undefined behavior and billions are spent on finding, removing, or mitigating that.
- me_me_me 6y agoI wonder what would happen if i put hand into fire. But I have never done it in my life, because it might cause 'undefined behaviour'. In my years of working with C its always the 'look how smart I am code' causes issues. Keep it clean and simple. Avoid complexity and fluff features of C. And you will not going to have deal with exotic bugs.
- cortesoft 6y agoI am pretty sure your hand going in fire is pretty defined behavior
- me_me_me 6y agoIs it though, for yourself/your body. Is putting hand into a fire something you experienced and internalized?
- cortesoft 6y agoNo, but it is part of the spec
- perl4ever 6y agoOf all the possible things that could happen to me, I am very confident that only a small subset could result, so it is pretty well defined. Nasal demons, for instance, are not on the menu.
- me_me_me 6y agoWe are really straying off actual point. Why would you not actively avoid undefined behaviour and instead take risk. Zeroing out variables, checking pointers coming from external modules etc. Not rallying on a compiler to resolve fancy stuff. Like the example: void contains_null_check(int P) { int dead = P; if (P == 0) return; P = 4; } Leave declarations uninitialized or zeroed Check input. Work with data. void contains_null_check(int P) { int dead; if (P == 0) return; int dead = P //work P = 4; } This leaves less space for error/interpretation and IMHO it keeps code cleaner. Any fancy optimizations should be used only for critical path and only when absolutely necessary.
- geofft 6y ago"Managed" is a strong word - there's regular complaining about compiler optimizations, there was a string of vulnerabilities where null checks were optimized out because they were secretly UB, and there was a discussion just last week about how compilers were reordering certain patterns in unexpected ways: https://linuxplumbersconf.org/event/7/contributions/821/ https://linuxplumbersconf.org/event/7/contributions/821/ Quoting from the abstract of that talk: "... preserving source-level dependencies through to the generated CPU instructions is achieved through a delicate balance of volatile casts, magic compiler flags and sheer luck. Wouldn't it be nice if we could do better?"
- barrkel 6y agoOne of the more pernicious problems is security checks written to detect underhanded violations of constraints being eliminated because the compiler doesn't recognize the underhandedness is possible. In a past life, I depended on signed overflow to detect out of bounds memory allocations. That kind of stuff gets compiled away these days, you need to be cleverer to detect when the user is deliberately trying to invoke UB. And because the compiler doesn't believe in UB, there's the risk it's going to remove your detection logic.
- sharpneli 6y agoMaking signed overflow to be well defined would be so much better. IIRC C++ just decided that signed numbers are two's complement and that's that. C could really use similar thing.
- steerablesafe 6y ago> IIRC C++ just decided that signed numbers are two's complement and that's that. Signed integer overflow is still undefined in C++.
- sharpneli 6y agoYou are correct. I misremembered, it was just a proposal that was not taken into the spec.
- steerablesafe 6y agoNo, it's taken into the spec. It affects unsigned <-> signed conversions and maybe other things. Not integer overflow though.
- sharpneli 6y agoAh. Makes sense as that’s relatively cheap to emulate by just doing conversion when loading and storing from memory, but allows the use of HW even in case of ones complement implementation.