Skip to content

Commit

Permalink
8320798: Console read line with zero out should zero out underlying b…
Browse files Browse the repository at this point in the history
…uffer

Reviewed-by: mbaesken
Backport-of: d568562966e9a2020704eee3d67b8a106f647d9c
  • Loading branch information
GoeLin committed Dec 16, 2023
1 parent 5653d2d commit 48c8478
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/java.base/share/classes/java/io/Console.java
Expand Up @@ -445,6 +445,9 @@ else if (rcb[len-1] == '\n') {
System.arraycopy(rcb, 0, b, 0, len);
if (zeroOut) {
Arrays.fill(rcb, 0, len, ' ');
if (reader instanceof LineReader lr) {
lr.zeroOut();
}
}
}
return b;
Expand All @@ -469,6 +472,11 @@ class LineReader extends Reader {
nextChar = nChars = 0;
leftoverLF = false;
}
public void zeroOut() throws IOException {
if (in instanceof StreamDecoder sd) {
sd.fillZeroToPosition();
}
}
public void close () {}
public boolean ready() throws IOException {
//in.ready synchronizes on readLock already
Expand Down
11 changes: 11 additions & 0 deletions src/java.base/share/classes/sun/nio/cs/StreamDecoder.java
Expand Up @@ -42,6 +42,7 @@
import java.nio.charset.CodingErrorAction;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Arrays;

public class StreamDecoder extends Reader {

Expand Down Expand Up @@ -212,6 +213,16 @@ private boolean isOpen() {
return !closed;
}

public void fillZeroToPosition() throws IOException {
Object lock = this.lock;
synchronized (lock) {
lockedFillZeroToPosition();
}
}

private void lockedFillZeroToPosition() {
Arrays.fill(bb.array(), bb.arrayOffset(), bb.arrayOffset() + bb.position(), (byte)0);
}

// -- Charset-based stream decoder impl --

Expand Down

1 comment on commit 48c8478

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.