News for hackers. Stuff that matters.
Re:How to C in 2016 by hyc_symas on Saturday January 09, 2016 @02:13AM (#10870413)
"Don't write C unless you need extreme performance,

This is the wrong mindset and has been the guiding principle behind over a generation of poor programming discipline.

You might be able to rationalize that you don't always need extreme performance, but it's not about performance, it's about efficiency. Efficiency always matters. We live on a small planet with finite resources. Today, the majority of computing is happening with handheld portable devices with tiny batteries. Every wasted byte and CPU cycle costs power, runtime, and usability.

Even if you're coding for a desktop PC or a server application - every bit of waste costs electricity, generates heat, increases cooling load. Every bit of waste costs the user real money, even if they don't see the time that you wasted for them.

Performance is a side-effect. The only thing that matters is efficiency, and it matters all the time.

Most of today's programming problems come down to laziness and fear of "premature optimization" (which is obviously bunk). It is possible to write perfect C code that runs correctly the first time and every time. You do that by planning ahead. Formal specs are part of this.

The same consideration applies to writing, in general. I witnessed this first hand, when I worked at the computer labs at the University of Michigan back in the 1980s. When all anyone had was an electric typewriter, they planned their papers out well in advance - detailed outlines, with logically designed introductions and conclusions. This was a requirement, because once you got into actually typing, correcting a mistake could be expensive or impossible without starting over from scratch.

When we only had Wordstar available, and users came in with the inevitable questions and requests for help, you would still see well-organized papers written with a clear logical flow.

In 1985 with the advent of the Macintosh and MacWrite, all of that changed: People discovered that they could edit at will, copy and paste and move text around on the fly. And so they did - throwing planning out the window. And the quality of papers we saw at the help desk plummeted. Surveys from those years confirmed it too - paradoxically, the writing scores in the better-funded departments dropped rapidly, commensurate with their rate of introduction of Macs.

Planning ahead, making sure you know what you need to write, before you start writing any actual lines of code, prevents most problems from ever happening. It's a pretty simple discipline, but one that's lost on most people today.

Re:How to C in 2016 by pcwalton on Saturday January 09, 2016 @04:21AM (#10870621)
> Most of today's programming problems come down to laziness and fear of "premature optimization" (which is obviously bunk). It is possible to write perfect C code that runs correctly the first time and every time. You do that by planning ahead. Formal specs are part of this.

If "planning ahead" were enough to prevent security vulnerabilities in practice, someone would have done it by now. Instead, we've had C for upwards of 30 years and everyone, "bad programmers", "good programmers" and "10xers" alike, keeps introducing the same vulnerabilities.

It's a nice theory, but we've been trying for over 30 years to make it work, and it keeps failing again and again. I think it's time to admit that C as a secure language (modulo the extremely expensive formal coding practices used in avionics and whatnot) has failed.

Re:How to C in 2016 by hyc_symas on Saturday January 09, 2016 @01:13PM (#10872197)
Sorry, but your statement makes no sense. That's like saying "we've been trying for over 5000 years to make hammers work, and people keep smashing their thumbs and fingers again and again. I think it's time to admit that hammers as a safe tool have failed."

Security is not a property of the tool, it is a property of the tool's wielder.

Re:How to C in 2016 by pcwalton on Saturday January 09, 2016 @01:45PM (#10872336)
> Security is not a property of the tool, it is a property of the tool's wielder.

How many memory safety bugs do you see in Java applications compared to C applications?

Re:How to C in 2016 by hyc_symas on Saturday January 09, 2016 @02:07PM (#10872419)
If Java memory safety is so good, why does NullPointerException still exist in the language?
Re:How to C in 2016 by pcwalton on Saturday January 09, 2016 @02:47PM (#10872562)
I'm not talking about NPE (though I would be happy to elaborate on how not having null pointers also reduces crashes in the wild). I'm talking about exploitable and/or non-type-safe memory safety problems.

Writing in Java is an effective way to reduce remote code execution vulnerabilities over writing in C. Statistics have shown this over and over again.

Re:How to C in 2016 by hyc_symas on Saturday January 09, 2016 @03:31PM (#10872753)
The very fact that NPE exists, in a language which doesn't have pointers, running on a JVM that doesn't have pointers, is evidence that the entire model of "doesn't have pointers" is a sham. The reality of computer architecture is that data resides in memory, memory is an array of bytes, and bytes have addresses (i.e., pointers) and denying this fact is impossible. Even in a pure pointerless system like Java. And if there are pointers in the system, somewhere, anywhere, they are exploitable.

Remote code execution vulnerabilities in C are less a feature of the language syntax and mostly due to the abysmally poor C library, which is regrettably part of "C The Language" - but no one forces you to use it - there are better libraries. Also, it's an artifact of implementation - just as Java's supposed immunity is an artifact of implementation, not an inherent feature of the language syntax.

Case in point http://www.trendmicro.com/vinfo/us/threat-encyclopedia/vulne...

As for RCEs in C code - these would be easily avoided if the implementation used two separate stacks - one for function parameters, and one for return addresses. If user code can only overwrite passive data, stack smashing attacks would be totally ineffective. Again - there's nothing in the C language specification that dictates or defines this particular vulnerability. It is solely an internal implementation decision.

Re:How to C in 2016 by pcwalton on Saturday January 09, 2016 @06:24PM (#10873291)
> As for RCEs in C code - these would be easily avoided if the implementation used two separate stacks - one for function parameters, and one for return addresses.

No. Increasingly, RCEs are due to vtable confusion resulting from use after free.

Re:How to C in 2016 by hyc_symas on Sunday January 10, 2016 @03:12AM (#10874644)
Sounds like you're the one with confusion; vtables are not a feature of C. C++, sure, but that's a different conversation. Your point is invalid.
Re:How to C in 2016 by pjmlp on Saturday January 09, 2016 @05:03PM (#10873078)
References are pointers.
Re:How to C in 2016 by oldmanjay on Saturday January 09, 2016 @12:11PM (#10871959)
The trouble here is that the efficiency you hold up so highly is marginal in most cases. Making the computer do work to make human lives easier isn't a waste of resources, it's the only reason computers exist in the first place.
Re:How to C in 2016 by hyc_symas on Saturday January 09, 2016 @01:05PM (#10872175)
Totally agree with your latter statement. But the overall assertion still leads people down misguided paths. Modern OSs like MacOS, Windows, and desktop Linux are presumably intended to make using computers easier. Each new release adds reams of new code implementing new features intended to improve ease of use. Yet the net effect is often negative, because the sheer amount of code added makes the overall system significantly slower.

Meanwhile, new programming languages abound, presumably to make programmers' lives easier. But these developments often completely lose sight of the users. Python is easier to write, but a program written in python uses 1000x the CPU and memory resources of a program written in C. It does the job, but slowly, and consuming so much that the computer is unable to do anything else productive. Meanwhile, it's questionable whether programmers' lives have been improved either, because they spend too much time chasing the next new tool instead of growing expertise in just one.

As for marginal efficiency gains - that is only a symptom of the latter developer problem. E.g., LMDB is pure C, less than 8KLOCs, and is orders of magnitude faster than every other embedded database engine out there. It is also 100% crash-proof and offers a plethora of features that other DB engines lack. All while being able to execute entirely within a CPU's L1 cache. The difference is more than "marginal" - and that difference comes from years of practice with C. You will never gain that kind of experience by being a dilettante and messing with the flavor of the week.

Re:How to C in 2016 by pjmlp on Saturday January 09, 2016 @05:05PM (#10873083)
Python is a scripting language, it was never meant to write full blown applications.