From e658d4ed1271227e9bbc64672d1d3ddd7d492b87 Mon Sep 17 00:00:00 2001
From: stepan <stepan.sindelar@oracle.com>
Date: Tue, 28 Nov 2017 14:32:13 +0100
Subject: [PATCH] Prevent index out of bounds in DoubleCentre

---
 .../com/oracle/truffle/r/library/stats/DoubleCentre.java    | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/DoubleCentre.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/DoubleCentre.java
index a817389e28..6a130dbfd8 100644
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/DoubleCentre.java
+++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/DoubleCentre.java
@@ -15,6 +15,7 @@ import com.oracle.truffle.api.dsl.Specialization;
 import com.oracle.truffle.r.nodes.attributes.SpecialAttributesFunctions.GetDimAttributeNode;
 import com.oracle.truffle.r.nodes.builtin.RExternalBuiltinNode;
 import com.oracle.truffle.r.runtime.RError;
+import com.oracle.truffle.r.runtime.RError.Message;
 import com.oracle.truffle.r.runtime.data.model.RAbstractDoubleVector;
 import com.oracle.truffle.r.runtime.data.nodes.VectorAccess;
 import com.oracle.truffle.r.runtime.data.nodes.VectorAccess.RandomIterator;
@@ -33,7 +34,10 @@ public abstract class DoubleCentre extends RExternalBuiltinNode.Arg1 {
                     @Cached("createNonShared(a)") VectorReuse reuse,
                     @Cached("create()") GetDimAttributeNode getDimNode) {
         int n = getDimNode.nrows(a);
-
+        if (!getDimNode.isMatrix(a) || n != a.getLength() / n) {
+            // Note: otherwise array index out of bounds
+            throw error(Message.MUST_BE_SQUARE_MATRIX, "x");
+        }
         try (RandomIterator aIter = aAccess.randomAccess(a)) {
             RAbstractDoubleVector result = reuse.getResult(a);
             VectorAccess resultAccess = reuse.access(result);
-- 
GitLab