News for hackers. Stuff that matters.
Re:Null Pointer Dereferencing Causes Undefined Behavior by kragen on Monday April 20, 2015 @08:10PM (#9411602)
This is ridiculous. The C standard was apparently written in order to allow compilers to introduce subtle security holes into previously-working code, all in the name of a fraction of a percent of performance increase. It’s totally irrational, especially in an epoch where C is no longer the language you’d use to get maximum bit-twiddling performance anyway — because it won’t run on your GPU!

See http://blog.regehr.org/archives/761 for a sarcastic take on the problem, http://blog.regehr.org/archives/880 for a more serious description of the problem, and http://blog.regehr.org/archives/1180 for the "Proposal for a Friendly Dialect of C" that proposes to actually solve it.

Re:Null Pointer Dereferencing Causes Undefined Behavior by lmm on Tuesday April 21, 2015 @05:15AM (#9412947)
> The C standard was apparently written in order to allow compilers to introduce subtle security holes into previously-working code, all in the name of a fraction of a percent of performance increase.

Yes it was. Or at least, that's the constituency C currently has - and understandably C continues to serve it. There needs to be a language for that kind of people, and that language is C.

If you don't want that, stop using C. It's not hard.

Re:Null Pointer Dereferencing Causes Undefined Behavior by kragen on Tuesday April 21, 2015 @12:04PM (#9414979)
Your suggestion is ridiculous. First, it wouldn’t be enough to stop using C; I would also have to stop using software written in C. And C++. Second, there aren’t any reasonable alternatives to C — it’s not the only portable low-level language, but it’s the only popular one, other than C++, which shares the same problems.
Re:Null Pointer Dereferencing Causes Undefined Behavior by mpweiher on Tuesday April 21, 2015 @09:07AM (#9413736)
I've been using C since before there even was a standard. Why should I ditch the language because <deleted> have hijacked the language spec?
Re:Null Pointer Dereferencing Causes Undefined Behavior by lmm on Tuesday April 21, 2015 @09:52AM (#9413973)
Because they're the ones who the language is valuable to. You have plenty of other options; they don't.
Re:Null Pointer Dereferencing Causes Undefined Behavior by mpweiher on Tuesday April 21, 2015 @02:47PM (#9416109)
For the sorts of semantics they are looking for, FORTRAN seems to be a good option. Or keep the crazy in C++ land?

What are my options if I want a simple, easy to understand language that is close to the execution model of current machines?

Re:Null Pointer Dereferencing Causes Undefined Behavior by makomk on Tuesday April 21, 2015 @01:37PM (#9415670)
No, it wasn't written for that purpose at all. Many of these undefined behaviours exist in the spec because long ago, there was some exotic architecture where doing that thing caused a hardware exception or behaved strangely. For instance, on some systems that are no longer in use, trying to obtain the pointer in this example would cause an access violation and abort the program because pointer calculations are checked in hardware. It was never intended as an excuse for compiler writers to randomly decide to be dicks.
Re:Null Pointer Dereferencing Causes Undefined Behavior by kragen on Tuesday April 21, 2015 @04:03PM (#9416655)
There’s that, and then there’s also long-ago exotic architectures where you weren’t processing untrusted data, you really did care about improving your performance by ½%, and it really was worth the massive amount of programmer time needed to debug the resulting problems. There’s a famous apocryphal quote that writing C for the Cray (?) compiler was like “writing to [the programmer’s] ex-wife’s lawyer”.

Also, there are still Unisys Clearpath/MCP systems in use.

Re:Null Pointer Dereferencing Causes Undefined Behavior by CHY872 on Monday April 20, 2015 @08:43PM (#9411715)
You've absolutely misrepresented the problem, presumably for effect. It's a far more subtle problem; the question is whether compiler writers should not only make their analyses and transformations be safe in the presence of valid code, but also safe in the presence of invalid code.

This is ironically mainly a problem because older compilers acted normally on some undefined code. So more modern optimising compilers will expect defined behaviour and so forth, leading to new bugs reported in old code. These bugs were always present, they just appear only now.

It's not the compiler trying to exploit the undefined behaviour deliberately; it's that the compiler is expecting the behaviour to be defined and behaving accordingly. So if you have

    int x = *foo;
    if (foo == NULL) {
        ERROR();
    }
then clearly in valid C, the if statement will never be taken, so it's safe to remove it. This is something you'd want to happen in most cases, it just so happens that in the above case you might not want it to.

In terms of whether the fraction of a percent is worth it, well the people who do most of the development of the compilers is large companies like Google, and they don't do it out of the goodness of their hearts, they do it because the benefit of having high performance compilers is worth the expenditure.

In terms of whether other languages are better, of course they are for many tasks, but C is king of the embedded world, where implementing a full Rust/Java/Node/language of day compiler is too much work. Then for many high performance tasks it's very difficult to beat the sheer performance of C or C++. If a task written in C or C++ runs just 20% faster than Java for Google/Microsoft/etc's 40,000 computer cluster, then that's 7000 computers that you don't need to buy.

For the bit about GPUs, well that's kinda an aside but GPUs are useful for some things and not others and appealing to their use suggests ignorance. People use C for lots of things, and 'high performance bit-twiddling' isn't a standard one. GPUs are terrible at most tasks.

The proposal for a friendly dialect of C is useful, but I suspect that the smarter way of doing it is the [0] undefined behaviour sanitiser, which produces an instrumented binary that errors when it reaches undefined behaviour.

The developer, upon finding a new bug in their code, compiles, runs, gets an error, fixes, rinses, repeats.

[0] - https://developerblog.redhat.com/2014/10/16/gcc-undefined-be...

Re:Null Pointer Dereferencing Causes Undefined Behavior by kragen on Tuesday April 21, 2015 @12:43PM (#9415274)
Your accusations of dishonesty are completely unfounded, and merely serve to demonstrate that you haven’t bothered to read the links I provided, and therefore have a very limited grasp of the situation. Please retract them.

You are framing the problem in terms that make the situation sound almost reasonable: given that this code is invalid, what should the compiler do? In fact, the question I asked is whether particular code should be invalid.

The choices made by the standards committees so far on this question — the one you are trying to divert attention from — have proven catastrophic. This is far from being a “misrepresentation”; it is patently obvious. We have reached a situation where every non-trivial execution of GCC (which is written in C, remember) executes undefined behavior; and where even zlib, which has gone nine years without a security hole, is full of undefined behavior.

Re:Null Pointer Dereferencing Causes Undefined Behavior by mpweiher on Tuesday April 21, 2015 @09:00AM (#9413699)
> This is ironically mainly a problem because older compilers acted normally on some undefined code.

As they should. C and C code predates all of the standards, so ALL that C code was, at the time, "undefined". There were still working compilers and working code, how could that possibly be??

Re:Null Pointer Dereferencing Causes Undefined Behavior by CHY872 on Tuesday April 21, 2015 @11:23AM (#9414635)
I was referring to compilers in early 2000s vs compilers now.