From 73436cdea3a2da2334d869a0d96b1e28dfcf03d7 Mon Sep 17 00:00:00 2001 From: Florian Angerer <florian.angerer@oracle.com> Date: Tue, 13 Mar 2018 16:40:53 +0100 Subject: [PATCH] Add further verification. --- .../oracle/truffle/r/launcher/RCommand.java | 3 +- .../src/com/oracle/truffle/r/parser/R.g | 72 +++++++++++-------- 2 files changed, 44 insertions(+), 31 deletions(-) diff --git a/com.oracle.truffle.r.launcher/src/com/oracle/truffle/r/launcher/RCommand.java b/com.oracle.truffle.r.launcher/src/com/oracle/truffle/r/launcher/RCommand.java index 4fd6c52a0f..64e03cca26 100644 --- a/com.oracle.truffle.r.launcher/src/com/oracle/truffle/r/launcher/RCommand.java +++ b/com.oracle.truffle.r.launcher/src/com/oracle/truffle/r/launcher/RCommand.java @@ -285,7 +285,8 @@ public class RCommand { try { Source src; if (srcFile != null) { - src = Source.newBuilder("R", sb.toString(), srcFile.getName() + "#" + startLine).interactive(true).uri(srcFile.toURI()).buildLiteral(); + int endLine = consoleHandler.getCurrentLineIndex(); + src = Source.newBuilder("R", sb.toString(), srcFile.getName() + "#" + startLine + "-" + endLine).interactive(true).uri(srcFile.toURI()).buildLiteral(); } else { src = Source.newBuilder("R", sb.toString(), "<REPL>").interactive(true).buildLiteral(); } diff --git a/com.oracle.truffle.r.parser/src/com/oracle/truffle/r/parser/R.g b/com.oracle.truffle.r.parser/src/com/oracle/truffle/r/parser/R.g index e593e086e1..21326517ab 100644 --- a/com.oracle.truffle.r.parser/src/com/oracle/truffle/r/parser/R.g +++ b/com.oracle.truffle.r.parser/src/com/oracle/truffle/r/parser/R.g @@ -91,41 +91,53 @@ import com.oracle.truffle.r.runtime.RError; } private Source createFullSource(Source original) { - String originalName = original.getName(); + String originalName = original.getName(); - // check if source name is like 'path/to/source.R#45-54' - int hash_idx = originalName.lastIndexOf("#"); - if (hash_idx == -1) { - return original; - } + // check if source name is like 'path/to/source.R#45-54' + int hash_idx = originalName.lastIndexOf("#"); + if (hash_idx == -1) { + return original; + } - String fileName = originalName.substring(0, hash_idx); - String lineRange = originalName.substring(hash_idx + 1); + String fileName = originalName.substring(0, hash_idx); + String lineRange = originalName.substring(hash_idx + 1); + + try { + // check for line range, e.g. '45-54' + int startLine = -1; + int endLine = -1; + int dashIdx = lineRange.indexOf('-'); + if (dashIdx != -1) { + startLine = Integer.parseInt(lineRange.substring(0, dashIdx)); + endLine = Integer.parseInt(lineRange.substring(dashIdx + 1)); + } else { + startLine = Integer.parseInt(lineRange); + endLine = startLine; + } + Builder<IOException, RuntimeException, RuntimeException> newBuilder = Source.newBuilder(new File(fileName)); + if (original.isInteractive()) { + newBuilder.interactive(); + } + Source fullSource = newBuilder.build(); - try { - // check for line range, e.g. '45-54' - int startLine = -1; - int endLine = -1; - int dashIdx = lineRange.indexOf('-'); - if (dashIdx != -1) { - startLine = Integer.parseInt(lineRange.substring(0, dashIdx)); - endLine = Integer.parseInt(lineRange.substring(dashIdx + 1)); - } else { - startLine = Integer.parseInt(lineRange); - } - Builder<IOException, RuntimeException, RuntimeException> newBuilder = Source.newBuilder(new File(fileName)); - if (original.isInteractive()) { - newBuilder.interactive(); + // verify to avoid accidentally matching file names + for (int i = 0; i < endLine - startLine + 1; i++) { + if (!original.getCharacters(i + 1).equals(fullSource.getCharacters(startLine + i))) { + return original; + } + } + + fileStartOffset = -fullSource.getLineStartOffset(startLine); + return fullSource; + } catch (NumberFormatException e) { + // invalid line number + } catch (IllegalArgumentException e) { + // file name is accidentally named in the expected scheme + } catch (IOException e) { + } catch (RuntimeException e) { } - Source fullSource = newBuilder.build(); - fileStartOffset = -fullSource.getLineStartOffset(startLine); - return fullSource; - } catch (NumberFormatException e) { - } catch (IOException e) { - } catch (RuntimeException e) { + return original; } - return original; - } /** * Helper function that returns the last parsed token, usually used for building source sections. -- GitLab