6 ms·
It's interesting that these sorts of problems with memcpy, etc., always boil down to the same thing - C arrays are lowered to simple pointers when they are pass
by WalterBright 13y ago
It's interesting that these sorts of problems with memcpy, etc., always boil down to the same thing - C arrays are lowered to simple pointers when they are passed as function arguments. This means they lose their array bounds, frustrating attempts at overflow detection. This has caused endless bugs and endless workaround schemes of varying complexity. I wrote about it as "C's Biggest Mistake" a while back:
http://www.drdobbs.com/architecture-and-design/cs-biggest-mistake/228701625 http://www.drdobbs.com/architecture-and-design/cs-biggest-mi...
and offered a simple adjustment to C to fix it. I quote from the article:
All it needs is a little new syntax:
void foo(char a[..])
meaning an array is passed as a so-called "fat pointer", i.e. a pair consisting of a pointer to the start of the array, and a size_t of the array dimension. Of course, this won't fix any existing code, but it will enable new code to be written correctly and robustly.
- rwmj 13y agoThis is basically what Bounds Checking GCC[1] did. Except instead of changing the syntax or using fat pointers, it kept a map of (pointer -> bounds), allowing memcpy to check the bounds of any arbitrary runtime pointer, and to interwork with code that isn't compiled with the -fbounds-checking flag. [1] https://www.doc.ic.ac.uk/~phjk/BoundsChecking.html https://www.doc.ic.ac.uk/~phjk/BoundsChecking.html
- chalst 13y agoYour analysis of the issue of not being able to pass arrays as arguments is compelling, but I'm not so sure about "All it needs is a little new syntax" as a solution: you say how to keep backwards compatibility with existing C standards you need a #if cpp-directive: that's already a code health and maintenance problem that isn't going to go away. Regehr's suggestion of using assertions hidden in code avoids that: isn't it better? You could have just a comment directive like @ arraycast char* a \represents char a[..] and then you wouldn't need any cpp-directives.
- Serow225 13y agoI've always been curious why C was designed this way, was it thought to offer any advantages or just done to make the compiler writer's job easier?
- Someone 13y agoUnix and C were written in an agile fashion. C was a step up from a macro assembler, to be used for writing an operating system. String manipulation is rare in operating systems, certainly in those days (even if you felt a need, you don't have much room to create new strings in if your system and its applications must run in 64 kilobytes of RAM (144 kB for the PDP 11/45; see http://cva.stanford.edu/classes/cs99s/papers/ritchie-thompson-unix-time-sharing-system.pdf http://cva.stanford.edu/classes/cs99s/papers/ritchie-thompso...) The filesystem was about the only place where strings got manipulated and there, filenames were stored in fixed-size (14 bytes) buffers. Since all code could know strings were at most 14 bytes, having a complex type (pointer,length) was deemed a waste of memory and performance there. Everything else followed from that.
- emmelaich 13y agoIt would have made C a profoundly different language. C is what it is; a portable assembler. I believe K&R had a strong sense of minimalism; fancier string s would be seen as something that belonged in a library not the language. This has its benefits; _anything_ fancier would have embedded a whole set of assumptions about typical string handling usage for which the design had optimised. That no safer string handling library became popular can be seen as a indictment of C or a tribute to their tasteful restraint.
- Peaker 13y agoIt would help, but it wouldn't get rid of the errors. It would convert corruption/security bugs into other, much safer runtime errors. To get rid of the runtime error altogether, we need a stronger type system or a different form of static analysis, as mentioned here. Also, the extra bounds checking might be somewhat expensive in some scenarios, especially as the fat pointers would consume many more cache lines than thin pointers.
- pcwalton 13y agoCyclone [1] did this, among many other things. They actually got C provably memory-safe, though they sacrificed a fair amount of performance doing so. [1]: http://cyclone.thelanguage.org/ http://cyclone.thelanguage.org/
- pjmlp 13y agoHaving grown with safe languages back when C was a UNIX only thing I always consider that FUD from C people. Back then, C optimizers were not better than Object Pascal/Modula-2 ones and similar languages. It was all down to profiling and switching off bounds checking in specific code sections. Of course, with time compilers got better, while at the same time safer languages lost to UNIX widespread into the industry creating this idea in younger generations only C can have fast code.
- ScottBurson 13y agoC's second biggest mistake, IMO, is NUL-terminated strings. The combination of these two mistakes is much worse than either of them alone. Since you don't know where a string ends without scanning it, and nothing prevents you from writing off the end of the allocated space, the familiar 'strcpy' etc. are loaded guns with hair triggers.
- WalterBright 13y agoI'd argue that the nul-terminated strings is a direct consequence of arrays decaying to pointers when passed to functions.
- gaius 13y agohttp://en.wikipedia.org/wiki/Tony_Hoare#Quotations http://en.wikipedia.org/wiki/Tony_Hoare#Quotations I call it my billion-dollar mistake. It was the invention of the null reference in 1965
- WalterBright 13y agoI think people would prefer that openssl seg-faulted than silently allow theft of private data. I.e. the array-decay-to-pointer is a more expensive problem.
- ScottBurson 13y agoThey're clearly closely related, but it would technically have been possible to represent strings using a header with a length field followed by the characters in the string. That would have required people to pass the location of an arbitrary character as two explicit values, the string itself and an offset, which would certainly not have been in the spirit of the way C takes advantage of pointers, but nonetheless would have been possible. BTW I must take issue slightly with your characterization of the problem. Arrays decay to pointers immediately when referenced, not just when passed to functions. I suppose a C implementation could do bounds checking on non-pointer-mediated accesses to local arrays, but I've never seen an implementation do that (except those that do bounds checking more generally). Did yours? In fact, the way I put it once was, "C does not have arrays. It has pointers and notations for allocating and initializing memory; it does not have arrays." (Meaning, of course, "array" in the sense of a firstclass object, with bounds checking.)