From 21b72dea7805357b3644161d1a158c52f49d0e6e Mon Sep 17 00:00:00 2001 From: Christian Stein Date: Tue, 17 Jun 2025 14:01:06 +0000 Subject: [PATCH] 8357862: Java argument file is parsed unexpectedly with trailing comment Co-authored-by: Stuart Marks Reviewed-by: vromero --- src/java.base/share/native/libjli/args.c | 29 ++++++++++++++++------ test/jdk/tools/launcher/ArgFileSyntax.java | 15 ++++++++--- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/java.base/share/native/libjli/args.c b/src/java.base/share/native/libjli/args.c index 547b279b7de..2f8fdf0fc43 100644 --- a/src/java.base/share/native/libjli/args.c +++ b/src/java.base/share/native/libjli/args.c @@ -149,6 +149,19 @@ static void checkArg(const char *arg) { } } +static char *computeToken(JLI_List *parts, const char *anchor, const char *nextc) { + char *token; + if ((*parts)->size == 0) { + token = clone_substring(anchor, nextc - anchor); + } else { + JLI_List_addSubstring(*parts, anchor, nextc - anchor); + token = JLI_List_combine(*parts); + JLI_List_free(*parts); + *parts = JLI_List_new(4); + } + return token; +} + /* [\n\r] +------------+ +------------+ [\n\r] +---------+ IN_COMMENT +<------+ | IN_ESCAPE +---------+ @@ -246,14 +259,7 @@ static char* nextToken(__ctx_args *pctx) { // fall through case '\n': case '\r': - if (pctx->parts->size == 0) { - token = clone_substring(anchor, nextc - anchor); - } else { - JLI_List_addSubstring(pctx->parts, anchor, nextc - anchor); - token = JLI_List_combine(pctx->parts); - JLI_List_free(pctx->parts); - pctx->parts = JLI_List_new(4); - } + token = computeToken(&pctx->parts, anchor, nextc); pctx->cptr = nextc + 1; pctx->state = FIND_NEXT; return token; @@ -262,6 +268,13 @@ static char* nextToken(__ctx_args *pctx) { continue; } pctx->state = IN_COMMENT; + // return non-zero length token, terminated by the number sign + if (nextc - anchor > 0) { + token = computeToken(&pctx->parts, anchor, nextc); + pctx->cptr = nextc + 1; + return token; + } + // anchor after number sign anchor = nextc + 1; break; case '\\': diff --git a/test/jdk/tools/launcher/ArgFileSyntax.java b/test/jdk/tools/launcher/ArgFileSyntax.java index bedb0d36fc5..5145c205166 100644 --- a/test/jdk/tools/launcher/ArgFileSyntax.java +++ b/test/jdk/tools/launcher/ArgFileSyntax.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2025, 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 @@ -21,9 +21,9 @@ * questions. */ -/** +/* * @test - * @bug 8027634 8210810 8240629 + * @bug 8027634 8210810 8240629 8357862 * @summary Verify syntax of argument file * @build TestHelper * @run main ArgFileSyntax @@ -172,6 +172,15 @@ public class ArgFileSyntax extends TestHelper { "-version", "-Dcontinue.with.leadingws=Line1continue with \f and \t" } + }, + { // multiple args in one line and comments without preceding whitespace + { "-Xmx32m -XshowSettings#COMMENT 1", + "-version#COMMENT 2" + }, + { "-Xmx32m", + "-XshowSettings", + "-version" + } } };