Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue 16046 - ScopedAllocator does not set prev, causing segfaults #4742

Merged
merged 1 commit into from Aug 24, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions std/experimental/allocator/building_blocks/scoped_allocator.d
Expand Up @@ -89,6 +89,8 @@ struct ScopedAllocator(ParentAllocator)
toInsert.prev = null;
toInsert.next = root;
toInsert.length = n;
assert(!root || !root.prev);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quite recently there was a longer discussion on a PR about the ~200 HALTs in Phobos that come without message, but segfault. Maybe we could tell the user at least what has gone wrong?
e.g. "Invalid allocation. Root node isn't at the top of the stack"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On top of the doubly-linked list actually ;-)
As we are at this, I'd suggest to add "Please, file an issue" or something like that, because if the assertion triggers, it's not a precondition gone wrong from the user, but rather an implementation bug.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be a regular assert, not a HALT, because it's not compile-time zero.

if (root) root.prev = toInsert;
root = toInsert;
return b;
}
Expand Down Expand Up @@ -128,6 +130,7 @@ struct ScopedAllocator(ParentAllocator)
n.prev = null;
n.next = root;
n.length = s;
if (root) root.prev = n;
root = n;
}
return result;
Expand Down Expand Up @@ -204,3 +207,15 @@ unittest
import std.experimental.allocator.gc_allocator : GCAllocator;
testAllocator!(() => ScopedAllocator!GCAllocator());
}

unittest // https://issues.dlang.org/show_bug.cgi?id=16046
{
import std.exception;
import std.experimental.allocator;
import std.experimental.allocator.mallocator;
ScopedAllocator!Mallocator alloc;
auto foo = alloc.make!int(1).enforce;
auto bar = alloc.make!int(2).enforce;
alloc.dispose(foo);
alloc.dispose(bar); // segfault here
}
Copy link
Member

@wilzbach wilzbach Aug 23, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can add another test and deallocate in non-list order?
e.g.

alloc.dispose(el2);
alloc.dispose(el3);
alloc.dispose(el1);
alloc.dispose(el4);