summaryrefslogtreecommitdiffstats
path: root/net/base/upload_data_stream.cc
diff options
context:
space:
mode:
authoragl@chromium.org <agl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-02-19 15:38:43 +0000
committeragl@chromium.org <agl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-02-19 15:38:43 +0000
commitf0c4689a6dd3036600581c87e6191ede2479556e (patch)
tree8bcb3f46aa47f3256716dd55a0405504ff9ec0f5 /net/base/upload_data_stream.cc
parent3037a3d03af4eb75444dbd31c0605a54e5f53e45 (diff)
downloadchromium_src-f0c4689a6dd3036600581c87e6191ede2479556e.zip
chromium_src-f0c4689a6dd3036600581c87e6191ede2479556e.tar.gz
chromium_src-f0c4689a6dd3036600581c87e6191ede2479556e.tar.bz2
Fix the case where the browser livelocks if we cannot open a file.
If one tries to upload a file that one doesn't have read access to, the browser livelocks. It tries to read from the file, gets nothing but spins forever because it knows that it hasn't finished reading. To address this, firstly we add a check at stat() time to make sure that we can read the file. However, this doesn't take care of the case where the access() call was incorrect, or the permissions have changed under us. In this case, we replace the missing file with NULs. http://codereview.chromium.org/541022 BUG=30850 TEST=Try to upload a file that isn't readable (i.e. /etc/shadow). The resulting upload should be a 0 byte file. git-svn-id: svn://svn.chromium.org/chrome/trunk/src@39446 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base/upload_data_stream.cc')
-rw-r--r--net/base/upload_data_stream.cc47
1 files changed, 28 insertions, 19 deletions
diff --git a/net/base/upload_data_stream.cc b/net/base/upload_data_stream.cc
index 221ef28..0a0ee73 100644
--- a/net/base/upload_data_stream.cc
+++ b/net/base/upload_data_stream.cc
@@ -10,7 +10,7 @@
namespace net {
-UploadDataStream::UploadDataStream(const UploadData* data)
+UploadDataStream::UploadDataStream(UploadData* data)
: data_(data),
buf_(new IOBuffer(kBufSize)),
buf_len_(0),
@@ -24,6 +24,7 @@ UploadDataStream::UploadDataStream(const UploadData* data)
}
UploadDataStream::~UploadDataStream() {
+ data_->CloseFiles();
}
void UploadDataStream::DidConsume(size_t num_bytes) {
@@ -67,18 +68,19 @@ void UploadDataStream::FillBuf() {
} else {
DCHECK(element.type() == UploadData::TYPE_FILE);
- if (!next_element_stream_.IsOpen()) {
- int flags = base::PLATFORM_FILE_OPEN |
- base::PLATFORM_FILE_READ;
- int rv = next_element_stream_.Open(element.file_path(), flags);
- // If the file does not exist, that's technically okay.. we'll just
- // upload an empty file. This is for consistency with Mozilla.
- DLOG_IF(WARNING, rv != OK) << "Failed to open \""
- << element.file_path().value()
- << "\" for reading: " << rv;
-
- next_element_remaining_ = 0; // Default to reading nothing.
- if (rv == OK) {
+ if (element.file_range_length() == 0) {
+ // If we failed to open the file, then the length is set to zero. The
+ // length used when calculating the POST size was also zero. This
+ // matches the behaviour of Mozilla.
+ next_element_remaining_ = 0;
+ } else {
+ if (!next_element_stream_.IsOpen()) {
+ // We ignore the return value of Open becuase we've already checked
+ // !IsOpen, above.
+ int flags = base::PLATFORM_FILE_READ |
+ base::PLATFORM_FILE_WRITE;
+ next_element_stream_.Open(element.platform_file(), flags);
+
uint64 offset = element.file_range_offset();
if (offset && next_element_stream_.Seek(FROM_BEGIN, offset) < 0) {
DLOG(WARNING) << "Failed to seek \"" << element.file_path().value()
@@ -90,11 +92,18 @@ void UploadDataStream::FillBuf() {
}
int rv = 0;
- int count = static_cast<int>(std::min(
- static_cast<uint64>(size_remaining), next_element_remaining_));
- if (count > 0 &&
- (rv = next_element_stream_.Read(buf_->data() + buf_len_,
- count, NULL)) > 0) {
+ if (next_element_remaining_ > 0) {
+ int count =
+ static_cast<int>(std::min(next_element_remaining_,
+ static_cast<uint64>(size_remaining)));
+ rv = next_element_stream_.Read(buf_->data() + buf_len_, count, NULL);
+ if (rv < 1) {
+ // If the file was truncated between the time that we opened it and
+ // now, or if we got an error on reading, then we pad with NULs.
+ memset(buf_->data() + buf_len_, 0, count);
+ rv = count;
+ }
+
buf_len_ += rv;
next_element_remaining_ -= rv;
} else {
@@ -105,7 +114,7 @@ void UploadDataStream::FillBuf() {
if (advance_to_next_element) {
++next_element_;
next_element_offset_ = 0;
- next_element_stream_.Close();
+ next_element_stream_.Release();
}
}