3 ms·
Can you imagine a better world? Can you imagine a programming language, library, and/or operating system that improves these issues? Perhaps one where integer
by tonyg 7y ago
Can you imagine a better world?
Can you imagine a programming language, library, and/or operating system that improves these issues?
Perhaps one where integer overflows were handled safely by default, with wrapping behaviour within easy reach of the programmer for those tricky bit-twiddling algorithms.
Perhaps one where allocating some large blob based on tainted data were a compile-time error until the programmer explicitly asserts that's what they really meant to do.
Perhaps one where process teardown isn't some faceless horror from the deepest depths, but a reasonable and expected way of reacting to an unexpected situation.
You don't have to settle for second-rate tools. Compilers and interpreters are just programs. We can learn from our mistakes.
> I just tire of hearing that switching your language will make it so you never write any bugs again.
Well, you might be reading too much into what I actually wrote. All I said was that writing C programs ought to be a literal crime against humanity, ultimately punishable by hanging from the neck until dead. A moderate and restrained position, if ever I heard one. I'd never go so far as to suggest that switching language would improve things in any way!
> The difference is that memory safety gives you better failure modes that don't lead to serious problems like remote code execution.
... yes! Yes. Exactly. This is exactly my point. Memory safety, proper support for integers, and so forth give you better failure modes. Significantly better failure modes. In the sense of "fail safe".
> You can make that argument reasonably without pretending the new language will make you infallible and the old language is responsible for all evil in the world.
Oh, I'd never say it's responsible for all the evil in the world. Just an eminently avoidable fraction of it.
- asveikau 7y ago> Yes. Exactly. This is exactly my point. Well actually, you said writing C should be a war crime...
- mike_hock 7y agoIn a sane language, there'd be no lossy automatic conversion, no arithmetic on incompatible types, no silent incorrect results by default, and no magic type selection for constants. What should integer arithmetic do by default? Either yield the correct result, or trap/throw. Of course. Obviously. No, this doesn't apply to rounding division, as there can never be any reasonable expectation that integer division is reversible or exact in the general case. But there are good reasons to want saturating arithmetic, wrapping arithmetic, or an optional/nullable result, so the language should provide all of these as opt-in (i.e. by using a syntax that clearly expresses the intent). What should floating point arithmetic do in case the exact result cannot be represented? Return the approximate result, of course. Floating point arithmetic cannot reasonably be expected to be exact. What should mixed floating point and integer arithmetic, arithmetic on mixed integer types, or mixed floating point precision do? Not compile! What was your intent when you wrote an addition of an int8 and an int32? Did you want an 8-bit addition and thought the int32 was actually an int8? Did you want to promote the int8 to an int32? Did you want to truncate the int32? Just say what you want explicitly. What should the type of a constant like 255 be? Nothing! It should not exist. If you want to specify a constant, specify the type with it. No automatic choice is reasonable. uint8? Then 255 + 255 would trap (if the language is otherwise sane, or worse, wrap, saturate, whatever). int32? OK, then what about 0x7fffffff? Also int32? Again, that added to itself would trap. So everything is int64 (or whatever the largest int is)? Then 0x7fffffffffffffff would trap. Eventually you'll hit the largest size available and there is nowhere to run. A burden on the programmer, unintuitive or needlessly complicated? Lolwat? It just cleanly exposes fundamental facts of the hardware that will crop up eventually anyway when you inevitably hit some limit, like in an overflow bug. And then you have to think about exactly those details. What is actually going on? What integer width is the addition performed with? Can it overflow? Is precision lost in an integer-to-float conversion, etc. Unfortunately, no mainstream language is sane.