4 ms·
You can do this. But it isn't C anymore, it's just a DSL on top of C and because it is a thing that is layered on top of an existing language that language is s
by jacquesm 4y ago
You can do this. But it isn't C anymore, it's just a DSL on top of C and because it is a thing that is layered on top of an existing language that language is still there and you will have to make sure that someone doesn't simply use the original unsafe language constructs.
I've built a thing like this for a project that had zero tolerance for any of C's well deserved faults, and yet, had to be written in C. I borrowed heavily from various other solutions (notably: Erlang) to ensure that the project was as safe as I could possibly make it. But I was always acutely aware that it only took one team member to declare a void * to an unbounded array for things to break out of that carefully constructed illusion of a safe jail.
Most of the work was done using some pretty tricky (and nifty!) macros and I really liked the end result, but I would have very much wished for that end result to be based on something more solid than C as the foundation. If you go down this route in the present day, unless you absolutely have to use something else. You'll be far happier and you will not have to be eternally vigilant against someone taking a shortcut.
- sheepscreek 4y agoThis sounds like JavaScript: The Good Parts revisited. Agree with everything you’ve written. However, in the rare situation that one has to go down this path, a linter/static code analyzer can prove to be a life saver. Found some on this page: https://github.com/caramelomartins/awesome-linters#cc https://github.com/caramelomartins/awesome-linters#cc
- ttfkam 4y agoLuckily, no one ever used The Bad Parts again after that book was published. Even getting folks to reliably use a linter can be a chore. Getting all C coders current and future to use these new techniques seems wildly optimistic to me.
- pulse7 4y agoIt's certainly better with such safety annotations than without...
- woodruffw 4y agoIt’s not clear to me that it necessarily is, if those annotations provide a false feeling of security. C-rusted doesn’t seem to have a real story for preserving invariants in linked-in but not locally compiled code, which is the main thing that makes C a valuable ecosystem.
- zozbot234 4y agoTo be fair, Rust does not really have such a story either. Externally compiled code (i.e. code not built with the exact same version of the compiler) must interact via the unsafe C ABI and be re-wrapped locally into safe Rust-friendly constructs. There are crates to make this a bit easier, though. Things might be different if you could preserve useful invariants all the way down to binary code, of course. Then externally compiled code could also be meaningfully checked for safety.
- Nullabillity 4y agoThe difference is culture and tooling: linking against random .so files is normal in the C world, while Rust strongly encourages you to use source dependencies.
- pjmlp 4y agoFor the time being, as Apple showed by their gigantic effort with Swift ABI, there are C and C++ shops (and Objective-C ones) that really care about dynamic libraries, which any alternative needs to take into account for their adoption. The main issue is the culture, being more in line with ALGOL/PL/Ada/Wirth languages point of view of why security matters, even if there is a bit of unsafe core around.
- Gigachad 4y ago[flagged]
- jacquesm 4y ago
- ActorNightly 4y ago>But I was always acutely aware that it only took one team member to declare a void * to an unbounded array for things to break out of that carefully constructed illusion of a safe jail. This is why code reviews are a thing. If you have competent programmers, with a CR process as part of CI/CD pipeline, you really don't need language features to handhold programming.
- maximilianburke 4y agoEven with the best code reviews, even with the best development practices, even with the most comprehensive automated testing processes, you still do need a language to help you. Case in point, even SQLite still has CVEs relating to memory safety: https://www.sqlite.org/cves.html https://www.sqlite.org/cves.html
- junofan 4y agoI dunno, those bugs look mostly no- or low-impact. Saying somebody needs a language for that is a bit strong in my opinion.
- slimsag 4y agoThe page itself literally leads with multiple extensive statements about how the sqlite CVEs are exaggerated and inaccurate: > Grey-hat hackers are rewarded based on the number and severity of CVEs that they write. This results in a proliferation of CVEs that have minor impact, or no impact at all, but which make exaggerated impact claims. Also how most CVEs require arbitrary SQL injection (and if you have this access, you're screwed already): > Almost all CVEs written against SQLite require the ability to inject and run arbitrary SQL. It also explains that: > Few real-world applications meet either of these preconditions, and hence few real-world applications are vulnerable, even if they use older and unpatched versions of SQLite. --- It also explains that many CVEs are denial-of-service due to division-by-zero, which in Rust can only be caught by using the opt-in `checked_div` so would be equally safe in C and Rust(?)
- 4y ago
- pjmlp 4y agoIt is as much C, as MISRA, Frama-C et al are. Or C coding on Windows with SAL.
- jacquesm 4y agoIndeed. But I see these as workarounds for very particular requirements, not as ways to build new software in a responsible way if other options exist.
- lmm 4y agoSometimes you can work around a "has to be written in C" requirement by generating (known-safe) C at build time and avoiding writing it directly.
- joecot 4y agoIs there maybe a linter you can use with C and then use as a required status check on merge requests in GitHub / Gitlab?
- drewm1980 4y agoYep, the company behind this advertises online demos in gitlab
- lost_tourist 4y agobut can't you lint for such things or even just grep? The world will always build a better idiot, we're just trying to reduce the attack surface.