Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/java.base/share/classes/java/lang/ProcessHandleImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,9 @@ public Stream<ProcessHandle> descendants() {
next++;
}
}
if (count+1 >= size) {
Comment thread
Michael-Mc-Mahon marked this conversation as resolved.
Outdated
break;
}
ppid = pids[++count]; // pick up the next pid to scan for
ppStart = starttimes[count]; // and its start time
} while (count < next);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tried } while (count < next && count < (size -1)) to avoid the break out of the loop.

Copy link
Copy Markdown
Member Author

@Michael-Mc-Mahon Michael-Mc-Mahon Apr 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tried } while (count < next && count < (size -1)) to avoid the break out of the loop.

It has already dereferenced the invalid index at that point. It might be possible to restructure the loop by putting the assignment to ppid and ppStart at the top, but the initial value for ppid is not taken from the array, which is probably why that code is at the end of the loop.

Expand Down
45 changes: 45 additions & 0 deletions test/jdk/java/lang/ProcessHandle/PidZero.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2026, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

/*
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this have a @bug tag?

* @test
* @bug 8381567
* @summary ProcessHandle.descendants fails with AIOOE for process 0
* @requires os.family == "mac"
* @run junit PidZero
*/
public class PidZero {
/**
* Test of ProcessHandle.descendants for pid 0
* Only Macos allows a ProcessHandle to be instantiated for pid 0
* which includes all processes on the system, but the implementation
* throws ArrayIndexOutOfBoundException without this fix.
*/
@Test
public void test() {
assertDoesNotThrow(() -> ProcessHandle.of(0).orElseThrow().descendants().toList().size());
}
}