6 ms·
The #1 problem with C is buffer overflows. The solution is pretty simple: https://www.digitalmars.com/articles/C-biggest-mistake.html https://www.digitalmars.c
by WalterBright 5y ago
The #1 problem with C is buffer overflows. The solution is pretty simple:
https://www.digitalmars.com/articles/C-biggest-mistake.html https://www.digitalmars.com/articles/C-biggest-mistake.html
and does not break existing code.
- thingsgoup 5y agoNeat idea. What does implementing new syntax in one of the established C compilers involve? Is it the kind of thing that could be reasonably tackled in a small patch just to play with?
- WalterBright 5y agoIt wouldn't be hard. The semantics are straightforward and don't interfere with the way a C compiler already works.
- bhaak 5y ago> C is probably the patriarch of the longest list of languages. Notable among these are C++, the D programming language, and most recently, Go. There are endless discussion threads on how to fix C, going back to the 80’s. Why is Java missing in that list?
- thingsgoup 5y agoI suspect it’s because manual memory management in Java isn’t built into the language. Is it even possible? I’m not a Java programmer and I don’t know. My understanding has been that the runtime doesn’t expose the memory model to you.
- kaba0 5y agoIn a way it does. Java just likes to push most features to methods on special objects, instead of exposing them as native functionality (to avoid backwards incompatible changes). So it would look something like MemorySegment.allocateNatice(100, someScope). This new API has a runtime ownership model, so by default only a single thread can access this memory address, and it can be freed at will.
- laumars 5y agoMaybe because Java is more of a direct descendant of C++ than pure C (though obviously C++ is an iteration of C). Or maybe they omitted it before there are literally hundreds of languages that were inspired from C and listing them all would have been boring for the reader.
- futharkshill 5y agoWhy would you think Java is a descendant of C++? There may be overlap in some syntax, mainly from C. C++ is not, and was not, the only OOP language, and I have heard no such that that is should be descendant of C++.
- pletnes 5y agoI was once at a talk given by James Gosling. He said that Java-the-language was «a trick to get C++ programmers to use the Java Virtual Machine». He deliberately made Java very similar to C++ but removed what he saw as the hard and risky parts (memory management, operator overloading, etc) that are typically not required for standard applications. Well, that was my interpretation of what he said, errors are my own etc. But this would make Java a direct descendant of C++, in my mind.
- bhaak 5y agoI have never heard that quote from James before. Are you sure about the JVM? The JVM was quite controversial back then, Java first had to prove that you could make a performant virtual machine. But Guy Steele claimed "We were not out to win over the Lisp programmers; we were after the C++ programmers. We managed to drag a lot of them about halfway to Lisp."
- pletnes 5y agoWell, it’s many years ago and memory corruption is real. I got the impression that their goal was to get adoption of (what at some point became) the JVM, or the «compile once run anywhere» vision. They envisioned many languages to coexist on the JVM, which kinda happened but maybe not as much as they thought. So they designed a language to get started, Java, and made it familiar-looking to get people om board.
- andai 5y agoI'm fairly new to C, am I understanding correctly that the new syntax is just sugar for a struct containing two values: a pointer to the start of the array, and its length? This can of course be done without the new syntax, and it seems exceedingly useful. Are such structs commonly used in C projects? What is the actual source of bugs in this regard? How does passing the length as a separate parameter lead to more bugs than having it bundled -- is the main source of error passing the wrong variable?
- thingsgoup 5y agoIf the length is bundled with the pointer as syntax then the compiler and maybe even the runtime can provide checking on behalf of the programmer. Passing it by hand means it’s the programmers responsibility to (remember to) validate
- WalterBright 5y ago> How does passing the length as a separate parameter lead to more bugs than having it bundled It boils down to being inconvenient, unreliable, error prone, and difficult to audit. That's why it isn't used and C's #1 problem remains buffer overflows. And so it goes for all the other solutions for C for this problem, except my proposal. My proposal is how D works, and it's been convenient, reliable, robust and auditable for 20 years. You can still use raw pointers in D, they are fully supported, but the use of the arrays make use of raw pointers rare.
- coldtea 5y ago>This can of course be done without the new syntax, and it seems exceedingly useful. Are such structs commonly used in C projects? There are string libraries that work that way in C. If you do it manually, without a lib, then you need to check that the length is valid yourself (after every operation), so it's not as useful as a language with first class support for it.
- naasking 5y agoPresumably you can add an inlined array access function or macro in the header to do this for you.
- deleted 5y ago[deleted]
- marginalia_nu 5y ago> The #1 problem with C is buffer overflows. There is also the null hypothesis that buffer overflows are simply a category of bugs that is simply easy to identify, and thus, apparently prevalent.
- jodrellblank 5y agoWould it be reasonable to assert that bugs which have such rare and uninteresting consequences that nobody has either noticed (not widely prevalent) or identified them (worth investigating because the consequences were severe), could be the number one problem?
- marginalia_nu 5y agoThe cold is one of the most common human infections, does that make it the most severe health problems facing humanity as well?
- ciphol 5y agoI've never heard of anyone dying of the cold. I've heard of many major security breaches caused by buffer overflows.
- deleted 5y ago[deleted]
- jodrellblank 5y agoYou started off suggesting there may be more serious problems in C which haven't been identified or are not prevalent. In this comparison we have identified car crashes, heart disease and lung cancer as health problems, and they are prevalent.
- WalterBright 5y agoBuffer overflows aren't easy to identify. That's why they lurk for years in shipping software, and then there's a panic when they're discovered.
- prirun 5y agoFor strings, you need more than the allocated length: you need the current length too. Otherwise you end up with: - O(n) algorithms that are constantly scanning strings looking for a zero byte - strings can't contain zero bytes - strings have to contain one zero byte - putting a zero byte in the middle of a string chops it off - probably other nonsense I haven't thought of Please don't adopt another half-assed solution just because it fits more easily into C's existing set of crap. That's how we got fake strings in the first place.
- WalterBright 5y agoThis solution has been in D for 20 years. It works very well. It is fully assed. It is very, very rare to see a buffer overflow in D because the use of these arrays is so easy, convenient, and robust. Not only does it virtually eliminate buffer overflows (when used), it is more efficient than 0 terminated strings. It does not need to scan the strings, nor does it need to load the string into memory to determine its length. I understand your concerns about mixing it up with 0 terminated strings. They are real, but have not been a particular problem in practice. What happens is one simply moves away from using 0 terminated strings. A zero terminated string can be converted to a length one with: a = s[0 .. strlen(s)]; Going the other way requires a memory allocation similar to what strdup() does.
- prirun 5y agoI read your article before posting, and it says: ----- 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. ----- I didn't see a "current length" mentioned. Is it there? Can I have a string with an allocated length of 20 bytes and a current length of 10 bytes, without using a looking for a zero byte?
- WalterBright 5y agoThe capacity value is not part of it. This proposal is not about memory management any more than 0 termination is about memory management. It is just about finding the end of the array.
- rst 5y agoThe CHERI extensions for the ARM architecture allow for compilers that achieve this effect by making all pointers "fat", with bounds, and doing pervasive hardware bounds checks. They've been playing with FPGA versions and emulators for a while, but the first actual SoCs just got shipped: https://www.theregister.com/2022/01/21/arm_morello_testing/ https://www.theregister.com/2022/01/21/arm_morello_testing/ -- software for it includes tweaked versions of BSD and I think Linux which use bounds-checked pointers throughout, including in the kernel.
- WalterBright 5y agoMaking them all fat doesn't fix code that uses strlen().
- rst 5y agoNot quite sure what you mean. All it can do is turn undefined behavior on an out-of-bounds reference into a segfault, but if that ends up turning an RCE vulnerability into something less severe, it's still an improvement -- at least from that perspective. The intent is for the hardware-checked bounds to be at very close to the declared bounds for arrays in source code, at least in cases most typically subject to buffer overlow; see https://www.cl.cam.ac.uk/techreports/UCAM-CL-TR-947.pdf https://www.cl.cam.ac.uk/techreports/UCAM-CL-TR-947.pdf (And I'm not sure how the alternative fat-pointer proposal you mentioned does better. Searched that page for references to strlen, but didn't find much.)
- WalterBright 5y agoMost arrays do not have declared bounds, that's why a runtime check is necessary.
- rst 5y agoBounds for storage are known when it is allocated. CHERI puts those bounds into the pointers (which double in size so they can fit), and has the processor do the runtime checks at every dereference. So, same effect as the proposal you reference, AFAICS -- just without altered syntax.
- BenFrantzDale 5y agoSo built-in std::span? https://en.cppreference.com/w/cpp/container/span https://en.cppreference.com/w/cpp/container/span
- gjvc 5y agodjb has https://cr.yp.to/lib/stralloc.html https://cr.yp.to/lib/stralloc.html and https://cr.yp.to/lib/array.html https://cr.yp.to/lib/array.html