6 ms·
Dlang added array bounds checking 20 years ago. It's a huge win. As evidenced by the article noting that 40% of the memory safety bugs were spacial. I used to
by WalterBright 2y ago
Dlang added array bounds checking 20 years ago. It's a huge win. As evidenced by the article noting that 40% of the memory safety bugs were spacial.
I used to have all kinds of problems with array overflows. I didn't make them very often, but when I did, they took a long time to track down. They've been gone for 20 years now.
Note that it would be easy to add it to C/C++:
https://www.digitalmars.com/articles/C-biggest-mistake.html https://www.digitalmars.com/articles/C-biggest-mistake.html
It would be the most useful and cost-effective enhancement ever.
- dataflow 2y agoThey have it already, it's called std::span.
- pjmlp 2y agoNo they didn't, if you care about security, gsl::span is the answer.
- dataflow 2y ago> No they didn't, if you care about security, gsl::span is the answer. https://godbolt.org/z/Pda9Me45P https://godbolt.org/z/Pda9Me45P ?
- pjmlp 2y agoUnless you use .at() it isn't portable to assume code safety.
- dataflow 2y agoSo what? Just pass the command-line flag to enable the code safety in your toolchain. The same way you pass it to enable optimizations in your toolchain.
- coffeeaddict1 2y ago> The same way you pass it to enable optimizations in your toolchain. No, it's not the same. I never enable optimisations by manually passing in flags to the compiler. It's always a `cmake -DCMAKE_BUILD_TYPE=...`. There is no such easily accessible equivalent for bounds checking.
- almostgotcaught 2y ago> I never enable optimisations by manually passing in flags to the compiler Lol then you don't use your compiler/toolchain correctly. How is that anyone's problem but yours?
- coffeeaddict1 2y agoHow exactly am I not using my toolchain correctly? What's the "correct" way?
- dataflow 2y agoHave you tried https://discourse.cmake.org/t/strictly-appending-to-cmake-lang-flags/6478/9 https://discourse.cmake.org/t/strictly-appending-to-cmake-la...
- coffeeaddict1 2y agoWhat flag can I pass to CMAKE_CXX_FLAGS to enable bounds checking on all platforms regardless of the compiler used? I can do that for optimisations with `CMAKE_BUILD_TYPE`.
- dataflow 2y ago(How) do you have no control over the environment variables you call CMake with?
- coffeeaddict1 2y ago
- coffeeaddict1 2y agoIt's quite obvious to me that the C++ folks running the committee didn't care about safety much. How can they standardise `std::span` knowing it's unsafe? They care now (well they pretend at least) because Rust is going to take significant market share in domains where C++ is still king.
- pjmlp 2y agoRust isn't the reason, rather governments are now serious about security, just like in any other industry.
- coffeeaddict1 2y agoWell yes and no. Rust is the reason because it's a real memory-safe alternative for system programming. If it didn't exist, governments would give C and C++ a "pass" for being memory unsafe.
- pjmlp 2y agoExcept, Rust is never mentioned alone on cybersecurity advisories, the anti safety folks are the ones doing that.
- aninteger 2y agoMeh.. I kind of don't think so. The reason you and I keep ending up on haveibeenpwned has likely nothing to do with Rust or C++.
- pjmlp 2y agoIndeed it has to do with lack of liability, but it will come. And then evolution will take care of which programming ecosystem are less expensive to result in lawsuits, or invalidation of insurance policies.
- 3836293648 2y agoThey didn't just standardise it when it was unsafe. They got a proposal for a safe span and demanded that safety be removed before they'd accept it
- coffeeaddict1 2y agostd::span is not bounds checked by default.
- dataflow 2y agoOptimizations aren't enabled by default either, and yet everyone passes a flag to optimize, and nobody argues C++ sucks just because you need to pass a flag to enable optimizations. Is it so hard to pass another flag to enable bounds checking?
- saagarjha 2y agoYes.
- dataflow 2y agoEh? How/why?
- saagarjha 2y agoBecause turning on optimizations makes your code faster and turning on bounds checks makes your code slower. Hence, one gets used far more than the other.
- dataflow 2y ago> Because turning on optimizations makes your code faster and turning on bounds checks makes your code slower. Hence, one gets used far more than the other. The question was "is it so hard to pass a command line flag". You said "yes" when you clearly don't see any difficulty with actually passing the flag. Instead you're apparently answering a totally different question: "why do people lack the motivation to do this." Which had nothing to do with the point you replied to. It's not like opt-out vs. opt-in somehow changes the performance characteristics. People who want maximum performance will turn it off. People who want safety will turn it on.
- lpapez 2y agoThanks for sharing, I enjoy reading your posts in regards to how ahead of time Dlang was in adopting these improvements. I wanted to ask: did you ever consider what was missing from Dlang to achieve widespread adoption? Clearly it was not features, so I'm wondering what that would be from your pespective.
- WalterBright 2y agoThe marketing department was what was missing. I've always had that problem. Borland was brilliant at marketing an inferior compiler. Phillippe Kahn is an amazing businessman. (He's also a very fun person to talk to.) For example, Borland at one point decided to include the source code to some of its runtime library for free. At a compiler roundup in the magazine, this was hailed as a great advance forward by the reviewer. Meanwhile, Datalight C was also in the roundup, and had always included 100% of the runtime library source code. No mention was made of this.
- dataflow 2y ago> what was missing from Dlang to achieve widespread adoption This: https://godbolt.org/z/s49qzPn81 https://godbolt.org/z/s49qzPn81