News for hackers. Stuff that matters.
Re:A Usable C++ Dialect That Is Safe Against Memory Corruption by EpicEng on Thursday March 15, 2018 @02:59PM (#16595592)
Perhaps someone better versed in those compilers can add to/correct me here, but I'm pretty sure that can only happen if you're invoking UB somewhere along the line.
Re:A Usable C++ Dialect That Is Safe Against Memory Corruption by saagarjha on Thursday March 15, 2018 @03:08PM (#16595663)
Once you've entered the realm of undefined behavior, the compiler can really do whatever it likes. Before then all it can do is assume you're not doing anything undefined.
Re:A Usable C++ Dialect That Is Safe Against Memory Corruption by masklinn on Friday March 16, 2018 @08:10AM (#16600067)
> Once you've entered the realm of undefined behavior, the compiler can really do whatever it likes. Before then all it can do is assume you're not doing anything undefined.

In practice there is very little difference and you're effectively entering the realm of UB as soon as the program starts, because through inlining and propagation a possible UB may be leveraged before the UB is sequentially hit.

Re:A Usable C++ Dialect That Is Safe Against Memory Corruption by kolpa on Thursday March 15, 2018 @03:43PM (#16595962)
That's exactly the problem. C/C++ has undefined behavior as part of the language spec, so it can never be safe unless you use a compiler that promises to reject programs that invoke undefined behavior.
Re:A Usable C++ Dialect That Is Safe Against Memory Corruption by staticassertion on Thursday March 15, 2018 @04:09PM (#16596145)
It's more complicated than this. C++ compilers can't just "reject" UB code - they don't have enough information at all times to prove that code invokes UB, the languages is not powerful enough to represent this in all cases.

When people say "just fix this in compilers, don't compile when they run into UB" it's a fundamental misunderstanding of the problem. Yes, in some cases the compiler can prove UB, and uses it to write optimiations, but it doesn't prove that all code is not invoking UB.

Re:A Usable C++ Dialect That Is Safe Against Memory Corruption by gpderetta on Thursday March 15, 2018 @06:03PM (#16596879)
> a fundamental misunderstanding of the problem

No kidding! There is one right here:

> Yes, in some cases the compiler can prove UB, and uses it to write optimiation

Compilers do not prove UB , on the contrary they postulate no-UB and then use this axiom to prove other properties of the code.

Re:A Usable C++ Dialect That Is Safe Against Memory Corruption by staticassertion on Thursday March 15, 2018 @07:02PM (#16597244)
I suppose that's true, yes. My point was more that your compiler is not hitting a line of code and saying "I know this is or is not UB" because it can not do that in the general case - it is definitely incorrect to say it is 'proving' this. Would you consider that an accurate representation?
Re:A Usable C++ Dialect That Is Safe Against Memory Corruption by masklinn on Friday March 16, 2018 @08:14AM (#16600079)
> My point was more that your compiler is not hitting a line of code and saying "I know this is or is not UB" because it can not do that in the general case

I think it's still a (cause of) misunderstanding. For the compiler, UBs are situations which axiomatically can not occur. "This is UB" is not a concept, because the compiler assumes at all point that UBs can not occur.

The compiler doesn't go "oh you're dereferencing a pointer which may be null, fuck you", it goes "you're dereferencing a pointer so it can't be null, and thus I can remove anything assuming possible nullability".

Re:A Usable C++ Dialect That Is Safe Against Memory Corruption by staticassertion on Friday March 16, 2018 @12:25PM (#16601699)
> The compiler doesn't go "oh you're dereferencing a pointer which may be null, fuck you", it goes "you're dereferencing a pointer so it can't be null, and thus I can remove anything assuming possible nullability".

:) This is what I'm trying to say.

Re:A Usable C++ Dialect That Is Safe Against Memory Corruption by kllrnohj on Thursday March 15, 2018 @05:21PM (#16596626)
int get_value(int* pointer) { return *pointer; }

How is a compiler supposed to reject this for undefined behavior? It'd be absurd to demand the compiler someone knows all possible usages for get_value to find if any of them pass a nullptr, so what is it supposed to do?

And this is why the spec says things like that deref'ing a null pointer is undefined behavior, so that the compiler can take that function and do the thing you'd expect it to do and transform it into a simple memory read.

Re:A Usable C++ Dialect That Is Safe Against Memory Corruption by fjsolwmv on Thursday March 15, 2018 @06:43PM (#16597111)
The compiler shouldn't rejected UB, it should define all behavior, and reject all code constructs that can't have behavior fully defined (like accessing raw pointers as arrays, so bounds- checking is impossible). It can throw an exception on null de-reference at runtime. That's slow, but safe.
Re:A Usable C++ Dialect That Is Safe Against Memory Corruption by adrianN on Friday March 16, 2018 @01:47AM (#16598893)
Slow but safe is not why people choose C++ for their software.
Re:A Usable C++ Dialect That Is Safe Against Memory Corruption by wruza on Friday March 16, 2018 @05:14AM (#16599468)
There is a compiler option for that, just run javac alias instead of g++