Skip to content

Commit e8cf56c

Browse files
committed
8320798: Console read line with zero out should zero out underlying buffer
Backport-of: d568562966e9a2020704eee3d67b8a106f647d9c
1 parent d65289d commit e8cf56c

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/java.base/share/classes/jdk/internal/io/JdkConsoleImpl.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -195,6 +195,9 @@ else if (rcb[len-1] == '\n') {
195195
System.arraycopy(rcb, 0, b, 0, len);
196196
if (zeroOut) {
197197
Arrays.fill(rcb, 0, len, ' ');
198+
if (reader instanceof LineReader lr) {
199+
lr.zeroOut();
200+
}
198201
}
199202
}
200203
return b;
@@ -228,6 +231,11 @@ class LineReader extends Reader {
228231
nextChar = nChars = 0;
229232
leftoverLF = false;
230233
}
234+
public void zeroOut() throws IOException {
235+
if (in instanceof StreamDecoder sd) {
236+
sd.fillZeroToPosition();
237+
}
238+
}
231239
public void close () {}
232240
public boolean ready() throws IOException {
233241
//in.ready synchronizes on readLock already

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
import java.nio.charset.CodingErrorAction;
4343
import java.nio.charset.IllegalCharsetNameException;
4444
import java.nio.charset.UnsupportedCharsetException;
45+
import java.util.Arrays;
46+
4547
import jdk.internal.misc.InternalLock;
4648

4749
public class StreamDecoder extends Reader {
@@ -271,6 +273,25 @@ private boolean isOpen() {
271273
return !closed;
272274
}
273275

276+
public void fillZeroToPosition() throws IOException {
277+
Object lock = this.lock;
278+
if (lock instanceof InternalLock locker) {
279+
locker.lock();
280+
try {
281+
lockedFillZeroToPosition();
282+
} finally {
283+
locker.unlock();
284+
}
285+
} else {
286+
synchronized (lock) {
287+
lockedFillZeroToPosition();
288+
}
289+
}
290+
}
291+
292+
private void lockedFillZeroToPosition() {
293+
Arrays.fill(bb.array(), bb.arrayOffset(), bb.arrayOffset() + bb.position(), (byte)0);
294+
}
274295

275296
// -- Charset-based stream decoder impl --
276297

0 commit comments

Comments
 (0)