changeset 58311:79371dab85c2

8240629: argfiles parsing broken for argfiles with comment cross 4096 bytes chunk Reviewed-by: alanb, mchung
author henryjen
date Fri, 06 Mar 2020 13:48:16 -0800
parents 0238badf51bc
children 20023740a683
files src/java.base/share/native/libjli/args.c test/jdk/tools/launcher/ArgFileSyntax.java
diffstat 2 files changed, 30 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/native/libjli/args.c	Mon Mar 09 22:31:12 2020 +0800
+++ b/src/java.base/share/native/libjli/args.c	Fri Mar 06 13:48:16 2020 -0800
@@ -218,11 +218,12 @@
         } else if (pctx->state == IN_COMMENT) {
             while (ch != '\n' && ch != '\r') {
                 nextc++;
-                if (nextc > eob) {
+                if (nextc >= eob) {
                     return NULL;
                 }
                 ch = *nextc;
             }
+            anchor = nextc + 1;
             pctx->state = FIND_NEXT;
             continue;
         }
@@ -258,6 +259,7 @@
                     continue;
                 }
                 pctx->state = IN_COMMENT;
+                anchor = nextc + 1;
                 break;
             case '\\':
                 if (pctx->state != IN_QUOTE) {
@@ -293,9 +295,12 @@
     }
 
     assert(nextc == eob);
-    if (anchor != nextc) {
-        // not yet return until end of stream, we have part of a token.
-        JLI_List_addSubstring(pctx->parts, anchor, nextc - anchor);
+    // Only need partial token, not comment or whitespaces
+    if (pctx->state == IN_TOKEN || pctx->state == IN_QUOTE) {
+        if (anchor < nextc) {
+            // not yet return until end of stream, we have part of a token.
+            JLI_List_addSubstring(pctx->parts, anchor, nextc - anchor);
+        }
     }
     return NULL;
 }
--- a/test/jdk/tools/launcher/ArgFileSyntax.java	Mon Mar 09 22:31:12 2020 +0800
+++ b/test/jdk/tools/launcher/ArgFileSyntax.java	Fri Mar 06 13:48:16 2020 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -23,7 +23,7 @@
 
 /**
  * @test
- * @bug 8027634 8210810
+ * @bug 8027634 8210810 8240629
  * @summary Verify syntax of argument file
  * @build TestHelper
  * @run main ArgFileSyntax
@@ -36,7 +36,6 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 public class ArgFileSyntax extends TestHelper {
@@ -213,8 +212,26 @@
         scratch.add(bag + "'" + filling + "\\\\aaa\\\\'");
         scratch.add(ver);
         rv.add(List.of(scratch, List.of(bag + filling + "\\aaa\\", ver)));
+        return rv;
+    }
 
-        return rv;
+    // 8240629: end or start comment at boundary
+    @Test
+    public void test8240629() throws IOException {
+        char[] data = new char[ARG_FILE_PARSER_BUF_SIZE];
+        data[0] = '#';
+        Arrays.fill(data, 1, data.length, '0');
+
+        int need = ARG_FILE_PARSER_BUF_SIZE - System.lineSeparator().length();
+        // Comment end before, at, after boundary
+        for (int count = need - 1; count <= need + 1 ; count++) {
+            String commentAtBoundary = String.valueOf(data, 0, count);
+            List<String> content = new ArrayList<>();
+            content.add(commentAtBoundary);
+            content.add("# start a new comment at boundary");
+            content.add("-Dfoo=bar");
+            verifyParsing(content, List.of("-Dfoo=bar"));
+        }
     }
 
     // ensure the arguments in the file are read in correctly