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: mdoerr
Backport-of: 48c847836e44aec2d5be4aaa3040dffc9d95fad5
  • Loading branch information
GoeLin committed Dec 18, 2023
1 parent dc374f6 commit 93f7a8c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/java.base/share/classes/java/io/Console.java
Expand Up @@ -426,6 +426,10 @@ else if (rcb[len-1] == '\n') {
System.arraycopy(rcb, 0, b, 0, len);
if (zeroOut) {
Arrays.fill(rcb, 0, len, ' ');
if (reader instanceof LineReader) {
LineReader lr = (LineReader)reader;
lr.zeroOut();
}
}
}
return b;
Expand All @@ -450,6 +454,12 @@ class LineReader extends Reader {
nextChar = nChars = 0;
leftoverLF = false;
}
public void zeroOut() throws IOException {
if (in instanceof StreamDecoder) {
StreamDecoder sd = (StreamDecoder)in;
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 @@ -32,6 +32,7 @@
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;
import java.util.Arrays;

public class StreamDecoder extends Reader
{
Expand Down Expand Up @@ -199,6 +200,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 93f7a8c

@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.