Bug 1238440 - FileReader should throw an error when the blob changed size when reading, r=khuey

This commit is contained in:
Andrea Marchesini 2016-01-26 10:19:15 +00:00
parent 5b7e0b7ed2
commit 9249440d19
3 changed files with 87 additions and 2 deletions

View File

@ -13,6 +13,7 @@
#include "nsIStreamTransportService.h"
#include "mozilla/Base64.h"
#include "mozilla/CheckedInt.h"
#include "mozilla/dom/DOMError.h"
#include "mozilla/dom/EncodingUtils.h"
#include "mozilla/dom/File.h"
@ -315,11 +316,17 @@ FileReader::DoReadData(uint64_t aCount)
NS_ASSERTION(bytesRead == aCount, "failed to read data");
}
else {
CheckedInt<uint64_t> size = mDataLen;
size += aCount;
//Update memory buffer to reflect the contents of the file
if (mDataLen + aCount > UINT32_MAX) {
// PR_Realloc doesn't support over 4GB memory size even if 64-bit OS
if (!size.isValid() ||
// PR_Realloc doesn't support over 4GB memory size even if 64-bit OS
size.value() > UINT32_MAX ||
size.value() > mTotal) {
return NS_ERROR_OUT_OF_MEMORY;
}
if (mDataFormat != FILE_AS_ARRAYBUFFER) {
mFileData = (char *) realloc(mFileData, mDataLen + aCount);
NS_ENSURE_TRUE(mFileData, NS_ERROR_OUT_OF_MEMORY);

View File

@ -24,3 +24,5 @@ skip-if = e10s # this tests non-e10s behavior. it's not expected to work in e10s
skip-if = true # Intermittent failures - bug 987493. Restore the skip-if above once fixed
[browser_bug1058164.js]
[browser_use_counters.js]
[browser_bug1238440.js]
skip-if = e10s

View File

@ -0,0 +1,76 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
Components.utils.import("resource://gre/modules/NetUtil.jsm");
Components.utils.import("resource://gre/modules/FileUtils.jsm");
waitForExplicitFinish();
const PAGE = "data:text/html,<html><body><input type=\"file\"/></body></html>";
function writeFile(file, text) {
return new Promise((resolve) => {
let converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"]
.createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
converter.charset = "UTF-8";
let ostream = FileUtils.openSafeFileOutputStream(file);
let istream = converter.convertToInputStream(text);
NetUtil.asyncCopy(istream, ostream, function(status) {
if (!Components.isSuccessCode(status)) throw 'fail';
resolve();
});
});
}
function runFileReader(input, status) {
return new Promise((resolve) => {
let fr = new FileReader();
fr.onload = function() {
ok(status, "FileReader called onload");
resolve();
}
fr.onerror = function(e) {
e.preventDefault();
ok(!status, "FileReader called onerror");
resolve();
}
fr.readAsArrayBuffer(input);
});
}
add_task(function() {
info("Creating a temporary file...");
let file = FileUtils.getFile("TmpD", ["bug1238440.txt"]);
yield writeFile(file, "hello world");
info("Opening a tab...");
let tab = gBrowser.addTab(PAGE);
gBrowser.selectedTab = tab;
let browser = tab.linkedBrowser;
yield BrowserTestUtils.browserLoaded(browser);
info("Populating the form...");
let doc = browser.contentDocument;
let input = doc.querySelector('input');
input.value = file.path;
info("Running the FileReader...");
yield runFileReader(input.files[0], true);
info("Writing the temporary file again...");
yield writeFile(file, "hello world-----------------------------");
info("Running the FileReader again...");
yield runFileReader(input.files[0], false);
info("Closing the tab...");
gBrowser.removeTab(gBrowser.selectedTab);
ok(true, "we didn't crash.");
});