News for hackers. Stuff that matters.
Re:Why Not Just Do Simple C++ RAII in C? by grumpyprole on Wednesday May 22, 2024 @04:11PM (#40445788)
I'm sorry I still don't buy it. Can you please show me a use case where ignoring null pointer or overflow checks makes your product non-viable or uncompetitive?

Some of these checks could be removed by languages with better compilers and likely more restrictions. That is the better approach. As a user, I don't want to run code that is potentially unsafe and/or insecure.

Re:Why Not Just Do Simple C++ RAII in C? by fooker on Wednesday May 22, 2024 @04:38PM (#40446192)
It's not ignoring about ignoring null pointer or overflow checks, it's having to necessarily insert these checks everywhere.
Re:Why Not Just Do Simple C++ RAII in C? by grumpyprole on Wednesday May 22, 2024 @04:48PM (#40446341)
We should build compilers that insert these checks for us (if they cannot statically determine them unnecessary). The ability to omit these checks doesn't IMHO justify undefined behaviour.
Re:Why Not Just Do Simple C++ RAII in C? by fooker on Wednesday May 22, 2024 @04:50PM (#40446360)
Well, good news is that you have optional modes in most compilers that do this.

You would not want to force these by default, nobody wants it. You can not statically determine them unnecessary in for the vast majority of code, even stuff as simple as `print(read(a) + read(b))`.

Re:Why Not Just Do Simple C++ RAII in C? by daemin on Thursday May 23, 2024 @12:09PM (#40456510)
So the simplest case for not providing a language specification for dereferencing a null pointer is that it requires putting in checks everywhere to detect the condition and then do something in the case where the pointer is null. So what should the null pointer case do then? Something like emit an exception, or send a signal, or call std::terminate to exit the process?

I know that languages like Java have a NullPointerException which they can throw and handle for situations like this, but they're also built on a highly specified virtual machine architecture that is consistent across hardware platforms. This also does not guarantee that your program is safe from crashing when this exception gets thrown, as you have to handle it somewhere. For something as general as this it will probably be in the Main function, so you might as well let it go unhandled as there's not that much you can do at that point.

For a language like C++ it is simpler, easier, and I would argue more correct, to just let the hardware handle the situation, which in this case would trigger a memory error of trying to access invalid memory. As the real issue is probably somewhere else in the code which isn't being handled correctly and the bad data is flowing through to the place where it accesses the null pointer and the program crashes.

To add to that in a lot of cases the program isn't crashing while trying to access address 0, it's crashing trying to access address 200, or 1000, or something like that, and putting in simplistic checks isn't going to catch those. You could argue that the check should guard against accessing the lowest 1k of memory, but then when do you stop, at 64k? Then you have an issue with programs that must fit within 1k of memory.

Leaving it unspecified is the better choice.