News for hackers. Stuff that matters.
Re:Some Thoughts on Zig by pcwalton on Thursday July 21, 2022 @11:20PM (#32188101)
We've had close to 50 years to answer the question "is memory safety necessary to write secure code under normal industry development practices"? During this time dozens of languages have come and gone, and the evidence still overwhelmingly points to "yes". It's going to take a lot to make a convincing argument otherwise, especially since Zig doesn't really do much different from C here, semantically speaking.
Re:Some Thoughts on Zig by LAC-Tech on Thursday July 21, 2022 @11:39PM (#32188237)
I've explained how Zig memory management is different. You've chosen to ignore that and just compare it C and C++. I've suggested you learn Zig. You've ignored that too, and just zeroed in on the fact it's "manual".

Here's a car analogy - old manual vehicles had unsynchronised transmissions where the clutch had to be depressed twice per gear change. Newer manual vehicles use steering wheel mounted paddle shifters and the clutch only needs to be depressed when stopping or starting.

You're trying to push your weird semi-automatic transmission - that a lot of people find unusable as a daily driver - by ranting about how awful double clutching is.

Re:Some Thoughts on Zig by pcwalton on Thursday July 21, 2022 @11:47PM (#32188291)
Car analogies aren't a substitute for actually writing widely-attacked software.

There are systematic reasons why I'm skeptical Zig's approach will lead to better results than what we see in modern C++. The worst memory safety issues from a security point of view tend to be use-after-free caused by complex, untested code patterns, where one programmer wrote, or implied, "free", without realizing that somewhere else in the millions-of-lines codebase some other part of the code, written by someone else, still had a reference to the object. Unit tests don't catch these problems because they're caused by strange patterns that the programmer didn't anticipate. That is the kind of vulnerability that is difficult to solve without either a compiler or a runtime system (GC) to help you, and I see nothing in Zig that solves it. You can, and people do, make raw pointers to other objects in Zig.

Re:Some Thoughts on Zig by anonymoushn on Friday July 22, 2022 @12:44AM (#32188609)
In support of the parent who already knows all this: you don't even need to take raw pointers to have these problems! You'll usually get a slice from your allocator. Keeping any copy of that or any slice into that (e.g. a string in a struct obtained by deserializing a json document into a short-lived slab of memory) will get you use-after-frees too.
Re:Some Thoughts on Zig by Schroedingersat on Friday July 22, 2022 @04:31AM (#32189850)
Zig is not trying to be a better C++, so I don't see how complaining that it isn't a better C++ is relevant.

It's not trying to be for large, widely attacked projects. It's for things where the complexity overhead (whether resources or mental overhead) of Rust is too much, so you'd have to use C.

It's not meant to make memory errors impossible, it's meant to make them much easier to avoid or find and remove than it is in C.

Noone is suggesting anyone make a web browser in Zig, but it's likely useful for drivers or similar where you'd otherwise be using a lot of unsafe code, so rust's guarantees are at best a partial help, and at worst a false sense of security.

Re:Some Thoughts on Zig by Tozen on Friday July 22, 2022 @07:48PM (#32198548)
But isn't the position of a "Better C", already covered too, by languages such as D? And I mean literally too, with their "-betterC".

There is arguably no place for Zig to go, along the "better C" route, that hasn't been covered by other languages. And languages like D and Object Pascal, are way up on the charts, so it will be a hard climb to reach them in popularity. Even the much older Nim language (which has been called a C alternative), still hasn't cracked the top 50, among language rankings. Debatably, a language's "claim to fame" (to escape niche status), can't just be about being a more complex C replacement. There has to be other things about it, that it can firmly stand on, for greater mass appeal.

Re:Some Thoughts on Zig by Schroedingersat on Saturday July 23, 2022 @09:13PM (#32209233)
D is garbage collected and requires a runtime.

Object pascal appears to be pascal with classes.

Both of them have exceptions and operator overloading.

Zig is smaller than object pascal or D, and closer to the metal than D, it's arguably simpler than C as it's more regular, has no hidden control flow and has no separate preprocessor.

The unique things it's attempting to do are manual memory management with handrails (not dissimilar to pascal, but maybe with a few more handrails like testing allocator), error handling with better erganomics than go, rust, or unchecked exceptions (but slightly less power), and comptime which afaik is unique and is where it provides much of the power to do things like generics, many kinds of composition, or even implement a borrow checker and lifetimes as user code (with terrible erganomics and poor performance, but it's illustrative of its power).

The thing it stands on for mass appeal is trivial interop with C and the zig compiler is a functional, statically linked C toolchain out of the box and is easier for a beginner to do things like cross compile on than gcc or clang msvcc or similar. Additionally it has the power and simplicity of C with fewer footguns (ie. it attempts to cover some of pascal's strengths, but on the balance I like pascal more as a teaching language). It also shares some of nim's goals like fast compilation and hot code reloading.

The thing I'm personally interested in is the type system and semantics of comptime seem to have all the ingredients required for formal verification with no additions to the language, translations to a prover, or annotations (but it would require extending the compiler's abilities considerably). This is unique if true (but I will lack the skills to find out if it is true for a while yet, and the people closer to it are busy with other things).

Whether it will succeed, or the comptime concept is fatally flawed in some way is as yet unknown, but it does fill out a currently empty niche as a simpler C, it is not trying to be yet another more complex C.

I think its main competitor if it is successful will be go. Go is slightly higher level, arguably marginally simpler and considerably less powerful out of the box (at least last time I looked before the latest generics stuff). The last two are arguably in its favour for being a better C.

Re:Some Thoughts on Zig by Tozen on Tuesday July 26, 2022 @03:06AM (#32235274)
The languages which are closer to Go, are Vlang and Odin. With Vlang also having a focus on C interop as well (importing C libraries, C2V transpiler, and ability to be compiled to human-readable C). These debatably have a much better shot, with being viable alternatives to Go, in terms of use cases.

Zig is arguably in a different category and appears targeted to a more limited or lower space, in terms of general programming, which brings it more in the orbit of Rust. It's made clear that Zig's features and size will be purposely kept to the minimum, though its syntax is significantly complex. Which may hamper it, as it appears such complexity does to Rust, in terms of being picked up for general usage.

It's hard to say how attempting to be a "better Rust" or attempts at taking on C, will work out. But, it does seem clear it's not going for more general and wide usage that languages such as Object Pascal, D, or Go cover.

By the way, Object Pascal can be written where it comes quite close to classical Pascal, as classes are extensions of the language and more optional than required (see also advanced Records and/or Objects). Object Pascal is used in embedded systems, along with various dialects of Pascal, specific for the purpose. There are also various variants of Go, aimed directly at embedded development (TinyGo). So variants of Go and Pascal can more directly compete with Zig, and they are already well established (like Rust is). Now how this will all turn out, in say 5 years from now, anybody's guess. But, it will be an uphill climb for Zig, as for any new language trying to displace established ones.

Vlang documentation (https://github.com/vlang/v/blob/master/doc/docs.md)

Odin documentation (https://odin-lang.org/docs/overview/).

Golang by example (https://golangbyexample.com/)

Re:Some Thoughts on Zig by Schroedingersat on Wednesday July 27, 2022 @11:50AM (#32251970)
> Zig is arguably in a different category and appears targeted to a more limited or lower space, in terms of general programming, which brings it more in the orbit of Rust. It's made clear that Zig's features and size will be purposely kept to the minimum, though its syntax is significantly complex. Which may hamper it, as it appears such complexity does to Rust, in terms of being picked up for general usage.

I don't understand the basis on which you're saying this. From where I'm standing only odin looks simpler, the other two just make different tradeoffs -- namely trading reduced metaprogramming facilities (and the danger of complexity that entails on user code) for more language features.

> It's hard to say how attempting to be a "better Rust" or attempts at taking on C, will work out. But, it does seem clear it's not going for more general and wide usage that languages such as Object Pascal, D, or Go cover.

D and Go are closer to rust than zig is, and object pascal is also just another OO language. Of those, only Go is even attempting to be a better C, but requiring a runtime prevents it from being a true competitor.

Re:Some Thoughts on Zig by pjmlp on Friday July 22, 2022 @09:20AM (#32191614)
They could have used Object Pascal or Modula-2, and would get the same memory safety guarantees that Zig offers.
Re:Some Thoughts on Zig by Schroedingersat on Friday July 22, 2022 @09:57AM (#32191901)
And it makes no pretense otherwise.

Why bring this up?

Re:Some Thoughts on Zig by pjmlp on Friday July 22, 2022 @10:42AM (#32192351)
One would thought a C replacement in 2022 would have better hindsight regarding memory safety than what was already possible in 1978, which was actually even available in JOVIAL (1958), or ESPOL/NEWP (1961), among many other possible examples.
Re:Some Thoughts on Zig by Schroedingersat on Saturday July 23, 2022 @03:40AM (#32201260)
Guarantee =/= capability.

There are plenty of tools in the language and ecosystem for dealing with memory errors and reducing their frequency, but you're clearly not interested in a rational discussion.

Re:Some Thoughts on Zig by nemothekid on Thursday July 21, 2022 @11:57PM (#32188344)
I don't think pcwalton is a stranger to Zig; I'd be surprised if he hasn't played with it yet. Regardless my experience with Zig, is, as far as memory management was concerned, it was less powerful than C++. A UAF is still trivial in the language (the stdlib makes exploiting one pretty difficult); and the lack of smart pointers still seemed like a strange decision in our multi-threaded, multi-core world.

This makes something like the use of sanitizers required for large projects in Zig; It's a race against time until your project gets big enough that someone mistakenly frees something that gets used 200 lines later.

Re:Some Thoughts on Zig by throwaway17_17 on Friday July 22, 2022 @12:17AM (#32188477)
Slightly off topic, but I would prefer to never see smart pointers in Zig. Smart pointers are an inefficient and semantically-unenforceable ‘solution’ for the wrong problem. Smart pointers are supposed to be a way to handle references to mutable data in concurrent/multi-threaded/distributed computation, however I don’t think they form a satisfactory basis to solve the problem they think they solve. Now, I’m not a fan of Rust’s approach in general or it’s approach to this problem, but at least it is a semantically motivated solution.

If a Zig user wants to restrict usage of pointers in certain ways let them make their own data structures, but unless Zig is willing to alter its semantics including a ‘standard’ smart pointer is putting icing on a cardboard box.

/edited to fix a typo/

Re:Some Thoughts on Zig by thehaxerdude on Thursday July 21, 2022 @11:48PM (#32188300)
Rust is a terribly complex language, there are people at FAANG companies threatening to fork the language when attempts to simplify it are proposed. The amount of half-baked features present in the language are footguns in their own right.

I love Rust, but I think Zig solves 70% of the memory safety problem at the benefit of a much simpler language.

Re:Some Thoughts on Zig by pcwalton on Thursday July 21, 2022 @11:51PM (#32188312)
> Rust is a terribly complex language, there are people at FAANG companies threatening to fork the language when attempts to simplify it are proposed.

Ironically, the person I think you're referring to is actually me, and that's a strong mischaracterization of what happened. Nobody was ever "threatening to fork the language". Besides, that's irrelevant to this discussion, because none of the complexity of Rust compromises memory safety.

> I love Rust, but I think Zig solves 70% of the memory safety problem at the benefit of a much simpler language.

I don't believe the 70% number without evidence. Again, the problem that we really need to solve is complex UAF caused by unexpected interactions between components of large software systems, and I see nothing in Zig to solve that.

Re:Some Thoughts on Zig by kaba0 on Friday July 22, 2022 @02:59AM (#32189315)
The problem is that solving 70% of memory safety is arguably the same as solving 0% of it.
Re:Some Thoughts on Zig by flohofwoe on Friday July 22, 2022 @03:33AM (#32189505)
So basically, using Rust is also entirely pointless as soon as it needs to call into non-Rust code (like operating system shared libraries)? ;)

On a more serious note: IME most memory corruption issues in C and C++ are 'side effects' of the sloppiness of those languages (like not catching overflow, missing range checks, problems arising from implicit type conversions, sometimes just not compiling with warnings enabled, etc...).

Zig enforces much more correctness (to a point where it is sometimes annoying to a C coder, but one quickly gets used to it), and it even does this when building C/C++ code (just by using much more 'picky' default compilation options than C/C++ compilers).

Re:Some Thoughts on Zig by Tozen on Friday July 22, 2022 @12:44PM (#32194090)
But, is Zig really all that much simpler a language, from the viewpoint of a C alternative? And is that simplicity partially due to lack of features and youth, so that the complexity can be expected to pile on later. We are talking about a language that hasn't hit 1.0 yet, nor is not due to for some years to come.
Re:Some Thoughts on Zig by dralley on Friday July 22, 2022 @09:15PM (#32199212)
Andrew Kelley has been pretty explicit that he's going to hold a hard line on not adding a surplus of features to the language.