Skip to content

Commit

Permalink
Part of fix for bug 5523834, backporting cache fixes
Browse files Browse the repository at this point in the history
This is hopefully a fix for bug 5255299

Cherry-picking CL
http://src.chromium.org/viewvc/chrome?view=rev&revision=92390

Change-Id: I2f9905bf2f53dd958eb17d1af789fcfb35224061
  • Loading branch information
kristianmonsen committed Nov 15, 2011
1 parent a972e26 commit 93bed14
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 5 deletions.
3 changes: 2 additions & 1 deletion net/base/net_error_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,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)
Expand Down
6 changes: 6 additions & 0 deletions net/disk_cache/block_files.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
1 change: 1 addition & 0 deletions net/disk_cache/block_files.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class 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);

Expand Down
26 changes: 25 additions & 1 deletion net/disk_cache/block_files_unittest.cc
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -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();
Expand Down
5 changes: 3 additions & 2 deletions net/disk_cache/file_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}

Expand Down
3 changes: 2 additions & 1 deletion net/disk_cache/file_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
}

Expand Down

0 comments on commit 93bed14

Please sign in to comment.