News for hackers. Stuff that matters.
Re:How safe is Zig? by randyrand on Thursday June 23, 2022 @07:39PM (#31856412)
I feel like the most user friendly solution for Use After Free is to just use Reference Counting. Basically, just copy Objective-C's "weak_ptr" design.

For every allocation, also store on the heap a "reference object", that keeps track of this new reference.

  struct reference_t {
   // the ptr returned by malloc
   void* ptr;
   // the stack/heap location that ptr was written to.
   // i.e  the reference location.
   void* referenceAddr; 
  }
Reference track: every time ptr is copied or overwritten, create and delete reference objects as needed.

If ptr is freed, visit every reference and call *referenceAddr = NULL, turning all of these references into NULL pointers.

Re:How safe is Zig? by kaba0 on Friday June 24, 2022 @06:25AM (#31859813)
Reference counting is an incomplete solution it itself - it leaks circular references, and on multithreaded programs it is quite slow. Though some optimizations could be done statically to improve on that.

All in all, a mark-and-sweep will likely be faster (in throughput at least) let alone a good GC.

Re:How safe is Zig? by randyrand on Friday June 24, 2022 @01:06PM (#31865796)
Circular references wouldn’t matter.

The programmer would still decide when to free the object, not some automatic system. Manual memory management, with Ref Counting just to add some additional Use After Free checks.

Re:How safe is Zig? by jhgb on Friday June 24, 2022 @03:33AM (#31858741)
Wouldn't this, if applied by default, complicate Zig's easy C interoperability? I don't quite see how plain C code could play well with this.