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: bpb, lancea, joehw, alanb, jpai, mbaesken
  • Loading branch information
naotoj committed Dec 1, 2023
1 parent 3087e14 commit d568562
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/java.base/share/classes/jdk/internal/io/JdkConsoleImpl.java
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -195,6 +195,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 Down Expand Up @@ -228,6 +231,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
21 changes: 21 additions & 0 deletions src/java.base/share/classes/sun/nio/cs/StreamDecoder.java
Expand Up @@ -42,6 +42,8 @@
import java.nio.charset.CodingErrorAction;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Arrays;

import jdk.internal.misc.InternalLock;

public class StreamDecoder extends Reader {
Expand Down Expand Up @@ -271,6 +273,25 @@ private boolean isOpen() {
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 --

Expand Down

3 comments on commit d568562

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

@GoeLin
Copy link
Member

@GoeLin GoeLin commented on d568562 Dec 13, 2023

Choose a reason for hiding this comment

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

/backport jdk21u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on d568562 Dec 13, 2023

Choose a reason for hiding this comment

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

@GoeLin the backport was successfully created on the branch backport-GoeLin-d5685629 in my personal fork of openjdk/jdk21u-dev. To create a pull request with this backport targeting openjdk/jdk21u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit d5685629 from the openjdk/jdk repository.

The commit being backported was authored by Naoto Sato on 1 Dec 2023 and was reviewed by Brian Burkhalter, Lance Andersen, Joe Wang, Alan Bateman, Jaikiran Pai and Matthias Baesken.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk21u-dev:

$ git fetch https://github.com/openjdk-bots/jdk21u-dev.git backport-GoeLin-d5685629:backport-GoeLin-d5685629
$ git checkout backport-GoeLin-d5685629
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk21u-dev.git backport-GoeLin-d5685629

Please sign in to comment.