Bug 1626973 - Make attempt to increase the file size via IDBFileHandle.truncate an error. r=dom-workers-and-storage-reviewers,janv

Differential Revision: https://phabricator.services.mozilla.com/D69580

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Simon Giesecke 2020-04-06 13:56:47 +00:00
parent 03d8a2f376
commit 1ca11f861a
3 changed files with 28 additions and 0 deletions

View File

@ -2137,6 +2137,25 @@ nsresult TruncateOp::DoFileWork(FileHandle* aFileHandle) {
nsCOMPtr<nsISeekableStream> sstream = do_QueryInterface(mFileStream);
MOZ_ASSERT(sstream);
if (mParams.offset()) {
nsCOMPtr<nsIFileMetadata> fileMetadata = do_QueryInterface(mFileStream);
MOZ_ASSERT(fileMetadata);
int64_t size;
nsresult rv = fileMetadata->GetSize(&size);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
MOZ_ASSERT(size >= 0);
if (mParams.offset() > static_cast<uint64_t>(size)) {
// Cannot extend the size of a file through truncate.
return NS_ERROR_DOM_INVALID_MODIFICATION_ERR;
}
}
// XXX If we allowed truncate to extend the file size, we would to ensure that
// the quota limit is checked, e.g. by making FileQuotaStream override Seek.
nsresult rv = sstream->Seek(nsISeekableStream::NS_SEEK_SET, mParams.offset());
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;

View File

@ -415,6 +415,7 @@ function expectingError(request, errorName) {
return new Promise(function(resolve, reject) {
request.onerror = function(event) {
is(errorName, event.target.error.name, "Correct exception type");
event.stopPropagation();
resolve(event);
};
request.onsuccess = function(event) {

View File

@ -27,6 +27,14 @@
info("Expecting TypeError on truncating to a negative length that converts to UINT64_MAX implicitly");
let message = assertThrowsInstanceOf(() => { fileHandle.truncate(-1); }, TypeError);
is(message, "IDBFileHandle.truncate: UINT64_MAX is not a valid size", "Correct message");
info ("Attempt to truncate to a size larger than the current size should fail.");
// XXX Expect a more specific error here?
await expectingError(fileHandle.truncate(10000000), "UnknownError");
info("Still the state should be consistent and we should be able to truncate to a valid size after this.");
// This wasn't always the case, see Bug 1626973
await expectingSuccess(fileHandle.truncate(0));
}
</script>
<script type="text/javascript" src="file.js"></script>