diff options
author | rvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-13 18:41:58 +0000 |
---|---|---|
committer | rvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-13 18:41:58 +0000 |
commit | e25d44ddde58a2e0fd05fd4e8f9c9632fb7209af (patch) | |
tree | 26054af3acf60a1109f76ba52ff4f827ae205e29 /net | |
parent | c369fd304932e51ad62cb3bd9cb5608665518e00 (diff) | |
download | chromium_src-e25d44ddde58a2e0fd05fd4e8f9c9632fb7209af.zip chromium_src-e25d44ddde58a2e0fd05fd4e8f9c9632fb7209af.tar.gz chromium_src-e25d44ddde58a2e0fd05fd4e8f9c9632fb7209af.tar.bz2 |
Disk cache: detect block files that are shorter than expected
and return a proper net error code from async IO.
BUG=88968
TEST=net_unittests
Review URL: http://codereview.chromium.org/7351007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@92390 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r-- | net/base/net_error_list.h | 3 | ||||
-rw-r--r-- | net/disk_cache/block_files.cc | 6 | ||||
-rw-r--r-- | net/disk_cache/block_files.h | 1 | ||||
-rw-r--r-- | net/disk_cache/block_files_unittest.cc | 26 | ||||
-rw-r--r-- | net/disk_cache/file_posix.cc | 5 | ||||
-rw-r--r-- | net/disk_cache/file_win.cc | 3 |
6 files changed, 39 insertions, 5 deletions
diff --git a/net/base/net_error_list.h b/net/base/net_error_list.h index 80d770f..cff5a79 100644 --- a/net/base/net_error_list.h +++ b/net/base/net_error_list.h @@ -475,7 +475,8 @@ NET_ERROR(CACHE_MISS, -400) // Unable to read from the disk cache. NET_ERROR(CACHE_READ_FAILURE, -401) -// ****NOTE THAT code -402 is available**** +// Unable to write to the disk cache. +NET_ERROR(CACHE_WRITE_FAILURE, -402) // The operation is not supported for this entry. NET_ERROR(CACHE_OPERATION_NOT_SUPPORTED, -403) diff --git a/net/disk_cache/block_files.cc b/net/disk_cache/block_files.cc index c9b5238..d73953d 100644 --- a/net/disk_cache/block_files.cc +++ b/net/disk_cache/block_files.cc @@ -419,6 +419,12 @@ bool BlockFiles::OpenBlockFile(int index) { return false; } + if (static_cast<int>(file_len) < + header->max_entries * header->entry_size + kBlockHeaderSize) { + LOG(ERROR) << "File too small " << name.value(); + return false; + } + if (index == 0) { // Load the links file into memory with a single read. scoped_array<char> buf(new char[file_len]); diff --git a/net/disk_cache/block_files.h b/net/disk_cache/block_files.h index 3e59cbc..5ba49c5 100644 --- a/net/disk_cache/block_files.h +++ b/net/disk_cache/block_files.h @@ -93,6 +93,7 @@ class NET_TEST BlockFiles { scoped_ptr<base::ThreadChecker> thread_checker_; FRIEND_TEST_ALL_PREFIXES(DiskCacheTest, BlockFiles_ZeroSizeFile); + FRIEND_TEST_ALL_PREFIXES(DiskCacheTest, BlockFiles_TruncatedFile); FRIEND_TEST_ALL_PREFIXES(DiskCacheTest, BlockFiles_InvalidFile); FRIEND_TEST_ALL_PREFIXES(DiskCacheTest, BlockFiles_Stats); diff --git a/net/disk_cache/block_files_unittest.cc b/net/disk_cache/block_files_unittest.cc index 90bf048..7ee08d3 100644 --- a/net/disk_cache/block_files_unittest.cc +++ b/net/disk_cache/block_files_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -177,6 +177,30 @@ TEST_F(DiskCacheTest, BlockFiles_ZeroSizeFile) { ASSERT_FALSE(files.Init(false)); } +// Handling of truncated files (non empty). +TEST_F(DiskCacheTest, BlockFiles_TruncatedFile) { + FilePath path = GetCacheFilePath(); + ASSERT_TRUE(DeleteCache(path)); + ASSERT_TRUE(file_util::CreateDirectory(path)); + + BlockFiles files(path); + ASSERT_TRUE(files.Init(true)); + Addr address; + EXPECT_TRUE(files.CreateBlock(RANKINGS, 2, &address)); + + FilePath filename = files.Name(0); + files.CloseFiles(); + // Truncate one of the files. + { + scoped_refptr<File> file(new File); + ASSERT_TRUE(file->Init(filename)); + EXPECT_TRUE(file->SetLength(15000)); + } + + // Initializing should fail, not crash. + ASSERT_FALSE(files.Init(false)); +} + // An invalid file can be detected after init. TEST_F(DiskCacheTest, BlockFiles_InvalidFile) { FilePath path = GetCacheFilePath(); diff --git a/net/disk_cache/file_posix.cc b/net/disk_cache/file_posix.cc index 6f78b7a..a80ae6c 100644 --- a/net/disk_cache/file_posix.cc +++ b/net/disk_cache/file_posix.cc @@ -8,6 +8,7 @@ #include "base/logging.h" #include "base/threading/worker_pool.h" +#include "net/base/net_errors.h" #include "net/disk_cache/disk_cache.h" #include "net/disk_cache/in_flight_io.h" @@ -92,7 +93,7 @@ void FileBackgroundIO::Read() { if (file_->Read(const_cast<void*>(buf_), buf_len_, offset_)) { result_ = static_cast<int>(buf_len_); } else { - result_ = -1; + result_ = net::ERR_CACHE_READ_FAILURE; } controller_->OnIOComplete(this); } @@ -101,7 +102,7 @@ void FileBackgroundIO::Read() { void FileBackgroundIO::Write() { bool rv = file_->Write(buf_, buf_len_, offset_); - result_ = rv ? static_cast<int>(buf_len_) : -1; + result_ = rv ? static_cast<int>(buf_len_) : net::ERR_CACHE_WRITE_FAILURE; controller_->OnIOComplete(this); } diff --git a/net/disk_cache/file_win.cc b/net/disk_cache/file_win.cc index 9477604..0b9468c 100644 --- a/net/disk_cache/file_win.cc +++ b/net/disk_cache/file_win.cc @@ -7,6 +7,7 @@ #include "base/file_path.h" #include "base/lazy_instance.h" #include "base/message_loop.h" +#include "net/base/net_errors.h" #include "net/disk_cache/disk_cache.h" namespace { @@ -42,7 +43,7 @@ void CompletionHandler::OnIOCompleted(MessageLoopForIO::IOContext* context, if (error) { DCHECK(!actual_bytes); - actual_bytes = static_cast<DWORD>(-1); + actual_bytes = static_cast<DWORD>(net::ERR_CACHE_READ_FAILURE); NOTREACHED(); } |