Skip to content

Commit 48c8478

Browse files
committed
8320798: Console read line with zero out should zero out underlying buffer
Reviewed-by: mbaesken Backport-of: d568562966e9a2020704eee3d67b8a106f647d9c
1 parent 5653d2d commit 48c8478

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/java.base/share/classes/java/io/Console.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,9 @@ else if (rcb[len-1] == '\n') {
445445
System.arraycopy(rcb, 0, b, 0, len);
446446
if (zeroOut) {
447447
Arrays.fill(rcb, 0, len, ' ');
448+
if (reader instanceof LineReader lr) {
449+
lr.zeroOut();
450+
}
448451
}
449452
}
450453
return b;
@@ -469,6 +472,11 @@ class LineReader extends Reader {
469472
nextChar = nChars = 0;
470473
leftoverLF = false;
471474
}
475+
public void zeroOut() throws IOException {
476+
if (in instanceof StreamDecoder sd) {
477+
sd.fillZeroToPosition();
478+
}
479+
}
472480
public void close () {}
473481
public boolean ready() throws IOException {
474482
//in.ready synchronizes on readLock already

src/java.base/share/classes/sun/nio/cs/StreamDecoder.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.nio.charset.CodingErrorAction;
4343
import java.nio.charset.IllegalCharsetNameException;
4444
import java.nio.charset.UnsupportedCharsetException;
45+
import java.util.Arrays;
4546

4647
public class StreamDecoder extends Reader {
4748

@@ -212,6 +213,16 @@ private boolean isOpen() {
212213
return !closed;
213214
}
214215

216+
public void fillZeroToPosition() throws IOException {
217+
Object lock = this.lock;
218+
synchronized (lock) {
219+
lockedFillZeroToPosition();
220+
}
221+
}
222+
223+
private void lockedFillZeroToPosition() {
224+
Arrays.fill(bb.array(), bb.arrayOffset(), bb.arrayOffset() + bb.position(), (byte)0);
225+
}
215226

216227
// -- Charset-based stream decoder impl --
217228

0 commit comments

Comments
 (0)