Fix more `DM_DEFAULT_ENCODING` SpotBugs violations (#6098)

This commit is contained in:
Basil Crow 2022-01-07 06:29:55 -08:00 committed by GitHub
parent 520e114ea1
commit 3b29e9d460
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 52 additions and 28 deletions

View File

@ -70,7 +70,6 @@ import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
@ -1656,7 +1655,7 @@ public final class FilePath implements SerializableOnlyOverRemoting {
throw new IOException("Failed to create a temporary directory in " + dir, e);
}
try (Writer w = new FileWriter(f)) {
try (Writer w = Files.newBufferedWriter(Util.fileToPath(f), Charset.defaultCharset())) {
w.write(contents);
}

View File

@ -433,7 +433,12 @@ public abstract class CLICommand implements ExtensionPoint, Cloneable {
@Restricted(NoExternalUse.class)
public final String getLongDescription() {
ByteArrayOutputStream out = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(out);
PrintStream ps;
try {
ps = new PrintStream(out, false, getClientCharset().name());
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
printUsageSummary(ps);
ps.close();

View File

@ -28,6 +28,7 @@ import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import hudson.Extension;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
@ -63,7 +64,7 @@ public class GroovyCommand extends CLICommand {
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
Binding binding = new Binding();
binding.setProperty("out", new PrintWriter(stdout, true));
binding.setProperty("out", new PrintWriter(new OutputStreamWriter(stdout, getClientCharset()), true));
binding.setProperty("stdin", stdin);
binding.setProperty("stdout", stdout);
binding.setProperty("stderr", stderr);

View File

@ -30,6 +30,7 @@ import groovy.lang.Closure;
import hudson.Extension;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.ArrayList;
@ -84,7 +85,7 @@ public class GroovyshCommand extends CLICommand {
Binding binding = new Binding();
// redirect "println" to the CLI
binding.setProperty("out", new PrintWriter(stdout, true));
binding.setProperty("out", new PrintWriter(new OutputStreamWriter(stdout, getClientCharset()), true));
binding.setProperty("hudson", Jenkins.get()); // backward compatibility
binding.setProperty("jenkins", Jenkins.get());

View File

@ -5,6 +5,7 @@ import hudson.model.Run;
import hudson.scm.ChangeLogSet;
import hudson.util.QuotedStringTokenizer;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.List;
import jenkins.scm.RunWithSCM;
@ -46,7 +47,7 @@ public class ListChangesCommand extends RunRangeCommand {
// No other permission check needed.
switch (format) {
case XML:
PrintWriter w = new PrintWriter(stdout);
PrintWriter w = new PrintWriter(new OutputStreamWriter(stdout, getClientCharset()));
w.println("<changes>");
for (Run<?, ?> build : builds) {
if (build instanceof RunWithSCM) {

View File

@ -33,9 +33,12 @@ import hudson.Util;
import hudson.util.StreamTaskListener;
import hudson.util.jna.Kernel32;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.model.Jenkins;
@ -110,7 +113,7 @@ public class WindowsServiceLifecycle extends Lifecycle {
File baseDir = getBaseDir();
File copyFiles = new File(baseDir, baseName + ".copies");
try (FileWriter w = new FileWriter(copyFiles, true)) {
try (Writer w = Files.newBufferedWriter(Util.fileToPath(copyFiles), Charset.defaultCharset(), StandardOpenOption.CREATE, StandardOpenOption.APPEND)) {
w.write(by.getAbsolutePath() + '>' + getHudsonWar().getAbsolutePath() + '\n');
}
}

View File

@ -47,6 +47,12 @@ public class StreamBuildListener extends StreamTaskListener implements BuildList
super(out, charset);
}
/**
* @deprecated as of TODO
* The caller should use {@link #StreamBuildListener(OutputStream, Charset)} to pass in
* the charset and output stream separately, so that this class can handle encoding correctly.
*/
@Deprecated
public StreamBuildListener(OutputStream w) {
super(w);
}

View File

@ -43,6 +43,7 @@ import hudson.util.ArgumentListBuilder;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.charset.Charset;
import java.util.Collections;
/**
@ -83,7 +84,7 @@ public abstract class SU {
return "sudo";
}
@SuppressFBWarnings(value = {"COMMAND_INJECTION", "DM_DEFAULT_ENCODING"}, justification = "TODO needs triage")
@SuppressFBWarnings(value = "COMMAND_INJECTION", justification = "TODO needs triage")
@Override
protected Process sudoWithPass(ArgumentListBuilder args) throws IOException {
args.prepend(sudoExe(), "-S");
@ -92,7 +93,7 @@ public abstract class SU {
Process p = pb.start();
// TODO: use -p to detect prompt
// TODO: detect if the password didn't work
try (PrintStream ps = new PrintStream(p.getOutputStream())) {
try (PrintStream ps = new PrintStream(p.getOutputStream(), false, Charset.defaultCharset().name())) {
ps.println(rootPassword);
ps.println(rootPassword);
ps.println(rootPassword);

View File

@ -76,6 +76,13 @@ public class StreamTaskListener extends AbstractTaskListener implements TaskList
this(out, null);
}
/**
* @deprecated as of TODO
* The caller should use {@link #StreamTaskListener(OutputStream, Charset)} to pass in
* the charset and output stream separately, so that this class can handle encoding correctly,
* or use {@link #fromStdout()} or {@link #fromStderr()}.
*/
@Deprecated
public StreamTaskListener(@NonNull OutputStream out) {
this(out, null);
}
@ -83,7 +90,7 @@ public class StreamTaskListener extends AbstractTaskListener implements TaskList
public StreamTaskListener(@NonNull OutputStream out, @CheckForNull Charset charset) {
try {
if (charset == null)
this.out = out instanceof PrintStream ? (PrintStream) out : new PrintStream(out, false);
this.out = out instanceof PrintStream ? (PrintStream) out : new PrintStream(out, false, Charset.defaultCharset().name());
else
this.out = new PrintStream(out, false, charset.name());
this.charset = charset;
@ -93,6 +100,12 @@ public class StreamTaskListener extends AbstractTaskListener implements TaskList
}
}
/**
* @deprecated as of TODO
* The caller should use {@link #StreamTaskListener(File, Charset)} to pass in
* the charset and file separately, so that this class can handle encoding correctly.
*/
@Deprecated
public StreamTaskListener(@NonNull File out) throws IOException {
this(out, null);
}
@ -187,8 +200,9 @@ public class StreamTaskListener extends AbstractTaskListener implements TaskList
private static /* not final */ boolean AUTO_FLUSH = SystemProperties.getBoolean(KEY_AUTO_FLUSH);
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
out = new PrintStream((OutputStream) in.readObject(), AUTO_FLUSH);
OutputStream os = (OutputStream) in.readObject();
String name = (String) in.readObject();
out = new PrintStream(os, AUTO_FLUSH, name != null ? name : Charset.defaultCharset().name());
charset = name == null ? null : Charset.forName(name);
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.log(Level.FINE, null, new Throwable("deserializing here with AUTO_FLUSH=" + AUTO_FLUSH));

View File

@ -7,12 +7,13 @@ import hudson.model.AdministrativeMonitor;
import hudson.util.jna.Kernel32Utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
@ -123,7 +124,7 @@ public class HsErrPidList extends AdministrativeMonitor {
private void scanFile(File log) {
LOGGER.fine("Scanning " + log);
try (Reader rawReader = new FileReader(log);
try (Reader rawReader = Files.newBufferedReader(log.toPath(), Charset.defaultCharset());
BufferedReader r = new BufferedReader(rawReader)) {
if (!findHeader(r))
@ -140,7 +141,7 @@ public class HsErrPidList extends AdministrativeMonitor {
return;
}
}
} catch (IOException e) {
} catch (IOException | InvalidPathException e) {
// not a big enough deal.
LOGGER.log(Level.FINE, "Failed to parse hs_err_pid file: " + log, e);
}

View File

@ -16,10 +16,12 @@ import hudson.slaves.JNLPLauncher;
import hudson.slaves.SlaveComputer;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.channels.ClosedChannelException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.concurrent.ExecutionException;
@ -152,7 +154,7 @@ public class DefaultJnlpSlaveReceiver extends JnlpAgentReceiver {
final SlaveComputer computer = state.getNode();
final OutputStream log = computer.openLogFile();
state.setLog(log);
try (PrintWriter logw = new PrintWriter(log, true)) {
try (PrintWriter logw = new PrintWriter(new OutputStreamWriter(log, /* TODO switch agent logs to UTF-8 */ Charset.defaultCharset()), true)) {
logw.println("Inbound agent connected from " + event.getRemoteEndpointDescription());
}
for (ChannelConfigurator cc : ChannelConfigurator.all()) {
@ -172,7 +174,7 @@ public class DefaultJnlpSlaveReceiver extends JnlpAgentReceiver {
try {
computer.setChannel(event.getChannel(), state.getLog(), null);
} catch (IOException | InterruptedException e) {
PrintWriter logw = new PrintWriter(state.getLog(), true);
PrintWriter logw = new PrintWriter(new OutputStreamWriter(state.getLog(), /* TODO switch agent logs to UTF-8 */ Charset.defaultCharset()), true);
Functions.printStackTrace(e, logw);
try {
event.getChannel().close();

View File

@ -50,17 +50,7 @@
<And>
<Bug pattern="DM_DEFAULT_ENCODING"/>
<Or>
<Class name="hudson.cli.CLICommand"/>
<Class name="hudson.cli.GroovyCommand"/>
<Class name="hudson.cli.GroovyshCommand"/>
<Class name="hudson.cli.ListChangesCommand"/>
<Class name="hudson.FilePath$CreateTextTempFile"/>
<Class name="hudson.lifecycle.WindowsServiceLifecycle"/>
<Class name="hudson.util.StreamTaskListener"/>
<Class name="hudson.util.TextFile"/>
<Class name="jenkins.diagnosis.HsErrPidList"/>
<Class name="jenkins.security.s2m.AdminWhitelistRule"/>
<Class name="jenkins.slaves.DefaultJnlpSlaveReceiver"/>
</Or>
</And>
</Match>