Skip to content

Commit

Permalink
Fix info and log double encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Nov 25, 2024
1 parent dbf8b4a commit e0ab57c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@

package com.google.devtools.build.lib.runtime;

import static java.nio.charset.StandardCharsets.ISO_8859_1;

import com.google.common.base.Supplier;
import com.google.devtools.build.lib.analysis.config.BuildConfigurationValue;
import com.google.devtools.build.lib.util.AbruptExitException;
import java.io.ByteArrayOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;

/** An item that is returned by <code>blaze info</code>. */
public abstract class InfoItem {
Expand Down Expand Up @@ -78,8 +79,7 @@ protected static byte[] print(Object value) {
return (byte[]) value;
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintWriter writer =
new PrintWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8));
PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream, ISO_8859_1));
writer.print(value + "\n");
writer.flush();
return outputStream.toByteArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.google.devtools.build.lib.runtime.commands;

import static com.google.devtools.build.lib.runtime.Command.BuildPhase.NONE;
import static java.nio.charset.StandardCharsets.ISO_8859_1;

import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
Expand Down Expand Up @@ -77,7 +78,6 @@
import com.google.devtools.common.options.OptionsParsingResult;
import com.google.devtools.common.options.OptionsProvider;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
Expand Down Expand Up @@ -194,7 +194,7 @@ public BlazeCommandResult exec(
}
value = infoItem.get(configurationSupplier, env);
if (residue.size() > 1) {
outErr.getOutputStream().write((key + ": ").getBytes(StandardCharsets.UTF_8));
outErr.getOutputStream().write((key + ": ").getBytes(ISO_8859_1));
}
outErr.getOutputStream().write(value);
}
Expand Down Expand Up @@ -224,8 +224,7 @@ public BlazeCommandResult exec(
if (infoItem.needsSyncPackageLoading()) {
ensureSyncPackageLoading(env, optionsParsingResult);
}
outErr.getOutputStream().write(
(infoItem.getName() + ": ").getBytes(StandardCharsets.UTF_8));
outErr.getOutputStream().write((infoItem.getName() + ": ").getBytes(ISO_8859_1));
try (SilentCloseable c = Profiler.instance().profile(infoItem.getName() + ".infoItem")) {
outErr.getOutputStream().write(infoItem.get(configurationSupplier, env));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.google.common.io.CountingOutputStream;
import com.google.common.net.InetAddresses;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InterruptedIOException;
Expand Down Expand Up @@ -524,11 +523,15 @@ private static <T> T getConfiguredProperty(
return builderValue;
}

// .properties files are read as Latin-1 by java.util.Properties, with Unicode escape sequences
// interpreted. Since the Bazel client passes path properties as UTF-8 without escaping,
// configuredValue already contains a string in Bazel's internal string encoding (see
// StringEncoding).
String configuredValue =
LogManager.getLogManager()
.getProperty(SimpleLogHandler.class.getName() + "." + configuredName);
if (configuredValue != null) {
return parse.apply(StringEncoding.unicodeToInternal(configuredValue));
return parse.apply(configuredValue);
}
return fallbackValue;
}
Expand Down Expand Up @@ -695,7 +698,7 @@ private static Path getBaseFilePath(String prefix, String pattern) {
}
}

return new File(StringEncoding.internalToPlatform(sb.toString())).getAbsoluteFile().toPath();
return Path.of(StringEncoding.internalToPlatform(sb.toString())).toAbsolutePath();
}

/**
Expand All @@ -718,7 +721,8 @@ private static Path getSymlinkAbsolutePath(Path logDir, String symlink) {

private static final class Output {
/** Log file currently in use. */
@Nullable private File file;
@Nullable private Path file;

/** Output stream for {@link #file} which counts the number of bytes written. */
@Nullable private CountingOutputStream stream;
/** Writer for {@link #stream}. */
Expand All @@ -733,11 +737,11 @@ public boolean isOpen() {
*
* @throws IOException if the file could not be opened
*/
public void open(String path) throws IOException {
public void open(Path file) throws IOException {
try {
close();
file = new File(StringEncoding.internalToPlatform(path));
stream = new CountingOutputStream(new FileOutputStream(file, true));
this.file = file;
stream = new CountingOutputStream(new FileOutputStream(file.toFile(), true));
writer = new OutputStreamWriter(stream, ISO_8859_1);
} catch (IOException e) {
close();
Expand All @@ -751,7 +755,7 @@ public void open(String path) throws IOException {
* @throws NullPointerException if not open
*/
public Path getPath() {
return file.toPath();
return file;
}

/**
Expand Down Expand Up @@ -825,7 +829,8 @@ private void openOutputIfNeeded() {

try {
output.open(
baseFilePath + timestampFormat.format(Date.from(Instant.now(clock))) + extension);
Path.of(
baseFilePath + timestampFormat.format(Date.from(Instant.now(clock))) + extension));
output.write(getFormatter().getHead(this));
} catch (IOException e) {
try {
Expand Down

0 comments on commit e0ab57c

Please sign in to comment.