News for hackers. Stuff that matters.
Re:Fixing a memory leak of xmlEntityPtr in librsvg by akira2501 on Sunday June 23, 2024 @05:42PM (#40770819)
Tangentially.. this is one of the things I don't like about a lot "newer" languages, like Rust and Zig. They seem to really love adding these single character sigils to the language that drastically change the meaning of a line of code or maybe an entire function.

As I get older my eyes strain more and all of this power packed into a single character really just puts me off. It seems like a strategy to emphasize writing code quickly rather than correctly, which is odd, given that this is opposite to the value proposition these languages purport to bring.

Re:Fixing a memory leak of xmlEntityPtr in librsvg by filmor on Sunday June 23, 2024 @05:50PM (#40770888)
The ? is not really the issue here. Rust is similar to C++ in that it encourages implicit resource management using scopes, so if you have resources that have to be free'd, you have to implement Drop somewhere.

The code would have looked just as correct and would have been just as wrong with the "old" non-sigil `try!(...)` syntax.

Re:Fixing a memory leak of xmlEntityPtr in librsvg by akira2501 on Sunday June 23, 2024 @06:01PM (#40770971)
> The ? is not really the issue here.

Based upon the way the code was written, it was, at least in the mind of the author. They forgot they could exit scope there. They clearly didn't _intend_ for that outcome, but ended up with it anyways, possibly out of habit, and possibly because a single impactful sigil like that is easy to miss in review.

I get that technically it didn't cause the memory leak.. but just look at the way that was written initially... it obviously led to it _within_ that particular structure.

Re:Fixing a memory leak of xmlEntityPtr in librsvg by nemetroid on Sunday June 23, 2024 @06:26PM (#40771137)
> They forgot they could exit scope there.

Their mistake was writing resource management code that requires manually thinking about where they might exit scope.

Re:Fixing a memory leak of xmlEntityPtr in librsvg by oguz-ismail on Monday June 24, 2024 @12:51AM (#40772730)
Just curious, would writing it in a better language have prevented this?
Re:Fixing a memory leak of xmlEntityPtr in librsvg by Dx5IQ on Monday June 24, 2024 @01:44AM (#40772907)
This is a classic problem you can make in any language. If you were doing Java, and forgot to use `finally` for a resource, you'd have a leak due to an exception.

I do not know what metric you use to define "better"

Re:Fixing a memory leak of xmlEntityPtr in librsvg by oguz-ismail on Monday June 24, 2024 @01:48AM (#40772917)
`finally' is a word though, harder to forget than a single punctuation mark.
Re:Fixing a memory leak of xmlEntityPtr in librsvg by filmor on Monday June 24, 2024 @08:01AM (#40774988)
The dev did not "forget a single punctuation mark". It was there and it was correct. What was /not/ correct is that he did not implement any form of implicit resource freeing in an RAII-language. Doing resource management using scopes is a choice. It's not inherently better or worse than explicit resource management with something like Go's `defer`. But if that choice is made, as in Rust or C++, you have to ensure that the implicit resource management is correctly implemented (Drop in Rust, destructors in C++). Same as in Go, where you have to ensure that you call `defer` with the correct cleanup function or in C# where you have to use `using` on your disposables, etc.
Re:Fixing a memory leak of xmlEntityPtr in librsvg by nemetroid on Monday June 24, 2024 @05:16AM (#40773918)
Slightly tongue in cheek answer: it is easy to write this kind of code in C++ without having C involved, hence style guides will usually have a prominent guideline specifically against this type of code[1].

In Rust, I think you only really run into this issue when interacting with C (or otherwise engaging in unsafe code), so for normal Rust coding it doesn't need to be spelled out as a guideline. And the Rustonomicon[2], the go-to resource for unsafe Rust, isn't really written as a set of guidelines. At least from a brief search, I found it harder to find a Rust page that specifically says "don't do this".

1: E.g. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines..., the first guideline in the "resource management" section.

2: https://doc.rust-lang.org/nomicon/intro.html

Re:Fixing a memory leak of xmlEntityPtr in librsvg by Panzer04 on Sunday June 23, 2024 @08:30PM (#40771796)
fwiw, I have to agree that having "?" at the end of a call act (as I parse the explanation) as a "return" statement seems like a very odd and deceptive semantic to have (and as you say, harder to spot).

It seems to derive from the same reason why breaks and goto are generally frowned upon if not used judiciously - they lead to unexpected control flow.

Re:Fixing a memory leak of xmlEntityPtr in librsvg by dralley on Sunday June 23, 2024 @11:08PM (#40772402)
It's not comparable to "goto" for the same reason that an early return isn't comparable to "goto". It's the furthest thing from "unexpected" control flow if you know what it does. And unlike "break" you can't accidentally mess it up. You HAVE to handle that error one way or another, and ? means "early return".

I can understand it not being intuitive to someone who has never seen it before, but that's purely a familiarity thing.

Re:Fixing a memory leak of xmlEntityPtr in librsvg by wizardforhire on Monday June 24, 2024 @02:00AM (#40772961)
I really want to upvote you but you're at 13377 karma and I would hate to fuck that up!
Re:Fixing a memory leak of xmlEntityPtr in librsvg by Panzer04 on Monday June 24, 2024 @03:25AM (#40773327)
Handling it with early return in this way still seems odd. I guess if everything is built to be done with automatic handling via scope, I can see why this way is OK. The way this would be done in my experience is just checking a return code, which feels like it's a bit more explicit if you want to early-return.

Still, lots of incorrect code has been written with that pattern as well due to incomplete cleanup, so it's not really fair to blame this syntax for it XD.

(I think most mechanisms that can jump far, including returns, need to be used carefully - nesting a return deep into a bunch of loops is just as bad)

Re:Fixing a memory leak of xmlEntityPtr in librsvg by cobbal on Sunday June 23, 2024 @08:47PM (#40771853)
You should see how much difference adding or removing a '*' can make in C when setting something to 0. I've written that bug before.
Re:Fixing a memory leak of xmlEntityPtr in librsvg by dralley on Sunday June 23, 2024 @11:07PM (#40772400)
Or putting an N byte string into an N byte buffer (forgetting to leave room for the null terminator)
Re:Fixing a memory leak of xmlEntityPtr in librsvg by jeffrallen on Monday June 24, 2024 @02:23AM (#40773036)
That's not a fair comparison though: you can't even see that single character! /s
Re:Fixing a memory leak of xmlEntityPtr in librsvg by qouteall on Tuesday June 25, 2024 @03:41AM (#40785605)
Agree. Same applies to '!'. '!' is small compared to `== false` and tend to get missed. The IDE should highlight '!' and '?' in Rust.

On the contrary, IDE should fold 'if err != nil' in golang code to make meaningful code more visible.