提交 3c74f3cc 编写于 作者: S snazarki

8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout...

8067796: (process) Process.waitFor(timeout, unit) doesn't throw NPE if timeout is less than, or equal to zero when unit == null
Summary: Implement checking for NPE in Process implementation before other conditions
Reviewed-by: martin, chegar, aph, andrew
上级 9458c03e
...@@ -401,12 +401,11 @@ final class UNIXProcess extends Process { ...@@ -401,12 +401,11 @@ final class UNIXProcess extends Process {
public synchronized boolean waitFor(long timeout, TimeUnit unit) public synchronized boolean waitFor(long timeout, TimeUnit unit)
throws InterruptedException throws InterruptedException
{ {
long remainingNanos = unit.toNanos(timeout); // throw NPE before other conditions
if (hasExited) return true; if (hasExited) return true;
if (timeout <= 0) return false; if (timeout <= 0) return false;
long remainingNanos = unit.toNanos(timeout);
long deadline = System.nanoTime() + remainingNanos; long deadline = System.nanoTime() + remainingNanos;
do { do {
TimeUnit.NANOSECONDS.timedWait(this, remainingNanos); TimeUnit.NANOSECONDS.timedWait(this, remainingNanos);
if (hasExited) { if (hasExited) {
......
...@@ -516,12 +516,11 @@ final class ProcessImpl extends Process { ...@@ -516,12 +516,11 @@ final class ProcessImpl extends Process {
public boolean waitFor(long timeout, TimeUnit unit) public boolean waitFor(long timeout, TimeUnit unit)
throws InterruptedException throws InterruptedException
{ {
long remainingNanos = unit.toNanos(timeout); // throw NPE before other conditions
if (getExitCodeProcess(handle) != STILL_ACTIVE) return true; if (getExitCodeProcess(handle) != STILL_ACTIVE) return true;
if (timeout <= 0) return false; if (timeout <= 0) return false;
long remainingNanos = unit.toNanos(timeout);
long deadline = System.nanoTime() + remainingNanos; long deadline = System.nanoTime() + remainingNanos;
do { do {
// Round up to next millisecond // Round up to next millisecond
long msTimeout = TimeUnit.NANOSECONDS.toMillis(remainingNanos + 999_999L); long msTimeout = TimeUnit.NANOSECONDS.toMillis(remainingNanos + 999_999L);
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
* 5026830 5023243 5070673 4052517 4811767 6192449 6397034 6413313 * 5026830 5023243 5070673 4052517 4811767 6192449 6397034 6413313
* 6464154 6523983 6206031 4960438 6631352 6631966 6850957 6850958 * 6464154 6523983 6206031 4960438 6631352 6631966 6850957 6850958
* 4947220 7018606 7034570 4244896 5049299 * 4947220 7018606 7034570 4244896 5049299
* 8067796
* @summary Basic tests for Process and Environment Variable code * @summary Basic tests for Process and Environment Variable code
* @run main/othervm/timeout=300 Basic * @run main/othervm/timeout=300 Basic
* @run main/othervm/timeout=300 -Djdk.lang.Process.launchMechanism=fork Basic * @run main/othervm/timeout=300 -Djdk.lang.Process.launchMechanism=fork Basic
...@@ -2366,6 +2367,56 @@ public class Basic { ...@@ -2366,6 +2367,56 @@ public class Basic {
p.destroy(); p.destroy();
} catch (Throwable t) { unexpected(t); } } catch (Throwable t) { unexpected(t); }
//----------------------------------------------------------------
// Check that Process.waitFor(timeout, null) throws NPE.
//----------------------------------------------------------------
try {
List<String> childArgs = new ArrayList<String>(javaChildArgs);
childArgs.add("sleep");
final Process p = new ProcessBuilder(childArgs).start();
THROWS(NullPointerException.class,
() -> p.waitFor(10L, null));
THROWS(NullPointerException.class,
() -> p.waitFor(0L, null));
THROWS(NullPointerException.class,
() -> p.waitFor(-1L, null));
// Terminate process and recheck after it exits
p.destroy();
p.waitFor();
THROWS(NullPointerException.class,
() -> p.waitFor(10L, null));
THROWS(NullPointerException.class,
() -> p.waitFor(0L, null));
THROWS(NullPointerException.class,
() -> p.waitFor(-1L, null));
} catch (Throwable t) { unexpected(t); }
//----------------------------------------------------------------
// Check that default implementation of Process.waitFor(timeout, null) throws NPE.
//----------------------------------------------------------------
try {
List<String> childArgs = new ArrayList<String>(javaChildArgs);
childArgs.add("sleep");
final Process proc = new ProcessBuilder(childArgs).start();
final DelegatingProcess p = new DelegatingProcess(proc);
THROWS(NullPointerException.class,
() -> p.waitFor(10L, null));
THROWS(NullPointerException.class,
() -> p.waitFor(0L, null));
THROWS(NullPointerException.class,
() -> p.waitFor(-1L, null));
// Terminate process and recheck after it exits
p.destroy();
p.waitFor();
THROWS(NullPointerException.class,
() -> p.waitFor(10L, null));
THROWS(NullPointerException.class,
() -> p.waitFor(0L, null));
THROWS(NullPointerException.class,
() -> p.waitFor(-1L, null));
} catch (Throwable t) { unexpected(t); }
//---------------------------------------------------------------- //----------------------------------------------------------------
// Check the default implementation for // Check the default implementation for
// Process.waitFor(long, TimeUnit) // Process.waitFor(long, TimeUnit)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册