Skip to content

Commit

Permalink
Merge pull request #5342 from tsbockman/issue_15645
Browse files Browse the repository at this point in the history
issue 15645 - Prevent unsafe usage of Tuple.slice
merged-on-behalf-of: Jack Stouffer <jack@jackstouffer.com>
  • Loading branch information
dlang-bot committed May 17, 2017
2 parents 22799ff + e63c620 commit a45dc66
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions std/typecons.d
Expand Up @@ -970,7 +970,7 @@ template Tuple(Specs...)
}

/**
* Takes a slice of this `Tuple`.
* Takes a slice by-reference of this `Tuple`.
*
* Params:
* from = A `size_t` designating the starting position of the slice.
Expand All @@ -982,9 +982,14 @@ template Tuple(Specs...)
* the original.
*/
@property
ref Tuple!(sliceSpecs!(from, to)) slice(size_t from, size_t to)() @trusted
ref inout(Tuple!(sliceSpecs!(from, to))) slice(size_t from, size_t to)() inout @trusted
if (from <= to && to <= Types.length)
{
static assert(
(typeof(this).alignof % typeof(return).alignof == 0) &&
(expand[from].offsetof % typeof(return).alignof == 0),
"Slicing by reference is impossible because of an alignment mistmatch. (See Phobos issue #15645.)");

return *cast(typeof(return)*) &(field[from]);
}

Expand All @@ -997,6 +1002,10 @@ template Tuple(Specs...)
auto s = a.slice!(1, 3);
static assert(is(typeof(s) == Tuple!(string, float)));
assert(s[0] == "abc" && s[1] == 4.5);

// Phobos issue #15645
Tuple!(int, short, bool, double) b;
static assert(!__traits(compiles, b.slice!(2, 4)));
}

/**
Expand Down

0 comments on commit a45dc66

Please sign in to comment.