From 6b5b6f1a589cf6f10f0282c248e6ec9c616b6397 Mon Sep 17 00:00:00 2001 From: RazvanN7 Date: Tue, 10 Nov 2020 12:26:50 +0800 Subject: [PATCH] Fix Issue 21110 - OOB memory access, safety violation --- src/core/internal/array/operations.d | 33 ++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/core/internal/array/operations.d b/src/core/internal/array/operations.d index 0e38d2cefa..3e2331484b 100644 --- a/src/core/internal/array/operations.d +++ b/src/core/internal/array/operations.d @@ -38,6 +38,14 @@ T[] arrayOp(T : T[], Args...)(T[] res, Filter!(isType, Args) args) @trusted @nog alias scalarizedExp = staticMap!(toElementType, Args); alias check = typeCheck!(true, T, scalarizedExp); // must support all scalar ops + foreach (argsIdx, arg; typeof(args)) + { + static if (is(arg == U[], U)) + { + assert(res.length == args[argsIdx].length, "Mismatched array lengths for vector operation"); + } + } + size_t pos; static if (vectorizeable!(T[], Args)) { @@ -635,3 +643,28 @@ unittest result[] = -data[]; assert(result[0] == -0.5); } + +// https://issues.dlang.org/show_bug.cgi?id=21110 +unittest +{ + import core.exception; + + static void assertThrown(T : Throwable, E)(lazy E expression, string msg) + { + try + expression; + catch (T) + return; + assert(0, "msg"); + } + + int[] dst; + int[] a; + int[] b; + a.length = 3; + b.length = 3; + dst.length = 4; + + void func() { dst[] = a[] + b[]; } + assertThrown!AssertError(func(), "Array operations with mismatched lengths must throw an error"); +}