Skip to content

Commit

Permalink
Fix issue 8580 - Peek returns an invalid pointer for structs larger t…
Browse files Browse the repository at this point in the history
…han Variant.size.

Style: Add space between if and condition.
  • Loading branch information
Kapps committed Feb 17, 2014
1 parent 3089fb2 commit 0c82698
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion std/variant.d
Expand Up @@ -669,7 +669,12 @@ public:
static if (!is(T == void))
static assert(allowed!(T), "Cannot store a " ~ T.stringof
~ " in a " ~ VariantN.stringof);
return type == typeid(T) ? cast(T*) &store : null;
if (type != typeid(T))
return null;
static if (T.sizeof <= size)
return cast(T*)&store;
else
return *cast(T**)&store;
}

/**
Expand Down Expand Up @@ -1727,6 +1732,37 @@ unittest
assert(v2() == 3);
}

// Using peek for large structs, issue 8580
unittest
{
struct TestStruct(bool pad)
{
int val1;
static if (pad)
ubyte[Variant.size] padding;
int val2;
}

void testPeekWith(T)()
{
T inst;
inst.val1 = 3;
inst.val2 = 4;
Variant v = inst;
T* original = v.peek!T;
assert(original.val1 == 3);
assert(original.val2 == 4);
original.val1 = 6;
original.val2 = 8;
T modified = v.get!T;
assert(modified.val1 == 6);
assert(modified.val2 == 8);
}

testPeekWith!(TestStruct!false)();
testPeekWith!(TestStruct!true)();
}

/**
* Applies a delegate or function to the given Algebraic depending on the held type,
* ensuring that all types are handled by the visiting functions.
Expand Down

0 comments on commit 0c82698

Please sign in to comment.