News for hackers. Stuff that matters.
Re:Some mistakes Rust doesn't catch by dhosek on Monday February 07, 2022 @11:58PM (#30255038)
I think the rust making you aware of what you need to do is a good thing. I have a vague memory of writing C code when I was a teenager where I directly manipulated a string from argv[] doing some concatenation or other and it worked fine until it didn't because I was merrily stomping on memory I didn't own (I was thinking in Pascal terms while writing C code). The whole String vs &str thing in Rust can feel a bit fiddly at times, but it does make you aware of what's happening (and I've found that it has improved my JVM coding as well although I do have to occasionally remind myself that I don't need to worry about borrows and lifetimes).
Re:Some mistakes Rust doesn't catch by amluto on Tuesday February 08, 2022 @04:55PM (#30265337)
Your teenage program wasn’t called pkexec perchance?

/me runs

Re:Some mistakes Rust doesn't catch by usrbinbash on Tuesday February 08, 2022 @03:32AM (#30256100)
The thing is, C also makes me aware of what is happening, by letting programs fail when I forget about certain things.

This may not be as nice as the compiler yelling at me even before the program is run, but it certainly makes for a much simpler language.

Re:Some mistakes Rust doesn't catch by AnyTimeTraveler on Tuesday February 08, 2022 @03:50AM (#30256193)
But C doesn't let programs fail. (or at least not loudly) That's the problem, to me at least.

I've had this code yesterday on an embedded arm cortex m0:

bool tx_byte(char msg) { for (int i = 0; i < 8; i++) { tx_bit((msg >> i) & 1); } }

This function wouldn't return. What's even stranger, the variable i would increment past 7. The for-loop just continues.

I got a warning about the missing return statement in that function, but didn't think there could be a connection to my bug. It turned out that adding that return after the for loop fixed it.

The warning was one among about 100 others, as I was refactoring quite some code at the time. I only started working on the warnings after I had decided that me an two of my colleagues were just overlooking something and would fix the bug another day.

All that I wanted to say is that, no, C doesn't make you aware of what is happening. It has this giant pit of UB that does weird things when you fall into it. I was lucky that in my instance, I got a warning. I know other UB just works most of the time and goes unwarned.

Re:Some mistakes Rust doesn't catch by ascar on Tuesday February 08, 2022 @06:17AM (#30257143)
Sounds like the compiler didn't correctly adhere to calling conventions without an explicit return statement.

Wouldn't that even be a compiler bug allowing no return statements, but then incorrectly handling scope?

Re:Some mistakes Rust doesn't catch by tux3 on Tuesday February 08, 2022 @06:27AM (#30257208)
It's undefined behavior, the compiler can do whatever it wants.

Here the compiler assumes the function never returns (because it would be UB since there's no return statement), so it looks like it just didn't bother to add a loop exit at all.

This is standard compliant.

Re:Some mistakes Rust doesn't catch by nybble41 on Tuesday February 08, 2022 @06:40PM (#30266459)
Actually it's only undefined (per C99 anyway) if the return value is used:

> If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.

So if the return value is never used then there is no undefined behavior and a standards-compliant compiler isn't allowed to optimize away the loop exit condition.

Re:Some mistakes Rust doesn't catch by nsajko on Tuesday February 08, 2022 @06:34AM (#30257253)
> I got a warning about the missing return statement in that function, but didn't think there could be a connection to my bug.

This is the whole story, really.

Re:Some mistakes Rust doesn't catch by leovailati on Tuesday February 08, 2022 @10:37AM (#30259655)
To complement your example, there are also cases when everything coincidentally happens to work fine. For example:

https://stackoverflow.com/questions/7280877/why-and-how-does...

Re:Some mistakes Rust doesn't catch by kjeetgill on Tuesday February 08, 2022 @03:44AM (#30256164)
That reminds me of my favorite bug in a C program I ever delt with. We had a dorm floors' worth of "Intro to C" students pouring over our friend's buggy program.

With a enough printfs() I was able to whittle the problem down to: A local variable "b" was changing after some function call that looked like: "call(&a)". Turns out because our program looked like "{ int a[3]; int b; ... call(&a); }" an off by one error accessing "a[4]" in the function was overwriting variables never passed to it!

Felt like the champ figuring out. (This is a 15 year old story from freshman year, so I hope I got the details right.)

Re:Some mistakes Rust doesn't catch by kjeetgill on Tuesday February 08, 2022 @05:46PM (#30265932)
Update, since this got attention: I remembered slightly wrong: "b" would need to be declared before "a", not after and you'd access it with "a[3]" not "a[4]". So after this, "b" would be 7: "int b=5; int a[3]; a[3]=7;". Looks like I'm still making the same mistakes.

But still: If you passed the array and referenced it wrong it would change local variables in some remote stack frame.

Re:Some mistakes Rust doesn't catch by cozzyd on Tuesday February 08, 2022 @04:41AM (#30256527)
valgrind is your friend!
Re:Some mistakes Rust doesn't catch by benibela on Tuesday February 08, 2022 @12:02PM (#30260983)
That does not really work for stack variables
Re:Some mistakes Rust doesn't catch by renox on Wednesday February 09, 2022 @03:32AM (#30270347)
There is ASAN https://github.com/google/sanitizers/wiki/AddressSanitizer which may work for stack variable though.
Re:Some mistakes Rust doesn't catch by nindalf on Tuesday February 08, 2022 @04:05AM (#30256295)
That’s horrifying.
Re:Some mistakes Rust doesn't catch by dureuill on Tuesday February 08, 2022 @07:05AM (#30257463)
> The thing is, C also makes me aware of what is happening, by letting programs fail when I forget about certain things.

Not necessarily. Many C programming errors invoke Undefined Behaviour, and then "appearing to work correctly" is a permissible option. So you will miss the programming error, until a change seemingly unrelated to that part of the system will trigger some segfaults or wrong results, or allow a determined attacker to use your program to launch calc.exe.

Re:Some mistakes Rust doesn't catch by avianlyric on Tuesday February 08, 2022 @06:29AM (#30257222)
If that was true then nobody would be fretting about unsafe memory access, and the primary source of bugs used in exploits wouldn’t exist.

Unfortunately it isn’t true, C let’s you write plenty of broken programs that don't fail at run time, despite making a complete dogs breakfast of your systems memory space, instead we get crap like Heartbleed instead.

Re:Some mistakes Rust doesn't catch by usrbinbash on Tuesday February 08, 2022 @07:56AM (#30257811)
Its also perfectly possible to write broken programs in languages that take care of all memory related issues:

    var ch chan bool
    for i := 0; i < 10; i++ {
        go func() {
            // do something amazing
            ch<-true
        }()
    }
Oops, I just leaked 10 goroutines, because I forgot to `make` the channel instead of declaring it, so its nil and will block indefinitely. Unless the program encounters an complete deadlock of all gorutines, this will not crash anything, and the caller of this code has no way of knowing it happened.

Broken code can be written in any language, no matter how much checks & convenience it offers.

Re:Some mistakes Rust doesn't catch by tialaramex on Tuesday February 08, 2022 @09:22AM (#30258666)
This depends on deciding that "broken code" is all the same, but it isn't.

Suppose I screw up writing the GIF decoder for avatars on my forum web site and you are a bad guy who can create accounts and upload an avatar.

If I write the decoder in C it is very possible that your bad files can seize control of the web server, spill user credentials, post nonsense, mine crypto-currency on my servers, anything.

If I write the decoder in safe Rust, some of these things are much harder to pull off, and I need to be really incompetent to cause the worst harm - but you can likely cause a lot of mayhem still if I screwed up.

But if I write the decoder in WUFFS, the best you can achieve is to have the decoder chew CPU in an infinite loop or something. There are no boundary misses, integer overflows, or anything like that in WUFFS. My decoder might render your weird input as a giant orange splodge, or, as I said, spin forever wasting CPU, but it can't accidentally become a reverse shell server, or send you credentials from my password database, such things are entirely impossible.

This is because WUFFS is a special purpose language. There doesn't need to be a way to write these nonsense programs in WUFFS, whereas it must be possible in a language like C to achieve the "general purpose" designator. But this should encourage us to write as little as possible with these unnecessarily powerful languages, like the way you don't use a chainsaw to sharpen pencils.

Re:Some mistakes Rust doesn't catch by zkldi on Tuesday February 08, 2022 @08:56AM (#30258383)
You can write a forkbomb in any language -- that's besides the point.

Some languages are easier to write broken code in than others.

Re:Some mistakes Rust doesn't catch by mypalmike on Tuesday February 08, 2022 @04:44PM (#30265199)
I don't see any memory getting unexpectedly stomped on.

C is a great language, but it is absurdly common to accidentally write code that writes garbage to nearby memory, or can be exploited to do so. Such bugs are the cause of a vast number of security problems. This characteristic of C is simply not true of most other languages.

Re:Some mistakes Rust doesn't catch by masklinn on Tuesday February 08, 2022 @09:58AM (#30259120)
C++ has even formalised a concept of compilation false positives (due to issues like separate compilation, which make it impossible for the compiler to know that you’re linking incompatible artefacts).
Re:Some mistakes Rust doesn't catch by kaba0 on Tuesday February 08, 2022 @03:37AM (#30256128)
The thing is, C won’t even fail in a reproducible way. Good luck hunting down mistakes caused by uninitialized variables for example.

Sure, valgrind exists, but that is not native execution.

Re:Some mistakes Rust doesn't catch by bregma on Tuesday February 08, 2022 @06:53AM (#30257392)
Why would runtime checking for a condition that could be caught at build time be in any way a more desirable solution?
Re:Some mistakes Rust doesn't catch by 10000truths on Tuesday February 08, 2022 @05:47AM (#30256943)
MemorySanitizer will catch uninitialized variable errors.
Re:Some mistakes Rust doesn't catch by tialaramex on Tuesday February 08, 2022 @07:18AM (#30257564)
The Sanitizers catch errors that happen when they're running

So in many situations the Sanitizer gives you a false sense of security that your code is correct because in the test environment, with the Sanitizer, it works, and then in Production, without a Sanitizer, under some circumstances it blows up because those circumstances never arose in your test environment.

Compare (safe) Rust which doesn't have uninitialized variables, and even WUFFS which doesn't have array bounds errors. In both cases the entire problem doesn't exist, programs which exhibit this problem are not valid programs in those languages, which is great because we did not mean to write these programs anyway. Hooray.

Re:Some mistakes Rust doesn't catch by usrbinbash on Tuesday February 08, 2022 @03:39AM (#30256140)
The reproducible thing about this error would be: I forgot to initialize my variables.
Re:Some mistakes Rust doesn't catch by MereInterest on Tuesday February 08, 2022 @03:54AM (#30256214)
That results in undefined behavior, which is by definition, not reproducible.
Re:Some mistakes Rust doesn't catch by usrbinbash on Tuesday February 08, 2022 @04:47AM (#30256554)
The behavior of the program is not reproducible.

The cause of the error is.

Re:Some mistakes Rust doesn't catch by programmer_dude on Tuesday February 08, 2022 @06:01AM (#30257042)
I think you are confused. The cause is not something you reproduce. It just exists. You can however "isolate/determine" the root cause.
Re:Some mistakes Rust doesn't catch by usrbinbash on Tuesday February 08, 2022 @07:42AM (#30257727)
If I have 10 different errors, but the root cause for all of them is that I forgot to do something I should have done (initialize a variable), then the cause is the same across all 10 different errors. If I don't start initializing the variables, then I will continue to have erros (which may or may not be similar to each other), thus making the problem reproduceable.

The errors are not the problem, but the result of the underlying problem.

Re:Some mistakes Rust doesn't catch by MereInterest on Tuesday February 08, 2022 @11:16AM (#30260245)
If you have an error that only occurs on your computer, on your compiler version, on your particular optimization level, on this particular phase of the moon, how do you determine that the root cause is forgetting to initialize a variable? In the context of debugging, "reproducible" means "can reproduce that exact error in order to better identify the root cause", not "frequently occurs as a root cause".
Re:Some mistakes Rust doesn't catch by catlifeonmars on Tuesday February 08, 2022 @05:25AM (#30256795)
I don’t think that a simpler language and a helpful compiler are mutually exclusive. In the article, the author even points out multiple examples where Go (a simpler language, relative to Rust) could output more helpful error messages.

I assert it’s possible to have a simple language where it’s difficult to shoot yourself in the foot. C just happens to be a simple language where it’s exceedingly easy to shoot yourself in the foot.

Re:Some mistakes Rust doesn't catch by kaba0 on Tuesday February 08, 2022 @06:14AM (#30257125)
> I assert it’s possible to have a simple language where it’s difficult to shoot yourself in the foot

Zig would be one such example.

Re:Some mistakes Rust doesn't catch by sgt on Tuesday February 08, 2022 @07:21AM (#30257584)
So why is Zig being dwarfed by Rust in this arena? Shouldn't Zig be more popular or is it still too small and immature?
Re:Some mistakes Rust doesn't catch by zozbot234 on Tuesday February 08, 2022 @10:24AM (#30259460)
Rust is built to be 100% memory safe, not just "hard to misuse in practice". Of course the claim comes with some provisos about how you're always relying on 'unsafe' code, but there is real value to it. Zig is interesting but doesn't have that USP.
Re:Some mistakes Rust doesn't catch by sgt on Tuesday February 08, 2022 @12:54PM (#30261835)
So basically Zig is a toy, Rust is for serious stuff.
Re:Some mistakes Rust doesn't catch by catlifeonmars on Tuesday February 08, 2022 @01:47PM (#30262633)
That’s an oversimplification. There are many reasons to choose one programming language over another. Strong guarantees of memory safety is only one design parameter.
Re:Some mistakes Rust doesn't catch by kaba0 on Wednesday February 09, 2022 @12:45AM (#30269349)
Is C a toy?

Zig just provides different guarantees.

Re:Some mistakes Rust doesn't catch by wyldfire on Tuesday February 08, 2022 @08:12AM (#30257953)
Zig refers to itself with a version number < 1 and says that the language is not yet stable. That's going to filter the less adventurous crowd out right away. And that's probably ideal - zig can work out the kinks with the adventurous crowd and be better prepared for its post-1.0 life.
Re:Some mistakes Rust doesn't catch by rthomas6 on Tuesday February 08, 2022 @09:02AM (#30258445)
I haven't used Zig, but my understanding is that Zig is more comparable to C, whereas Rust is more comparable to C++. Zig is a much smaller and simpler language, but Rust has more high level constructs and abstractions.
Re:Some mistakes Rust doesn't catch by sgt on Tuesday February 08, 2022 @10:13AM (#30259295)
I can't comment on either, but I do enjoy C and you can build relatively safe C programs using tools like Valgrind and static analysis. So unless you are building the next OpenSSL it could very well be worth sticking to C11.
Re:Some mistakes Rust doesn't catch by mamcx on Tuesday February 08, 2022 @10:44AM (#30259746)
> whereas Rust is more comparable to C++

That only in matter of complexity. If rust were actually comparable to C++ it will a major mess.

Rust is actually a ML + C + Haskell-ish lang..

Re:Some mistakes Rust doesn't catch by sgt on Tuesday February 08, 2022 @12:28PM (#30261381)
And that's exactly why I am a bit worried about learning it. I am not looking for a language that is too theoretical, like Haskell.
Re:Some mistakes Rust doesn't catch by mamcx on Wednesday February 09, 2022 @07:33AM (#30271878)
> I am not looking for a language that is too theoretical, like Haskell.

Rust is not "theoretical", but have some theory applied to great effect. The comparison to Haskell is more in the use functional idioms, algebraic types, pattern matching and bits here and there...

Re:Some mistakes Rust doesn't catch by programmer_dude on Tuesday February 08, 2022 @09:26PM (#30267933)
It's a very practical language (more than C or C++). But it does have a challenging learning curve.

Pay no attention to how the language was derived. Just dive into it.

Re:Some mistakes Rust doesn't catch by masklinn on Tuesday February 08, 2022 @01:54PM (#30262749)
Elm is a good example of what you’re talking about, though it “cheats” a bit by being quite domain specific.

Anyway it’s simultaneously a very simplified cousin of Haskell (similar syntax but most of the advanced syntactic and type stuff dropped), and where the current movement to make compiler error message more readable, conversational, and helpful (e.g. decoration, explaining the “reasoning”, providing possible solutions) originates.

Re:Some mistakes Rust doesn't catch by 10x-dev on Tuesday February 08, 2022 @06:55AM (#30257405)
In my experience (and to generalize a bit), people who shoot themselves in the foot with C are the same people who will shoot themselves in the foot with any other language.

Those who aren't careful enough to free up allocated memory are the same types of people who leak memory in Java.

You can't fix lack of knowledge with a compiler. You can only catch accidental mistakes, but that assumes that the norm is for the person to know what they are doing and a lot of people have only superficial knowledge of the tools they are using.

We already have accident catching tools for C. What C doesn't have is a helmet, training wheels and padded walls, which is by design.

Re:Some mistakes Rust doesn't catch by couchand on Tuesday February 08, 2022 @09:34AM (#30258814)
In my experience, people who shoot themselves in the foot with a magnetized needle are the same people who will shoot themselves in the foot with any other programming technique.

Those who aren't careful enough to only flip the bits they intend to when programming with a magnetized needle are the same types of people who overflow buffers in C.

You can't fix lack of knowledge with a compiler. You can only catch accidental mistakes, but that assumes that the norm is for the person to know what they are doing and a lot of people have only superficial knowledge of the tools they are using.

We already have accident catching tools for magnetized needles. What a magnetic needle doesn't have is a helmet, training wheels and padded walls, which is by design.

Re:Some mistakes Rust doesn't catch by 10x-dev on Tuesday February 08, 2022 @01:24PM (#30262302)
It's telling that you chose to omit my memory leak example. Weak developers blame the tools.
Re:Some mistakes Rust doesn't catch by catlifeonmars on Tuesday February 08, 2022 @02:01PM (#30262860)
Quoting from the article:

> AS IT TURNS OUT, good craftspeople don't shy away from questioning, criticizing, and trying to improve their tools (or even switching to other tools entirely!)

C is the best tool for a lot of use cases, but no tool can be a perfect fit for every use case. This is the reason that multiple programming languages exist and the world has not converged on using only C everywhere. Also, there are properties not intrinsic to the language that also affect the fit.

Re:Some mistakes Rust doesn't catch by 10x-dev on Tuesday February 08, 2022 @08:59PM (#30267700)
Saying "it's easy to shoot yourself in the foot with C" is not constructive criticism because what's easy for you could be difficult for me.

If the Rust fanboy brigade could articulate actual criticism for C, such as the lack of imports, or eskewing the type system with void*, or the signedness of char, or any of the other topics beaten to death in online "C gotchas" power point presentations, then we could have a constructive conversation amongst craftspeople.

Re:Some mistakes Rust doesn't catch by Yoric on Wednesday February 09, 2022 @12:25AM (#30269195)
I believe that Rust users are past caring about that.

If you feel that C is a good language for your needs, then by all means, use it.

I know that Rust brings me a very efficient safety net that lets me concentrate on the algorithm [1] without having to also spend quite as much time auditing, fuzzing, valgrinding and {thread, memory, ...} sanitizing my code. In addition to the safety added by the type-system, I also enjoy the convenience of tools such as `Drop`, or the ability to derive (de)serializers for most formats, etc.

This is not about criticizing C. This is about picking the best tool for the job. Sometimes, it is Rust. Sometimes, it is TypeScript. I haven't encountered a job for which C was the best tool for me in a while. I'm sure that there are cases, mind you, but I don't encounter them anymore.

[1] With caveats, of course. As all Rust users, I need to please the borrow-checker gods.

Re:Some mistakes Rust doesn't catch by catlifeonmars on Tuesday February 08, 2022 @11:14PM (#30268726)
> Saying "it's easy to shoot yourself in the foot with C" is not constructive criticism because what's easy for you could be difficult for me.

That’s a fair point. Unfortunately I think that in order to really talk about the pros and cons, we’d need a relatively specific situation. Toy examples are just that… toy examples. FWIW most of the time I will make language choices based on the expected lifetime of a particular project, the relative expertise of team members working on a project, and the ease of delivering bug fixes.

Re:Some mistakes Rust doesn't catch by catlifeonmars on Tuesday February 08, 2022 @09:04AM (#30258473)
> You can't fix lack of knowledge with a compiler.

The original article and the Rust language would beg to differ. The compiler generates detailed, didactic error messages with actionable suggestions.

Re:Some mistakes Rust doesn't catch by bluGill on Tuesday February 08, 2022 @09:25AM (#30258712)
Note that rust doesn't claim to catch all errors, and the article points out places where it doesn't. In rust it is a lot harder to get into those situations accidentally, but you can.
Re:Some mistakes Rust doesn't catch by cfcosta on Tuesday February 08, 2022 @12:05PM (#30261016)
Most of the incorrect code on the article for rust is quite contrived though. It's not perfect, just better, or more strict.
Re:Some mistakes Rust doesn't catch by bluGill on Tuesday February 08, 2022 @04:41PM (#30265156)
Any non contrived example would be thousands of lines of code. Rust is meant for those areas where you are forced to do weird things, it does it's best but they don't know how to solve the problem of what they need in a way that can't be misused.

Seeing how contrived the examples are makes me more interested in rust.

Re:Some mistakes Rust doesn't catch by cfcosta on Wednesday February 09, 2022 @03:10PM (#30277785)
I use it professionally, and can say: it's worth it. Even the warts are minor compared to how polished and intuitive the language feels after you accept it's dogmas (or die fighting against the borrow checker).

The error messages are very good, cargo is awesome, rust analyzer is already good but still needs some time to flourish, and the performance in production is really predictable. It's for sure not perfect, but in many aspects it feels like a step forward, so much that sometimes I'm wary on how much I'm relying on the compiler to check my decisions for me.

Re:Some mistakes Rust doesn't catch by 10x-dev on Tuesday February 08, 2022 @01:36PM (#30262483)
So does clang and GCC at this point.
Re:Some mistakes Rust doesn't catch by catlifeonmars on Tuesday February 08, 2022 @01:51PM (#30262699)
Yes, that supports my point that it’s a useful and helpful thing to add to a compiler.
Re:Some mistakes Rust doesn't catch by eklavya on Tuesday February 08, 2022 @03:36AM (#30256125)
Sure and sometimes it directly tells your users, bypassing you entirely :)
Re:Some mistakes Rust doesn't catch by mlindner on Saturday February 12, 2022 @02:45AM (#30310770)
> The thing is, C also makes me aware of what is happening, by letting programs fail when I forget about certain things.

That isn't true at all. The majority of my and other's bugs I hit working with C (I did C for several years at a large company) were because they explicitly did not fail when people forgot things. If you iteratively learn C you learn tons of bad behaviors as C does not tell you when things are broken.