Skip to content

Commit

Permalink
8240629: argfiles parsing broken for argfiles with comment cross 4096…
Browse files Browse the repository at this point in the history
… bytes chunk

Reviewed-by: alanb, mchung
  • Loading branch information
slowhog committed Mar 6, 2020
1 parent aa2be11 commit dc17821
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
13 changes: 9 additions & 4 deletions src/java.base/share/native/libjli/args.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,12 @@ static char* nextToken(__ctx_args *pctx) {
} 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;
}
Expand Down Expand Up @@ -258,6 +259,7 @@ static char* nextToken(__ctx_args *pctx) {
continue;
}
pctx->state = IN_COMMENT;
anchor = nextc + 1;
break;
case '\\':
if (pctx->state != IN_QUOTE) {
Expand Down Expand Up @@ -293,9 +295,12 @@ static char* nextToken(__ctx_args *pctx) {
}

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;
}
Expand Down
25 changes: 21 additions & 4 deletions test/jdk/tools/launcher/ArgFileSyntax.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -23,7 +23,7 @@

/**
* @test
* @bug 8027634 8210810
* @bug 8027634 8210810 8240629
* @summary Verify syntax of argument file
* @build TestHelper
* @run main ArgFileSyntax
Expand All @@ -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 {
Expand Down Expand Up @@ -213,10 +212,28 @@ public List<List<List<String>>> loadCases() {
scratch.add(bag + "'" + filling + "\\\\aaa\\\\'");
scratch.add(ver);
rv.add(List.of(scratch, List.of(bag + filling + "\\aaa\\", ver)));

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
private void verifyParsing(List<String> lines, List<String> args) throws IOException {
File argFile = createArgFile(lines);
Expand Down

0 comments on commit dc17821

Please sign in to comment.