From f443a85b921093deceb30e0b82875d7badb1f6e7 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Mon, 4 Jun 2018 08:50:42 +0200 Subject: [PATCH] throw exception with useful message if input handler hits EOF --- WHATSNEW | 4 ++++ .../apache/tools/ant/input/DefaultInputHandler.java | 3 +++ .../org/apache/tools/ant/input/SecureInputHandler.java | 10 +++++++++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/WHATSNEW b/WHATSNEW index 9e096c9d8..699ad2304 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -28,6 +28,10 @@ Fixed bugs: * The junit task when used with includeantruntime="no" was incorrectly printing a warning about multiple versions of ant detected in path + * Default and SecureInputHandler will now raise an error when then + end of the inout stream (usually System.in or System.console) are + reached before a valid input has been read. + Other changes: -------------- diff --git a/src/main/org/apache/tools/ant/input/DefaultInputHandler.java b/src/main/org/apache/tools/ant/input/DefaultInputHandler.java index 164505f6d..fa6822327 100644 --- a/src/main/org/apache/tools/ant/input/DefaultInputHandler.java +++ b/src/main/org/apache/tools/ant/input/DefaultInputHandler.java @@ -56,6 +56,9 @@ public class DefaultInputHandler implements InputHandler { System.err.flush(); try { String input = r.readLine(); + if (input == null) { + throw new BuildException("unexpected end of stream while reading input"); + } request.setInput(input); } catch (IOException e) { throw new BuildException("Failed to read input from" diff --git a/src/main/org/apache/tools/ant/input/SecureInputHandler.java b/src/main/org/apache/tools/ant/input/SecureInputHandler.java index 51f8b95ef..7fb0c34dd 100644 --- a/src/main/org/apache/tools/ant/input/SecureInputHandler.java +++ b/src/main/org/apache/tools/ant/input/SecureInputHandler.java @@ -40,6 +40,7 @@ public class SecureInputHandler extends DefaultInputHandler { * @throws BuildException if not possible to read from console */ public void handleInput(InputRequest request) throws BuildException { + boolean nullInput = false; String prompt = getPrompt(request); try { Object console = ReflectUtil.invokeStatic(System.class, "console"); @@ -47,6 +48,10 @@ public class SecureInputHandler extends DefaultInputHandler { char[] input = (char[]) ReflectUtil.invoke( console, "readPassword", String.class, prompt, Object[].class, (Object[]) null); + if (input == null) { + nullInput = true; + break; + } request.setInput(new String(input)); /* for security zero char array after retrieving value */ java.util.Arrays.fill(input, ' '); @@ -55,5 +60,8 @@ public class SecureInputHandler extends DefaultInputHandler { /* Java6 not present use default handler */ super.handleInput(request); } + if (nullInput) { + throw new BuildException("unexpected end of stream while reading input"); + } } -} \ No newline at end of file +}