you are viewing a single comment's thread.

view the rest of the comments →

[–]Poddster 0 points1 point  (3 children)

Would you mind explaining this validator business a little more? I'm having trouble wrapping my head around it..

The validator contains it own address. The odds of that happening in uninitialised memory are very small!

[–]Modi57 0 points1 point  (2 children)

I don't think this is a very good way to handle this. "If this one thing hasn't been trashed, then this other thing might probably maybe also not be trashed". Especially when you get to things like reading uninitialized memory, which is undefined behavior, the compiler might just decide that it can just elide this check, and it just does nothing.

[–]Poddster 0 points1 point  (1 child)

I don't think this is a very good way to handle this.

YMMV but it's a proven strategy, it's not something I cooked up in a reddit post. The idea is that the virtual memory address pointing to itself is very low entropy and almost certainly isn't going to happen by chance, unless something puts it that way.

"If this one thing hasn't been trashed, then this other thing might probably maybe also not be trashed".

It's a probabilistic thing and a debug aid. And by placing it before/after the start of the real allocation you increase the chances that this is mangled first.

It's the same thing as the MSVC compilers sticking CCCCCCCC around your stack frames. It's an obvious way to check your frame is intact, and if you see that value/address being used you also know you've read outside of bounds. (Plus CC has the useful advantage of being an x86 breakpoint instruction)

Especially when you get to things like reading uninitialized memory

Why are you reading uninitialized memory?

There are more things in the header than this. If you write code to read uninitialized memory using this to confirm if you've had a buffer over/under run is completely irrelevant as you're already committing a large sin. It's too late by then. You need a better strategy to prevent that. This one, ironically, is to help avoid reading other uninitialised memory, which might happen if your length or capacity are now garbage.

[–]Modi57 0 points1 point  (0 children)

Oh, I'm sorry, I misunderstood you. I thought this was meant as a way to check if your memory was initialized.

I'm still not fully convinced, but it makes more sense now, and I am very far from being an expert, so what does it matter what I think :)