4 ms·
I had a go at this problem, backwards-compatible safe C, back in 2012.[1] It was discussed on the C standards mailing list, and the consensus was that it would
by Animats 10y ago
I had a go at this problem, backwards-compatible safe C, back in 2012.[1] It was discussed on the C standards mailing list, and the consensus was that it would work technically, but was politically infeasible. What I proposed was not too far from "Checked C", but the syntax was different.
I'd defined a "strict mode" for C. The first step was to add C++ references to C. The second step was to make arrays first-class objects, instead of reducing them to pointers on every function call. When passing arrays around, you'd usually use references to arrays, which wouldn't lose the size information as reduction to a pointer does.
Array size information for array function parameters was required, but could be computed from other parameters. For example, UNIX/Linux "read" is usually defined as
int read(int fd, char buf[], size_t len);
The safe version would be
int read(int fd, &char buf[len], size_t len);
In both cases, all that's passed at run time is a pointer, but the compiler now knows that the size of the array is "len" and has something to check it against. The check can be made at both call and entry, and in many cases, can be optimized out. In general, in all the places in C where you'd describe an array with a pointer with empty brackets, as with "buf[]", you'd now have to put in a size expression.
You could do pointer arithmetic, but only if the pointer had been initialized from an array, and was attached to that array for the life of the pointer.
char s[100];
char* p = s;
...
char ch = *p++;
Because p is associated only with s, the compiler knows what to check it against.
There was more, but that's the general idea. A key point was that the run-time representation didn't change; there were no "fat pointers". Thus, you could intermix strict and non-strict compilation units, and gradually convert a working program to strict mode.
This took less new syntax and fewer new keywords than "Checked C". I was trying to keep C style, adding just enough that you could talk about arrays properly in C.
[1] http://www.animats.com/papers/languages/safearraysforc43.pdf http://www.animats.com/papers/languages/safearraysforc43.pdf
- peterwwillis 10y agoPerl introduced a "strict" mode a long time ago. Although it doesn't force it on you, virtually all modern Perl programmers use it by default. As an extra security feature Perl also supports "taint" mode, which "taints" all user-supplied data as potentially hazardous to the program. This was used more for CGI, but it's still a simple and powerful security feature. Unfortunately, it was never adopted like 'use strict' was because it would more often get in the way of the programmer.
- Animats 10y ago"strict mode" is an assistance to migration. The fundamental memory safety problem in C is that arrays are not first-class objects. They degrade to pointers when passed around. Yet any function which references an array has to know how big it is. Somehow. The language has no syntax for this. That's the big problem, and what I was addressing.
- peterwwillis 10y agoI wasn't arguing your point. I'm saying it's been proven that some new optional functionality can be adopted slowly and eventually become de-facto standard. (And that it might not get adopted if it gets in a programmer's way)
- vasilipupkin 10y agoI've already been downvoted for this question, but I don't get the business case here. If you are worried about this, why not write code in modern C++ which has plenty of ways to make all sorts of issues like this safe ? Why stick to C at all?
- Animats 10y agoC++ doesn't solve the problem. C++ papers over the problem with templates. You can still get raw pointers out of C++ objects, and for most APIs, you have to. The underlying language objects are still unsafe.
- vasilipupkin 10y agook, so write C++ classes that don't let you access raw pointers. I mean I understand that C/C++ languages are less safe than, say, Java - but that is the whole point. The lesser safety gives you better performance. If you don't care about bleeding edge performance, you should probably choose Java or Python or some other language that is much more flexible and easier to use
- Animats 10y agoThe lesser safety gives you better performance. No. That excuse has been bandied about for years, but it's wrong, as we're now seeing as Rust gets better. C's problems with arrays come from lack of expressive power in the language. C doesn't even let you talk about array size in parameters. Many of C's painful design decisions come from trying to cram a compiler into a PDP-11 with 128KB (not MB) of memory for a process. Global analysis on a machine that small while retaining reasonable compile times was hopeless. This is no longer a limitation.
- Gibbon1 10y agoI think the thing is that the problems people are still using C to solve for are a different than the ones where people are using C++, Java, C#. Meaning if C++ was a better option then people would have switched to using C++ two decades ago. Yet C has historical deficiencies that could be fixed by smallish mostly painless extensions to the language.