Remix.run Logo
▲ 01HNNWZ0MV43FF 7 hours ago

Did you see the "Spooky C++" post? https://www.nablag.com/spooky_cpp

Did you already know that? Do all your junior C++ programmers know that? Do you have lints turned on in every C++ project and dep for that?

Cause I had to really look at it to figure it out. And I had to think, "Would Rust allow this code?"

▲William_BB 7 hours ago | parent | next [-]

I'm not sure what's so "spooky" about this example. It's really obvious if you know what `std::string_view` is. `Sanitize()` signature is fishy to begin with -- it takes a single string by const ref, but returns a string view. And I don't even consider myself very experienced in C++.

Sure, it'd be nice if the compiler didn't allow you to write this code. But there are worse examples than this out there.

▲grg0 4 hours ago | parent | next [-]

I don't think that is a fair assessment. Imagine Sanitize() and its caller are defined in different files, which is a more realistic scenario. On the one hand, you look at Sanitize's signature and it is "fine" (I agree a string_view argument would be better); it is implicitly stating that the caller is responsible for the lifetime of the argument. Separately, you look at the call and it has a good chance of passing inadvertently. Marking std::string's constructor explicit would have been a slight improvement but it still doesn't solve the problem. Overall, the fact that this code is possible and yields different results under different optimization levels is just cooked. As a programmer, I want to focus on solving problems, not on keeping my guard up at 110% and working around bad language design choices.

To reply to GP: many projects specifically avoid the STL.

▲simonask 4 hours ago | parent | prev [-]

You have to know a lot about C++ to tell at a glance what's wrong with this code. I fall in that category, and I can tell you I think that the signature of Sanitize() looks extremely reasonable!

What's completely unreasonable here is how easy it is for code that looks reasonable in C++ to be deeply dangerous. This is a perfectly normal piece of code, and it looks like the author is even conscientious about nice things like avoiding too many string copies. We shouldn't be out here defending the absolutely unhinged semantics that led to this.

C++ is so extremely hostile that people who try to write good code end up with a very defensive style of programming if they know what they're doing. In my experience, this tends to either almost fully eradicate any performance benefits they were chasing by choosing C++ in the first place, or blow up the time it takes to deliver the product.

▲William_BB 3 hours ago | parent | next [-]

I agree with your general sentiment, but imo this particular example is oversold. Maybe I'm wrong.

The difference between const ref string and string view is one of the first things you learn when you learn about `string view`.

To me, the signature of `Sanitize` is fishy because it's implied that the returned string view is of the const ref string argument. But I agree there are more than one ways to break it.

▲lollollol3 2 hours ago | parent [-]

I think it is a real issue, yet in some ways worse in Rust. Mojo likely does this way better than both Rust and C++.

An addition could be made to C++ to make this less prone to errors, but Rust cultists and Jewish pedophiles make that harder to fix, with Jewish pedophiles being entrenched in both Rust and C++ political positions as well as in Ycombinator (Epstein board member).

▲lollollol3 2 hours ago | parent | prev [-]

What is funny is that Rust has similar or worse issues regarding temporary lifetimes. Only some UB is caught (and in practice only sometimes), issues like deadlocks aren't.

https://fasterthanli.me/articles/a-rust-match-made-in-hell

Challenge to you, simonask: Explain why the curly braces here determines whether there is a deadlock or not. Bonus points if you do so without asking for help or looking anything up.

https://play.rust-lang.org/?version=stable&mode=debug&editio...

https://play.rust-lang.org/?version=stable&mode=debug&editio...

▲nwallin 4 hours ago | parent | prev | next [-]

I mean, you could just turn warnings on: https://godbolt.org/z/ns6cb9T6q

But yes, I know that any function with the signature `std::string_view(const std::string&)` or especially `std::string_view(std::string)` is a land mine.

I asked a local LLM to do a code review on it. (gemma4:12b) It flagged the bug, with all caps "THIS WILL CAUSE UNDEFINED BEHAVIOR", and suggested a fix.

▲cv5005 4 hours ago | parent | prev [-]

For some reason both c++ and rust decided to go with "lets make temporaries have insanely short lifetimes! Kill them immediatly at the first semicolon we can!" and I don't know why. (rust at least makes it a compiler error to misuse them, but still annoying.)

C has a much better design with its compound literals having block scope lifetimes.