News for hackers. Stuff that matters.
Re:Undefined behavior is often a thing for C programmers (2011) by fortran77 on Thursday September 03, 2020 @09:55AM (#24364060)
And yet, they managed to write the Linux kernel in it, as well as thousands of other things we use every day.
Re:Undefined behavior is often a thing for C programmers (2011) by staticassertion on Thursday September 03, 2020 @10:02AM (#24364128)
I'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.
Re:Undefined behavior is often a thing for C programmers (2011) by barrkel on Thursday September 03, 2020 @10:32AM (#24364450)
One 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.

Re:Undefined behavior is often a thing for C programmers (2011) by sharpneli on Thursday September 03, 2020 @10:40AM (#24364508)
Making 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.

Re:Undefined behavior is often a thing for C programmers (2011) by steerablesafe on Thursday September 03, 2020 @10:44AM (#24364533)
> IIRC C++ just decided that signed numbers are two's complement and that's that.

Signed integer overflow is still undefined in C++.

Re:Undefined behavior is often a thing for C programmers (2011) by sharpneli on Thursday September 03, 2020 @12:05PM (#24365451)
You are correct. I misremembered, it was just a proposal that was not taken into the spec.
Re:Undefined behavior is often a thing for C programmers (2011) by steerablesafe on Thursday September 03, 2020 @12:30PM (#24365723)
No, it's taken into the spec. It affects unsigned <-> signed conversions and maybe other things. Not integer overflow though.
Re:Undefined behavior is often a thing for C programmers (2011) by sharpneli on Thursday September 03, 2020 @04:43PM (#24368673)
Ah. 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.
Re:Undefined behavior is often a thing for C programmers (2011) by geofft on Thursday September 03, 2020 @10:09AM (#24364193)
"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/

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?"

Re:Undefined behavior is often a thing for C programmers (2011) by tinus_hn on Thursday September 03, 2020 @09:58AM (#24364093)
Obviously the most dangerous form of undefined behavior is not that which always crashes but that which for some unlikely inputs cases crashes.
Re:Undefined behavior is often a thing for C programmers (2011) by me_me_me on Thursday September 03, 2020 @10:04AM (#24364151)
I 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.

Re:Undefined behavior is often a thing for C programmers (2011) by cortesoft on Thursday September 03, 2020 @11:12AM (#24364799)
I am pretty sure your hand going in fire is pretty defined behavior
Re:Undefined behavior is often a thing for C programmers (2011) by me_me_me on Thursday September 03, 2020 @12:28PM (#24365695)
Is it though, for yourself/your body. Is putting hand into a fire something you experienced and internalized?
Re:Undefined behavior is often a thing for C programmers (2011) by perl4ever on Thursday September 03, 2020 @06:38PM (#24369669)
Of 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.
Re:Undefined behavior is often a thing for C programmers (2011) by me_me_me on Friday September 04, 2020 @05:38AM (#24373351)
We 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.

Re:Undefined behavior is often a thing for C programmers (2011) by cortesoft on Thursday September 03, 2020 @04:34PM (#24368571)
No, but it is part of the spec