4 ms·
> For example, accessing a std::span or a std::vector outside of its bounds would abort the program, and so would accessing an empty std::optional. This can be
by saagarjha 4y ago
> For example, accessing a std::span or a std::vector outside of its bounds would abort the program, and so would accessing an empty std::optional. This can be done while staying Standards-conforming because undefined behavior implies that the library can do whatever it wants, which includes aborting.
sickosyes.jpg
Getting bad operations to abort execution (e.g. by throwing an exception) seems impossible to ship, not in the least due to certain, uh, entities really being against it. But abusing UB to allow people to turn on checks is excellent. This is why we should be very careful about what we choose to standardize-a common request is making signed overflow wrap instead of being UB, which prevents its diagnosis in this way.
- olliej 4y agoMaking out of bounds access on explicitly bound types is an easy memory safety win. The fact the UB is still being used as an excuse to clearly undermine reasonably expected behaviour remains a BS trope, and WG21's continued acceptance of new features and specifications that say perfectly specifiable behaviour is UB instead of unspecified is a continuing source of avoidable security bugs. It is inexcusable for new features to be added to C++ that have UB in any case other than the undefinable (invalid memory access, etc). Removing the copious unnecessary UB from the existing specs should be a higher priority that many of the new features being proposed.
- saagarjha 4y agoMuch that is UB deserves to be defined. And some that is defined deserves to be UB. Can you make it illegal? Then do not be too eager to deal out behaviors in standardization. I don't actually oppose, in the long run, slowly picking some behaviors and changing them to be standardized. But I do vehemently oppose many of the suggestions that are brought up. The kinds of things that should be moved out of being undefined are things like "you named your function isfoo" and not "you passed NULL to memset".
- olliej 4y ago> And some that is defined deserves to be UB. Can you provide an example of currently specified behaviour that should be UB? To be very clear I am using specification language here. "Unspecified behaviour" is very much not "undefined behaviour".
- flohofwoe 4y agoIt's an LoTR joke: "Many that live deserve death. And some that die deserve life. Can you give it to them? Then do not be too eager to deal out death in judgement"
- mgaunard 4y agoUndefined behaviour is great. I for one will continue to vote in its favour. C++ does not add runtime overhead to validate that programs are well-formed.
- staticassertion 4y ago> C++ does not add runtime overhead to validate that programs are well-formed. You have succinctly described the problem.
- mgaunard 4y agoThat's not a problem, that's a core policy of that language's design. If you want a language that is slow, there are plenty of options.
- olliej 4y agoCan you provide examples of UB that could be unspecified behaviour, where UB does anything other than cause harm?
- mgaunard 4y agoDereferencing a pointer to memory that doesn't point to a live valid object is undefined behaviour. You couldn't possibly specify what this does without instrumenting every single access to memory, like asan or valgrind does.
- olliej 4y agoAs I said, behaviour that could be unspecified rather than undefined. This is an example of something that cannot be anything other than UB.
- kllrnohj 4y agohttps://www.youtube.com/watch?v=yG1OZ69H_-o https://www.youtube.com/watch?v=yG1OZ69H_-o is a great talk on this
- liiisalong 4y agoTurning all UB into IB would be better so a compiler can provide --overflow=crash or --overflow=wrap.
- olliej 4y agoIB?
- flohofwoe 4y agoImplementation defined behaviour.
- matthews2 4y agoImplementation defined behaviour
- olliej 4y agoAh yeah, "unspecified behaviour" is my known phrase, but I can see it would be confusing to be arguing about whether something should be UB or UB :D
- planede 4y agoUnspecified behavior and implementation defined behavior are distinct in theory. The latter requires the behavior to be documented by the implementer, while the former does not. In practice I dare you to find compiler documentation for each implementation defined behavior. https://timsong-cpp.github.io/cppwp/n4868/intro.defs#defns.unspecified https://timsong-cpp.github.io/cppwp/n4868/intro.defs#defns.u... https://timsong-cpp.github.io/cppwp/n4868/intro.defs#defns.impl.defined https://timsong-cpp.github.io/cppwp/n4868/intro.defs#defns.i... edit: C wording: https://cigix.me/c17#J.1 https://cigix.me/c17#J.1 https://cigix.me/c17#J.3 https://cigix.me/c17#J.3
- gpderetta 4y ago> In practice I dare you to find compiler documentation for each implementation defined behavior. For gcc there is this: https://gcc.gnu.org/onlinedocs/gcc/C-Implementation.html https://gcc.gnu.org/onlinedocs/gcc/C-Implementation.html No idea how complete it is. Also a lot of stuff is architecture/platform specific, not compiler specific, so you won't find it in the general compiler docs but you have to look at the psABI.
- avgcorrection 4y agoCompilers optimizing based on UB: normal Acting in a crash-predictable way on UB: abuse A strange world this be.