Bug in the borrow-checker?

What's related here:

is pretty scary.

Apologies that this is a members-only story. I think I'll past the pertinent part here:

What the Hell Is a Lifetime Bug?

Rust has this thing called lifetimes.

They’re supposed to help you prove to the compiler that your references are valid. It’s like having a friend constantly look over your shoulder and slap your hand if you’re about to dereference a dangling pointer.

Most of the time, lifetimes protect you.
Sometimes, they do exactly what you ask.
And that’s where the danger begins.

We had code like this (simplified):

use std::collections::HashMap;

struct UserRef<'a> {
    name: &'a str,
    email: &'a str,
}

fn get_user<'a>(store: &'a HashMap<u32, (String, String)>, id: u32) -> Option<UserRef<'a>> {
    store.get(&id).map(|(name, email)| UserRef {
        name: name.as_str(),
        email: email.as_str(),
    })
}

This compiled fine. Passed unit tests.
Returned the right values in staging.
Then broke under pressure.
How I Wasted 3 Months on a Bug That Compiled

Here’s the problem.

We were returning a reference to a String that belonged to a map. But that map lived somewhere else, and under async conditions, the map could be dropped while the reference still lived.

The Rust compiler didn’t catch it, because we told it that 'a was safe.
It believed us.

But in production, under certain concurrent workloads, that reference became invalid. And Rust doesn’t null it or throw an exception.

It just gives you… empty strings.

...

The Fix That Felt Like Giving Up (But Wasn’t)

I resisted cloning for weeks. I thought:

“I’m writing in Rust! I don’t need to clone!”

Eventually, I tried this:

struct User {
    name: String,
    email: String,
}

fn get_user(store: &HashMap<u32, (String, String)>, id: u32) -> Option<User> {
    store.get(&id).map(|(name, email)| User {
        name: name.clone(),
        email: email.clone(),
    })
}

Boom. Bug gone. Clean output. No more ghost responses.

Memory usage increased by ~0.3% on stress tests.
That was it.

I don't have access to the article, but that sounds like they used unsafe somewhere.

1 Like

Surely the bug is not in the quoted code.

3 Likes

That article was so devoid of actual information that it wasn't even worth pirating it. Somehow they get empty strings with their broken version, but even with UB this is impossible as the reference stores the length of the string which would have lead to garbage data being read.

On second thought the article uses quite some em-dashes an one instance of "not just, but" and multiple sentences that are structured similarly.

I'm just gonna call it AI slop

I just looked at their medium history. jup more than one article every day about some clickbait topic. Very reliable author.

3 Likes

I think you're right.

I shouldn't have assumed that someone would not publish an article like this unless the issue were real.

I tried to replicate what he described and sure could not. Borrow-checker will not allow that with anything I could think of!

2 Likes

Well, we have a certain answer! I asked in a comment if he had any unsafe code anywhere, and he DELETED IT AND BLOCKED ME. Like, instantly. Within two minutes.

Apologies for the thread. Again, I just assumed that nobody would basically lie like this. The piece is a lie

10 Likes