Bug 1688711 - Don't write to empty JsBuffer when reading compressed files r=emalysz

Differential Revision: https://phabricator.services.mozilla.com/D103524
This commit is contained in:
Barret Rennie 2021-01-30 00:33:21 +00:00
Родитель 868935867c
Коммит 60d382943b
2 изменённых файлов: 31 добавлений и 25 удалений

Просмотреть файл

@ -814,40 +814,35 @@ Result<IOUtils::JsBuffer, IOUtils::IOError> IOUtils::ReadSync(
}
JsBuffer buffer = JsBuffer::CreateEmpty(aBufferKind);
Span<char> toRead;
if (aDecompress || bufSize > 0) {
if (bufSize > 0) {
auto result = JsBuffer::Create(aBufferKind, bufSize);
if (result.isErr()) {
return result.propagateErr();
}
buffer = result.unwrap();
} else {
// No need to read an empty file.
return buffer;
}
Span<char> toRead = buffer.BeginWriting();
toRead = buffer.BeginWriting();
// Read the file from disk.
uint32_t totalRead = 0;
while (totalRead != bufSize) {
uint32_t bytesRead = 0;
if (nsresult rv =
stream->Read(toRead.Elements(), bufSize - totalRead, &bytesRead);
NS_FAILED(rv)) {
return Err(IOError(rv).WithMessage(
"Encountered an unexpected error while reading file(%s)",
aFile->HumanReadablePath().get()));
// Read the file from disk.
uint32_t totalRead = 0;
while (totalRead != bufSize) {
uint32_t bytesRead = 0;
if (nsresult rv =
stream->Read(toRead.Elements(), bufSize - totalRead, &bytesRead);
NS_FAILED(rv)) {
return Err(IOError(rv).WithMessage(
"Encountered an unexpected error while reading file(%s)",
aFile->HumanReadablePath().get()));
}
if (bytesRead == 0) {
break;
}
totalRead += bytesRead;
toRead = toRead.From(bytesRead);
}
if (bytesRead == 0) {
break;
}
totalRead += bytesRead;
toRead = toRead.From(bytesRead);
}
buffer.SetLength(totalRead);
buffer.SetLength(totalRead);
}
// Decompress the file contents, if required.
if (aDecompress) {

Просмотреть файл

@ -364,6 +364,17 @@
"IOUtils::readUTF8 fails to read corrupt LZ4 contents with a correct header"
);
info("Testing decompression of an empty file (no header)");
{
const n = await IOUtils.writeUTF8(tmpFileName, "");
ok(n === 0, "Overwrote with empty file");
}
await Assert.rejects(
IOUtils.readUTF8(tmpFileName, { decompress: true }),
/Could not decompress file because the buffer is too short/,
"IOUtils::readUTF8 fails to decompress empty files"
);
await cleanup(tmpFileName);
});
</script>