News for hackers. Stuff that matters.
Re:The C23 edition of Modern C is now available for free by uecker on Tuesday October 15, 2024 @03:43PM (#41852363)
UB is does not automatically make things unsafe. You can have a compiler that implements safe defaults for most UB, and then it is not unsafe.
Re:The C23 edition of Modern C is now available for free by ahoka on Tuesday October 15, 2024 @04:03PM (#41852548)
By definition UB cannot be safe.
Re:The C23 edition of Modern C is now available for free by marssaxman on Tuesday October 15, 2024 @05:11PM (#41853174)
this depends on your choice of definition for "safe"
Re:The C23 edition of Modern C is now available for free by Maxatar on Tuesday October 15, 2024 @09:58PM (#41854910)
The definition given by the C standard allows for safe undefined behavior.
Re:The C23 edition of Modern C is now available for free by umanwizard on Wednesday October 16, 2024 @09:21AM (#41858758)
Something can be UB according to the standard, but defined (and safe) according to a particular implementation. Lots of stuff is UB according to the C or C++ standard but does something sensible in gcc and/or clang.
Re:The C23 edition of Modern C is now available for free by duped on Tuesday October 15, 2024 @04:54PM (#41853004)
That's implementation defined behavior, not undefined behavior. Undefined behavior explicitly refers to something the compiler does not provide a definition for, including "safe defaults."
Re:The C23 edition of Modern C is now available for free by fuhsnn on Tuesday October 15, 2024 @05:11PM (#41853169)
Compilers are not prohibited to provide their own definition for UB, that's how UBsan exists.
Re:The C23 edition of Modern C is now available for free by Maxatar on Tuesday October 15, 2024 @08:52PM (#41854616)
The C standard says, and I quote:

>Possible undefined behavior ranges from ignoring the situation completely with unpredictable results ... or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message)

So a compiler is absolutely welcome to make undefined behavior safe. In fact every compiler I know of, such as GCC, clang, MSVC has flags to make various undefined behavior safe, such as signed integer overflow, type punning, casting function pointers to void pointers.

The Linux kernel is notorious for leveraging undefined behavior in C for which GCC guarantees specific and well defined behavior.

It looks like there is also the notion of unspecified behavior, which gives compilers a choice about the behavior and does not require compilers to document that choice or even choose consistently.

And finally there is what you bring up, which is implementation defined behavior which is defined as a subset of unspecified behavior in which compilers must document the choice.

Re:The C23 edition of Modern C is now available for free by renox on Tuesday October 15, 2024 @06:24PM (#41853762)
Well Zig has ReleaseSafe for this.. ReleaseFast is for using these UBs to generate the fastest code.