6996192: Console.readPassword race: input echo off must be prior to writing prompt
authorsherman
Fri, 11 Feb 2011 17:09:35 -0800
changeset 8389 3d0196408a7a
parent 8388 bc5ae489cd71
child 8390 f9ed509a2634
child 8392 3e5784c9f73e
6996192: Console.readPassword race: input echo off must be prior to writing prompt Summary: To turn off echo before prompt Reviewed-by: alanb
jdk/src/share/classes/java/io/Console.java
--- a/jdk/src/share/classes/java/io/Console.java	Sat Feb 12 07:30:01 2011 +0800
+++ b/jdk/src/share/classes/java/io/Console.java	Fri Feb 11 17:09:35 2011 -0800
@@ -308,17 +308,29 @@
         char[] passwd = null;
         synchronized (writeLock) {
             synchronized(readLock) {
-                if (fmt.length() != 0)
-                    pw.format(fmt, args);
                 try {
                     echoOff = echo(false);
+                } catch (IOException x) {
+                    throw new IOError(x);
+                }
+                IOError ioe = null;
+                try {
+                    if (fmt.length() != 0)
+                        pw.format(fmt, args);
                     passwd = readline(true);
                 } catch (IOException x) {
-                    throw new IOError(x);
+                    ioe = new IOError(x);
                 } finally {
                     try {
                         echoOff = echo(true);
-                    } catch (IOException xx) {}
+                    } catch (IOException x) {
+                        if (ioe == null)
+                            ioe = new IOError(x);
+                        else
+                            ioe.addSuppressed(x);
+                    }
+                    if (ioe != null)
+                        throw ioe;
                 }
                 pw.println();
             }