Skip to content

memory_block_stack::owns() seems wrong? #151

@jwdevel

Description

@jwdevel

I haven't written a test to prove this yet, but just looking at the code, I see (comments added):

bool memory_block_stack::owns(const void* ptr) const noexcept
{
    auto address = static_cast<const char*>(ptr);
    for (auto cur = head_; cur; cur = cur->prev)
    {
        auto mem = static_cast<char*>(static_cast<void*>(cur));     // 'mem' is ptr to the 'node' itself (not payload area)
        if (address >= mem && address < mem + cur->usable_size)     // but we check our ptr against the payload size, not full node size!
            return true;
    }
    return false;
}

So, that calculation seems wrong to me.

As I understand it, a given node in the linked list looks like:

+----------------------+
|  node* prev          |     <- "head_" points here
+----------------------+
|  size_t usable_size  |
+----------------------+
|                      |
|  ... payload ...     |
|                      |
+----------------------+

So for instance, if we were checking ownership of the very last byte in the block, I think "owns(ptr)" would wrongly return false?

Should the condition be, instead:

    const char *usable = mem + implementation_offset();
    if (address >= usable && address < usable + cur->usable_size)
        ...

I can test further, but just asking in case there's something obvious I'm missing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions