Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.0.1] Detect file reads that are longer than expected #24821

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -843,21 +843,6 @@ public static String readContent(Path inputFile, Charset charset) throws IOExcep
return asByteSource(inputFile).asCharSource(charset).read();
}

/**
* Reads at most {@code limit} bytes from {@code inputFile} and returns it as a byte array.
*
* @throws IOException if there was an error.
*/
public static byte[] readContentWithLimit(Path inputFile, int limit) throws IOException {
Preconditions.checkArgument(limit >= 0, "limit needs to be >=0, but it is %s", limit);
ByteSource byteSource = asByteSource(inputFile);
byte[] buffer = new byte[limit];
try (InputStream inputStream = byteSource.openBufferedStream()) {
int read = ByteStreams.read(inputStream, buffer, 0, limit);
return read == limit ? buffer : Arrays.copyOf(buffer, read);
}
}

/**
* The type of {@link IOException} thrown by {@link #readWithKnownFileSize} when fewer bytes than
* expected are read.
Expand All @@ -876,23 +861,47 @@ private ShortReadIOException(Path path, int fileSize, int numBytesRead) {
}
}

/**
* The type of {@link IOException} thrown by {@link #readWithKnownFileSize} when more bytes than
* expected could be read.
*/
public static class LongReadIOException extends IOException {
public final Path path;
public final int fileSize;

private LongReadIOException(Path path, int fileSize) {
super("File '" + path + "' is unexpectedly longer than " + fileSize + " bytes)");
this.path = path;
this.fileSize = fileSize;
}
}

/**
* Reads the given file {@code path}, assumed to have size {@code fileSize}, and does a check on
* the number of bytes read.
*
* <p>Use this method when you already know the size of the file. The check is intended to catch
* issues where filesystems incorrectly truncate files.
* issues where the filesystem incorrectly returns truncated file contents, or where an external
* modification has concurrently truncated or appended to the file.
*
* @throws IOException if there was an error, or if fewer than {@code fileSize} bytes were read.
*/
public static byte[] readWithKnownFileSize(Path path, long fileSize) throws IOException {
Preconditions.checkArgument(fileSize >= 0, "fileSize needs to be >=0, but it is %s", fileSize);
if (fileSize > Integer.MAX_VALUE) {
throw new IOException("Cannot read file with size larger than 2GB");
}
int fileSizeInt = (int) fileSize;
byte[] bytes = readContentWithLimit(path, fileSizeInt);
if (fileSizeInt > bytes.length) {
throw new ShortReadIOException(path, fileSizeInt, bytes.length);
int size = (int) fileSize;
byte[] bytes = new byte[size];
try (InputStream in = asByteSource(path).openBufferedStream()) {
int read = ByteStreams.read(in, bytes, 0, size);
if (read != size) {
throw new ShortReadIOException(path, size, read);
}
int eof = in.read();
if (eof != -1) {
throw new LongReadIOException(path, size);
}
}
return bytes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Before;
Expand Down Expand Up @@ -407,18 +406,19 @@ public void testMoveFileFixPermissions() throws Exception {
}

@Test
public void testReadContentWithLimit() throws IOException {
public void testReadWithKnownFileSize() throws IOException {
createTestDirectoryTree();
String str = "this is a test of readContentWithLimit method";
FileSystemUtils.writeContent(file1, StandardCharsets.ISO_8859_1, str);
assertThat(readStringFromFile(file1, 0)).isEmpty();
assertThat(str.substring(0, 10)).isEqualTo(readStringFromFile(file1, 10));
assertThat(str).isEqualTo(readStringFromFile(file1, 1000000));
}

private String readStringFromFile(Path file, int limit) throws IOException {
byte[] bytes = FileSystemUtils.readContentWithLimit(file, limit);
return new String(bytes, StandardCharsets.ISO_8859_1);
FileSystemUtils.writeContent(file1, ISO_8859_1, str);

assertThat(FileSystemUtils.readWithKnownFileSize(file1, str.length()))
.isEqualTo(str.getBytes(ISO_8859_1));
assertThrows(
FileSystemUtils.LongReadIOException.class,
() -> FileSystemUtils.readWithKnownFileSize(file1, str.length() - 1));
assertThrows(
FileSystemUtils.ShortReadIOException.class,
() -> FileSystemUtils.readWithKnownFileSize(file1, str.length() + 1));
}

@Test
Expand Down
Loading