Skip to content

Commit

Permalink
Fix Bugzilla 24622 - Modify const data with void[] concatenation/appe…
Browse files Browse the repository at this point in the history
…nd (#16606)
  • Loading branch information
ntrel authored Jun 21, 2024
1 parent 9773c41 commit 3949a4d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
6 changes: 5 additions & 1 deletion compiler/src/dmd/expressionsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -11873,6 +11873,8 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
(tb2.ty == Tarray || tb2.ty == Tsarray) &&
(exp.e2.implicitConvTo(exp.e1.type) ||
(tb2.nextOf().implicitConvTo(tb1next) &&
// Do not strip const(void)[]
(!global.params.fixImmutableConv || tb1next.ty != Tvoid) &&
(tb2.nextOf().size(Loc.initial) == tb1next.size(Loc.initial)))))
{
// EXP.concatenateAssign
Expand Down Expand Up @@ -12622,7 +12624,9 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
exp.type = tb.nextOf().arrayOf();
if (exp.type.ty == Tarray && tb1next && tb2next && tb1next.mod != tb2next.mod)
{
exp.type = exp.type.nextOf().toHeadMutable().arrayOf();
// Do not strip const(void)[]
if (!global.params.fixImmutableConv || tb.nextOf().ty != Tvoid)
exp.type = exp.type.nextOf().toHeadMutable().arrayOf();
}
if (Type tbn = tb.nextOf())
{
Expand Down
21 changes: 21 additions & 0 deletions compiler/test/fail_compilation/void_cat.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
REQUIRED_ARGS: -preview=fixImmutableConv
TEST_OUTPUT:
---
fail_compilation/void_cat.d(15): Error: cannot copy `const(void)[]` to `void[]`
fail_compilation/void_cat.d(15): Source data has incompatible type qualifier(s)
fail_compilation/void_cat.d(15): Use `cast(void[])` to force copy
fail_compilation/void_cat.d(19): Error: cannot append type `const(void)[]` to type `void[]`
---
*/

void g(int*[] a, const(int*)[] b) @system
{
void[] va = a;
va[] = va.init ~ b; // a now contains b's data
*a[0] = 0; // modify const data

const(void)[] vb = b;
va ~= vb; // also leaks const pointers into void[]
// va could be copied into `a` via another void[]
}

0 comments on commit 3949a4d

Please sign in to comment.