8320798: Console read line with zero out should zero out underlying buffer

Reviewed-by: bpb, lancea, joehw, alanb, jpai, mbaesken
This commit is contained in:
Naoto Sato 2023-12-01 17:39:11 +00:00
parent 3087e14cde
commit d568562966
2 changed files with 30 additions and 1 deletions

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -195,6 +195,9 @@ public final class JdkConsoleImpl implements JdkConsole {
System.arraycopy(rcb, 0, b, 0, len); System.arraycopy(rcb, 0, b, 0, len);
if (zeroOut) { if (zeroOut) {
Arrays.fill(rcb, 0, len, ' '); Arrays.fill(rcb, 0, len, ' ');
if (reader instanceof LineReader lr) {
lr.zeroOut();
}
} }
} }
return b; return b;
@ -228,6 +231,11 @@ public final class JdkConsoleImpl implements JdkConsole {
nextChar = nChars = 0; nextChar = nChars = 0;
leftoverLF = false; leftoverLF = false;
} }
public void zeroOut() throws IOException {
if (in instanceof StreamDecoder sd) {
sd.fillZeroToPosition();
}
}
public void close () {} public void close () {}
public boolean ready() throws IOException { public boolean ready() throws IOException {
//in.ready synchronizes on readLock already //in.ready synchronizes on readLock already

View file

@ -42,6 +42,8 @@ import java.nio.charset.CoderResult;
import java.nio.charset.CodingErrorAction; import java.nio.charset.CodingErrorAction;
import java.nio.charset.IllegalCharsetNameException; import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException; import java.nio.charset.UnsupportedCharsetException;
import java.util.Arrays;
import jdk.internal.misc.InternalLock; import jdk.internal.misc.InternalLock;
public class StreamDecoder extends Reader { public class StreamDecoder extends Reader {
@ -271,6 +273,25 @@ public class StreamDecoder extends Reader {
return !closed; return !closed;
} }
public void fillZeroToPosition() throws IOException {
Object lock = this.lock;
if (lock instanceof InternalLock locker) {
locker.lock();
try {
lockedFillZeroToPosition();
} finally {
locker.unlock();
}
} else {
synchronized (lock) {
lockedFillZeroToPosition();
}
}
}
private void lockedFillZeroToPosition() {
Arrays.fill(bb.array(), bb.arrayOffset(), bb.arrayOffset() + bb.position(), (byte)0);
}
// -- Charset-based stream decoder impl -- // -- Charset-based stream decoder impl --