From b98e677f1a8785c4f10ca8ad32a9c0bf6d91abf0 Mon Sep 17 00:00:00 2001
From: stepan <stepan.sindelar@oracle.com>
Date: Tue, 17 Oct 2017 16:31:46 +0200
Subject: [PATCH] Arguments for .Internal calls may be omitted

---
 .../com/oracle/truffle/r/nodes/builtin/InternalNode.java | 9 +++++++--
 .../r/runtime/nodes/EvaluatedArgumentsVisitor.java       | 4 +++-
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/builtin/InternalNode.java b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/builtin/InternalNode.java
index 3be03b9913..20bb4b423f 100644
--- a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/builtin/InternalNode.java
+++ b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/builtin/InternalNode.java
@@ -297,12 +297,17 @@ public abstract class InternalNode extends OperatorNode {
         @Override
         @ExplodeLoop
         protected Object[] prepareArgs(VirtualFrame frame) {
-            Object[] args = new Object[factory.getSignature().getLength()];
+            int argsLength = factory.getSignature().getLength();
+            if (arguments.length < argsLength - 1) {
+                // Note: GnuR seems to be OK with this and makes up some random values
+                throw error(Message.ARGUMENT_LENGTHS_DIFFER);
+            }
+            Object[] args = new Object[argsLength];
 
             for (int i = 0; i < args.length - 1; i++) {
                 args[i] = arguments[i].execute(frame);
             }
-            Object[] varArgs = new Object[arguments.length - (factory.getSignature().getLength() - 1)];
+            Object[] varArgs = new Object[arguments.length - (argsLength - 1)];
             for (int i = 0; i < varArgs.length; i++) {
                 varArgs[i] = arguments[args.length - 1 + i].execute(frame);
             }
diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/nodes/EvaluatedArgumentsVisitor.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/nodes/EvaluatedArgumentsVisitor.java
index fa3254ef13..6f05ebf3ca 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/nodes/EvaluatedArgumentsVisitor.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/nodes/EvaluatedArgumentsVisitor.java
@@ -106,7 +106,9 @@ public final class EvaluatedArgumentsVisitor extends RSyntaxVisitor<Info> {
                         } else {
                             assert signature.getVarArgCount() == 1 : signature;
                             assert innerArguments.length == signature.getLength() || signature.getVarArgIndex() == signature.getLength() - 1 : signature;
-                            assert innerArguments.length >= signature.getLength() - 1 : signature + " " + innerArguments.length;
+                            // holds only for well-formed code, so we cannot rely on it:
+                            // assert innerArguments.length >= signature.getLength() - 1 : signature
+                            // + " " + innerArguments.length;
                         }
                         Info info = Info.createNew();
                         for (int i = innerArguments.length - 1; i >= 0; i--) {
-- 
GitLab