Skip to content

Commit 8737a8c

Browse files
author
Alexey Semenyuk
committed
8373448: jpackage: StackOverflowError when processing a very long argument
Reviewed-by: almatvee
1 parent 6d1bfdf commit 8737a8c

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

src/jdk.jpackage/share/classes/jdk/jpackage/internal/cli/StandardOption.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2025, 2026, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -727,8 +727,15 @@ private static final class Arguments {
727727
//
728728

729729
// regexp for parsing args (for example, for additional launchers)
730-
private static Pattern pattern = Pattern.compile(
731-
"(?:(?:([\"'])(?:\\\\\\1|.)*?(?:\\1|$))|(?:\\\\[\"'\\s]|[^\\s]))++");
730+
private static Pattern PATTERN = Pattern.compile(String.format(
731+
"(?:(?:%s|%s)|(?:\\\\[\"'\\s]|\\S))++",
732+
createPatternComponent('\''),
733+
createPatternComponent('\"')));
734+
735+
private static String createPatternComponent(char quoteChar) {
736+
var str = Character.toString(quoteChar);
737+
return String.format("(?:%s(?:\\\\%s|[^%s])*+(?:%s|$))", str, str, str, str);
738+
}
732739

733740
static List<String> getArgumentList(String inputString) {
734741
Objects.requireNonNull(inputString);
@@ -741,7 +748,7 @@ static List<String> getArgumentList(String inputString) {
741748
// The "pattern" regexp attempts to abide to the rule that
742749
// strings are delimited by whitespace unless surrounded by
743750
// quotes, then it is anything (including spaces) in the quotes.
744-
Matcher m = pattern.matcher(inputString);
751+
Matcher m = PATTERN.matcher(inputString);
745752
while (m.find()) {
746753
String s = inputString.substring(m.start(), m.end()).trim();
747754
// Ensure we do not have an empty string. trim() will take care of

test/jdk/tools/jpackage/junit/share/jdk.jpackage/jdk/jpackage/internal/cli/StandardOptionTest.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2025, 2026, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -341,9 +341,33 @@ private static Collection<Arguments> test_ARGUMENTS() {
341341
Arguments.of("abc", List.of("abc")),
342342
Arguments.of("a b c", List.of("a", "b", "c")),
343343
Arguments.of("a=10 -Dorg.acme.name='John Smith' c=\\\"foo\\\"", List.of("a=10", "-Dorg.acme.name=John Smith", "c=\"foo\"")),
344+
Arguments.of(" foo \"a b c\" v=' John Smith ' 'H e ll o' ", List.of("foo", "a b c", "v= John Smith ", "H e ll o")),
344345
Arguments.of("\"\"", List.of("")),
345346
Arguments.of(" ", List.of()),
346-
Arguments.of("", List.of())
347+
Arguments.of(" ", List.of()),
348+
Arguments.of(" foo ", List.of("foo")),
349+
Arguments.of("", List.of()),
350+
Arguments.of("'fo\"o'\\ buzz \"b a r\"", List.of("fo\"o\\ buzz", "b a r")),
351+
Arguments.of("a\\ 'b\"c'\\ d", List.of("a\\ b\"c\\ d")),
352+
Arguments.of("\"a 'bc' d\"", List.of("a 'bc' d")),
353+
Arguments.of("\'a 'bc' d\'", List.of("a bc d")),
354+
Arguments.of("\"a \\'bc\\' d\"", List.of("a 'bc' d")),
355+
Arguments.of("\'a \\'bc\\' d\'", List.of("a 'bc' d")),
356+
Arguments.of("'a b c' 'd e f'", List.of("a b c", "d e f")),
357+
Arguments.of("'a b c' \"'d e f' h", List.of("a b c", "'d e f' h")),
358+
Arguments.of("'a b c' \"'d e f' \t ", List.of("a b c", "'d e f'")),
359+
Arguments.of(" a='' '' \t '\\'\\'' \"\" \"\\\"\\\"\" ", List.of("a=", "", "\'\'", "", "\"\"")),
360+
Arguments.of("' \'foo '", List.of(" foo", "")),
361+
Arguments.of("' \'foo ' bar", List.of(" foo", " bar")),
362+
Arguments.of("' \'foo\\ '", List.of(" foo\\ ")),
363+
Arguments.of("'fo\"o buzz \"b a r\"", List.of("fo\"o buzz \"b a r\"")),
364+
Arguments.of("'", List.of("")),
365+
Arguments.of("' f g ", List.of(" f g")),
366+
Arguments.of("' f g", List.of(" f g")),
367+
Arguments.of("'\\'", List.of("'")),
368+
Arguments.of("'\\' ", List.of("'")),
369+
Arguments.of("'\\' a ", List.of("' a")),
370+
Arguments.of("\"" + "\\\"".repeat(10000) + "A", List.of("\"".repeat(10000) + "A"))
347371
);
348372
}
349373

0 commit comments

Comments
 (0)