11 ms·
Checked C: extension to C that adds static and dynamic checking
- peterevans 9y agoI highly recommend the Static Analyzer that clang already uses. I use this frequently in the C project I am currently working on, and it's very good at spotting memory leaks and other potential issues. I've had more than one occasion where I've looked at an issue it's reported and said, nah, this is a false positive...oh, no, wait.
- fwsgonzo 9y agoIt's apparently a Microsoft research project: https://www.microsoft.com/en-us/research/project/checked-c/?from=http%3A%2F%2Fresearch.microsoft.com%2Fen-us%2Fprojects%2Fcheckedc%2Fdefault.aspx https://www.microsoft.com/en-us/research/project/checked-c/?... Very interesting to add additional compiler support to am existing language, in a sense. Sanitizers and static analyzers have made C/C++ much more bearable, and this adds to the pile!
- epx 9y agoOne thing that C should have is an automatic string type (more like a byte[] than a String) that knows its own size. A huge amount of programming bugs arise from the lack of strings, and I can't see the reason why they could not be supported even in small systems. Delphi/Object Pascal had something like 3 or 5 function pointers that support the string type (create, free, resize, things like that).
- rs86 9y agoArrays are actually pointers to the first element... They get translated to pointer arithmetic: adding an offset to the first element. You can easily create a struct with one field for the size and another with the pointer to the first character
- kqr 9y agoSemantically, arrays are different from pointers. They do, however, degrade into pointers in many situations, which causes that misunderstanding.
- petecox 9y agoA language with an array type, e.g. Java, distinguishes between a 'null' array reference and an array of zero length. which could substitute in languages that don't have an option type; SOME([]) vs NONE But a plain *char in C makes no such distinction.
- charliesome 9y agoThere is a difference. An empty string in C is a non-null char* pointer pointing to a zero byte. Passing a null pointer to strlen will crash.
- esrauch 9y agoA zero-length array can't ever be a valid cstring at all since they have to be null terminated. I don't think that is actually relevant to the claim you are replying to, which is about arrays and pointers and not strings.
- admax88q 9y agoThe funny thing is that the memory allocator typically knows the size of the string. You call free(foo) not free(foo, 12) or free(foo, strlen(foo)); But there's no standard way to ask for this size. Why doesn't strcpy just check with the allocator about how much memory is available and refuse to write past that.
- rootbear 9y agoNot every string is allocated from the heap. There is no way to know if a string passed to a function, for example, came from the heap or not.
- admax88q 9y agoThat's true, although there's no reason the stack allocator couldn't record this information somewhere as well. And if the memory allocator doesn't know about the string then you could just revert to the existing behaviour so you're no worse off.
- chrisseaton 9y agoThere's more ways to allocate memory than the heap and stack.
- stochastic_monk 9y agoWhat are you referring to? sbrk? mmap? I typically use either the heap or the stack myself.
- timClicks 9y agoStatic memory is at the bottom of the address space, below the heap. But that would really only be relevant in the case of string literals unless I'm mistaken.
- 9y ago
- peterevans 9y agoThe main issue is that there's already a lot of standard library support for classical strings--things that are just a pointer to a chunk of bytes, and no more; rewriting those functions would be destructive. The C standards group could add to the standard library, I suppose, but generally they are a bit reticent to do so. It is not the case that C has a benevolent dictator with a particular vision and drive for the language. It's more like a small group of people who don't want to mess things up. And so not much changes. This is neither good nor bad to me--it simply is; and anyway, these days, you have quite a few options that do offer automatic strings that can also compile into machine code (Go, Rust I suppose?).
- Gibbon1 9y ago> rewriting those functions would be destructive Getting rid of standard string functions would be the best thing to happen to the c language in the last 25 years. There are two types of c programs. Those that scrupulously avoid standard string functions and brittle programs shot through with security holes.
- kbenson 9y agoAt the point when you have to convert between string types and can no longer easily use much of libc without conversions, why not just use a different language that's already gone much farther in the name of safety, usability or other features? In other words, if your pseudo-c candidate already suffers from most the interop problems that Rust, D, Nim and Go do, why not just use one of those and at least reap the other benefits they provide?
- watersb 9y agoAnything to help legacy C code base is welcome. Hard part is knowing whether touching decades-old, tested code is worth the risk.
- peterevans 9y agoI mean, I guess it's the best thing if you want to wreak havoc and chaos? Which, perhaps, you do! Your suggested dichotomy is, of course, a little bit false. But I'm sure you knew that when you wrote it down. It's entirely possible to write secure programs in C, even with standard functions. Writing your own code does not somehow confer a level of security-consciousness that you lacked when sticking to strings.h. (It does give you a wonderful opportunity to write your own security holes that no one has discovered yet!) I mentioned this somewhere else, but we're in a pretty good place right now with languages; we finally have really solid alternatives to C that can compile to machine code, in both Go and Rust.
- userbinator 9y agoI've used something similar --- a null-terminated string, with a length prefix at a negative offset. Resizeable ones have an "allocated size" field before that one. This makes for strings that are both easy to manipulate and compatible with existing functions that only read a regular C string.
- zzzcpan 9y agoBasically this: https://github.com/antirez/sds https://github.com/antirez/sds
- userbinator 9y agoGood to see I'm not the only one who thought of that idea, although my implementation has a different order of the fields (and IMHO better, since it allows for statically allocated strings with only a length field --- a sort of subclassing.)
- timClicks 9y agoSo in a sense implementing Pascal strings in C?
- flukus 9y agoYes. But also "abusing" C's permissiveness with pointers to get pascal strings that interop with standard functions. It get's passed around as a pointer to a char array but the length function would return the value at that pointer - sizeof(size_T). In most languages this wouldn't be possible.
- nikanj 9y agoSo, BSTRs? They’re common in old-timey COM apis
- watersb 9y agoBSTR size field is at positive offset, IIRC.
- munin 9y agoWe have tried to do this by adding a "null terminated string" type. Start in section 2.4 of the specification (https://github.com/Microsoft/checkedc/releases/download/v0.7-final/checkedc-v0.7.pdf https://github.com/Microsoft/checkedc/releases/download/v0.7...).
- _sdegutis 9y agoThe point is to keep C small and flexible. It supports whatever kind of string implementation needed by the present environment, platform and algorithm. That said, several “high-level” string libraries for C exist that make it safer, more efficient, etc. Some of them are even pretty clever, allocating extra space right before the C pointer to a null terminated C string that it gives you, so it can do its own bookkeeping.
- isaachier 9y agoI think Fortran uses a number followed by a byte array to represent strings. This is the big argument with C which used null terminators. In retrospect, maybe Fortran was right after all. Edit: Actually seems I confused Fortran and Pascal. Also, Fortran uses an internal length variable.
- Gibbon1 9y agoI think the failure of C to support a built in array type is exactly like Steve Jobs refusing to give up one button mice. The reasons aren't technical or based on merit but on stubborn ideology.
- andrewmcwatters 9y agoThat type of thinking led to C++, a language whose contributors do not know how to stop adding features.
- staticassertion 9y agoIs there a whitepaper associated with this? I found the project page and the github page, but I want to see: a) What's involved in getting the C code to be 'safe' - how much annotation overhead, etc b) How this interacts with C Preprocessor c) What the runtime overhead is (usually the killer)
- munin 9y agoThere is a specification: https://github.com/Microsoft/checkedc/releases/download/v0.7-final/checkedc-v0.7.pdf https://github.com/Microsoft/checkedc/releases/download/v0.7... We have been trying to publish a paper on this, perhaps one day we will be successful! Our runtime overhead is very good, and our interaction with the preprocessor is minimal.
- staticassertion 9y agoWonderful, thank you for the link.
- ausjke 9y agothe github is just a spec, you have to use llvm/clang to use this extension. it would be nice to make this an independent executable to do C checking, apparently not the case here.
- fulafel 9y agoAnyone know what stage the implementation (https://github.com/Microsoft/checkedc-clang https://github.com/Microsoft/checkedc-clang) is at, is it ready for use?
- lenary 9y agoIt is not ready for production use. Do try it out though!
- kqr 9y agoIf anyone is looking for a battle-tested (sometimes literally) production ready, statically and dynamically checked C-like language, take a look at https://ada2012.org https://ada2012.org!
- inamberclad 9y agoI really want Ada to get its fair share of attention. It's an impressively well thought out language. It's like a team of academics and industry pros took a long hard at C and C++ and then made something to fit the the same niche (high performance, real time, and very important software), but made it almost impossible to write incorrect code. Actually, that's exactly what happened.
- beefhash 9y agoLast I heard about Ada, there was the issue of having virtually no community that shares code freely, just commercial vendors selling theirs. Has that situation improved?
- inamberclad 9y agoThere's a free compiler called Gnat, but the community is still rather small. I think it's something of a catch-22. Most of the resources you'll find on the internet are old (mid-90s) blogs and educational sites. Here's some of the better ones I've found: https://www.adacore.com/gems/ https://www.adacore.com/gems/ https://blog.adacore.com/ https://blog.adacore.com/ https://two-wrongs.com/tags.html#ada https://two-wrongs.com/tags.html#ada The official reference manual is freely available too, but it's quite dense.
- wiz21c 9y agoGiven the parent comment : >>> fit the the same niche (high performance, real time, and very important software I don't see any community emerging from the web any time soon :-) To me this niche means "avionics, military, nuclear, medical, spatial". Not your week-end project.
- fuklief 9y agoHow does it compare to CCured ?
- Animats 9y agoFrom the technical paper: Checked C adds new types to C: • ptr<T>, the checked pointer to singleton type, • array_ptr<T>, the checked pointer to array type, • T checked[N], the checked array type, Many people have been down that road. GCC had "fat pointers" for subscript checking years ago.[1] That never caught on. Walter Bright had a proposal.[2] I had a proposal. Microsoft had "Managed C++". It's technically possible, but retrofitting old code rarely happens. [1] http://williambader.com/bounds/example.html http://williambader.com/bounds/example.html [2] https://news.ycombinator.com/item?id=8509155 https://news.ycombinator.com/item?id=8509155
- pjmlp 9y agoIt only works when companies take the "my way or the high way" approach. Hence why Microsoft went C++ and .NET Native, keeping C compatibility just as far as ANSI C++ requires it. And support C++ kernel code since Windows 8. Why the NDK is such a pain to use on Android, with a very tiny API surface. Why all Objective-C improvements are now only related to improve interoperability with Swift. So maybe if Microsoft decides to also force Checked C on Windows, it might eventually work.
- Animats 9y ago10-15 years ago, Microsoft had the clout to do that. They no longer do.
- pjmlp 9y agoThey still do, just in HN bubble people think they don't. Azure is a money printing machine, the "Year of Desktop Linux" will never happen, hybrid tablets/laptops with Windows are being bought instead of Android ones, IoT devices for healthcare or ticketing machines run on Windows, on the enterprise Linux mostly matters on the server room, they own PC/XBox gaming. So just like Apple and Google, they can do whatever they feel like it, because non-technical consumers aren't going to suddenly start buying alternative systems, just because devs don't like Apple, Google and Microsoft roadmaps regarding which programming languages one is allowed to use on their systems.
- ericfrederich 9y agoI wish that when all these guys tried to create a better C/C++ they made it compatible such that I could create shared libraries usable from other languages. C, not even C++, is still the only common denominator between languages. The fact that I cannot create a shared library in Golang or Python that is callable from the other is very disappointing.
- andrewmcwatters 9y agoI wonder if that has more to do with C++ undefined name mangling than anything else. C exports are beautifully simple to read. C++ exports on the other hand are some sort of alternate reality mind-bending shitstorm due to the language not defining a scheme for decorators. Maybe I'm wrong and hopefully someone can correct me; I don't have deep export knowledge outside of simply using C exports, but it seems this decision has had long-standing repercussions for people wanting to use FFIs outside of C.
- trentnelson 9y agoHow does this compare to SAL, out of interest? Somewhat competing aims, no?