4 ms·
The D language uses that to represent slices. It's a native type that has two fields - a pointer and a length. They're used like pointers, but with bounds check
by WalterBright 6y ago
The D language uses that to represent slices. It's a native type that has two fields - a pointer and a length. They're used like pointers, but with bounds checking.
Goodbye buffer overflows.
https://dlang.org/spec/arrays.html#dynamic-arrays https://dlang.org/spec/arrays.html#dynamic-arrays
- koolba 6y agoNot just good by overflows, it’s a better paradigm because you’re passing the context of the totality of the data to the target. It’s impossible to get the “wrong” length because it’s an intrinsic part of the reference.
- WalterBright 6y agoI've proposed this as a backwards-compatible improvement to C: https://www.digitalmars.com/articles/C-biggest-mistake.html https://www.digitalmars.com/articles/C-biggest-mistake.html but there's been no uptake on it. I don't get this reluctance to exterminate the biggest source of security bugs in C code, but that sort of reluctance is why I started the D project.
- MaulingMonkey 6y ago> I don't get this reluctance to exterminate the biggest source of security bugs in C code The forward thinking people who care about security bugs in C code - those ones who aren't reluctant - did exactly what I'd argue you've done: given up on C. It's selection bias in action. You started the D project. I'm RIIRing. Others prefer Python. Or perhaps one might use a language that compiles down into C, allowing you the best of both worlds - a decent programming language, and the ability to target whatever wretched excuse for a C89 compiler that has been forced upon you by whatever niche platform you're targeting. Fixing C's biggest mistake won't fix C. Fixing C's 10 biggest mistakes won't fix C. Fixing C's 100 biggest mistakes won't fix C, and by that point, it will no longer be C. There's an argument to be made for incremental improvements, so I won't discourage your efforts, but it's also the kind of pragmatic, boring, middle ground, half answer that won't make anyone outright excited either.
- WalterBright 6y agoFor excitement, DasBetterC is the answer. It looks and feels just like C, but is a modern language. But adding slices is an easy add to C, it is backwards compatible, it has zero impact on performance, it does not degrade anything about the C experience, it can be incorporated incrementally, and half of the memory vulnerabilities are taken behind the woodshed and shot. I'll even claim it leads to performance increases (from not having to do strlen over and over).
- titzer 6y agoI really do like slices, but implementing them as an (interior) pointer and a length creates some problems for GC'd languages. Is D GC'd by default? The main problem is that a GC needs to find the start of an object to scan it. Go originally solved this with 2-bit tags per word of memory. Now, I think they use size classes. For Virgil, I am considering adding slices but initially implementing them as a triple of (Array, start, length), to simplify this problem. This is pretty much required for Virgil's JVM backend, but the native backend can be more efficient in the future. Btw, can you racily write slice values and observe tearing in D?
- WalterBright 6y ago> Is D GC'd by default? It has a GC, but uses it only when operator "new" is called and a couple other cases. > can you racily write slice values and observe tearing in D? Shared data (meaning data accessible by multiple threads) is handled separately through locking library functions. Shared data types are distinct from other types.
- titzer 6y agoCan you racily write the same field containing a slice from multiple threads? Unless you have atomic multi-word writes, you could observe tearing and go out of bounds silently. One thing that JVM guarantees is that racy programs cannot break the type system. This gets hard when you have multi-word quantities.
- WalterBright 6y ago> Can you You have to use an atomic library function to do it.