Skip to content

Commit cd9fa3f

Browse files
committed
8353439: Shell grouping of -XX:OnError= commands is surprising
Reviewed-by: dholmes, stuefe
1 parent a1d566c commit cd9fa3f

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

src/hotspot/share/utilities/vmError.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,9 @@ static const char* env_list[] = {
136136
nullptr // End marker.
137137
};
138138

139-
// A simple parser for -XX:OnError, usage:
139+
// A simple parser for lists of commands such as -XX:OnError and -XX:OnOutOfMemoryError
140+
// Command list (ptr) is expected to be a sequence of commands delineated by semicolons and/or newlines.
141+
// Usage:
140142
// ptr = OnError;
141143
// while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr) != nullptr)
142144
// ... ...
@@ -145,13 +147,13 @@ static char* next_OnError_command(char* buf, int buflen, const char** ptr) {
145147

146148
const char* cmd = *ptr;
147149

148-
// skip leading blanks or ';'
149-
while (*cmd == ' ' || *cmd == ';') cmd++;
150+
// skip leading blanks, ';' or newlines
151+
while (*cmd == ' ' || *cmd == ';' || *cmd == '\n') cmd++;
150152

151153
if (*cmd == '\0') return nullptr;
152154

153155
const char * cmdend = cmd;
154-
while (*cmdend != '\0' && *cmdend != ';') cmdend++;
156+
while (*cmdend != '\0' && *cmdend != ';' && *cmdend != '\n') cmdend++;
155157

156158
Arguments::copy_expand_pid(cmd, cmdend - cmd, buf, buflen);
157159

test/hotspot/jtreg/runtime/ErrorHandling/TestOnError.java

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2025, 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
@@ -40,7 +40,10 @@ public class TestOnError {
4040

4141
public static void main(String[] args) throws Exception {
4242
String msg = "Test Succeeded";
43+
String msg1 = "OnError Test Message1";
44+
String msg2 = "OnError Test Message2";
4345

46+
// Basic OnError test:
4447
ProcessBuilder pb = ProcessTools.createLimitedTestJavaProcessBuilder(
4548
"-XX:-CreateCoredumpOnCrash",
4649
"-XX:ErrorHandlerTest=14", // trigger potential SEGV
@@ -59,6 +62,30 @@ public static void main(String[] args) throws Exception {
5962
both get written to stdout.
6063
*/
6164
output.stdoutShouldMatch("^" + msg); // match start of line only
65+
66+
// Test multiple OnError arguments:
67+
pb = ProcessTools.createLimitedTestJavaProcessBuilder(
68+
"-XX:-CreateCoredumpOnCrash",
69+
"-XX:ErrorHandlerTest=14",
70+
"-XX:OnError=echo " + msg1,
71+
"-XX:OnError=echo " + msg2,
72+
TestOnError.class.getName());
73+
74+
output = new OutputAnalyzer(pb.start());
75+
output.stdoutShouldMatch("^" + msg1);
76+
output.stdoutShouldMatch("^" + msg2);
77+
78+
// Test one argument with multiple commands using ; separator:
79+
pb = ProcessTools.createLimitedTestJavaProcessBuilder(
80+
"-XX:-CreateCoredumpOnCrash",
81+
"-XX:ErrorHandlerTest=14",
82+
"-XX:OnError=echo " + msg1 + ";echo " + msg2,
83+
TestOnError.class.getName());
84+
85+
output = new OutputAnalyzer(pb.start());
86+
output.stdoutShouldMatch("^" + msg1);
87+
output.stdoutShouldMatch("^" + msg2);
88+
6289
System.out.println("PASSED");
6390
}
6491
}

0 commit comments

Comments
 (0)