Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: content/browser/download/base_file.cc

Issue 1887873002: [Downloads] BaseFile shouldn't read past the expected EOF for a stub file. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | content/browser/download/base_file_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/download/base_file.h" 5 #include "content/browser/download/base_file.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/files/file.h" 10 #include "base/files/file.h"
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 212
213 if (bytes_so_far_ == 0) 213 if (bytes_so_far_ == 0)
214 return DOWNLOAD_INTERRUPT_REASON_NONE; 214 return DOWNLOAD_INTERRUPT_REASON_NONE;
215 215
216 if (file_.Seek(base::File::FROM_BEGIN, 0) != 0) 216 if (file_.Seek(base::File::FROM_BEGIN, 0) != 0)
217 return LogSystemError("Seek partial file", 217 return LogSystemError("Seek partial file",
218 logging::GetLastSystemErrorCode()); 218 logging::GetLastSystemErrorCode());
219 219
220 const size_t kMinBufferSize = secure_hash_->GetHashLength(); 220 const size_t kMinBufferSize = secure_hash_->GetHashLength();
221 const size_t kMaxBufferSize = 1024 * 512; 221 const size_t kMaxBufferSize = 1024 * 512;
222 static_assert(kMaxBufferSize <= std::numeric_limits<int>::max(),
223 "kMaxBufferSize must fit on an int");
222 224
223 // The size of the buffer is: 225 // The size of the buffer is:
224 // - at least kMinBufferSize so that we can use it to hold the hash as well. 226 // - at least kMinBufferSize so that we can use it to hold the hash as well.
225 // - at most kMaxBufferSize so that there's a reasonable bound. 227 // - at most kMaxBufferSize so that there's a reasonable bound.
226 // - not larger than |bytes_so_far_| unless bytes_so_far_ is less than the 228 // - not larger than |bytes_so_far_| unless bytes_so_far_ is less than the
227 // hash size. 229 // hash size.
228 std::vector<char> buffer(std::max( 230 std::vector<char> buffer(std::max<int64_t>(
229 kMinBufferSize, std::min<size_t>(kMaxBufferSize, bytes_so_far_))); 231 kMinBufferSize, std::min<int64_t>(kMaxBufferSize, bytes_so_far_)));
230 232
231 int64_t current_position = 0; 233 int64_t current_position = 0;
232 while (current_position < bytes_so_far_) { 234 while (current_position < bytes_so_far_) {
233 int length = file_.ReadAtCurrentPos(&buffer.front(), buffer.size()); 235 // While std::min needs to work with int64_t, the result is always at most
236 // kMaxBufferSize, which fits on an int.
237 int bytes_to_read =
238 std::min<int64_t>(buffer.size(), bytes_so_far_ - current_position);
239 int length = file_.ReadAtCurrentPos(&buffer.front(), bytes_to_read);
234 if (length == -1) { 240 if (length == -1) {
235 return LogInterruptReason("Reading partial file", 241 return LogInterruptReason("Reading partial file",
236 logging::GetLastSystemErrorCode(), 242 logging::GetLastSystemErrorCode(),
237 DOWNLOAD_INTERRUPT_REASON_FILE_TOO_SHORT); 243 DOWNLOAD_INTERRUPT_REASON_FILE_TOO_SHORT);
238 } 244 }
239 245
240 if (length == 0) 246 if (length == 0)
241 break; 247 break;
242 248
243 secure_hash_->Update(&buffer.front(), length); 249 secure_hash_->Update(&buffer.front(), length);
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
366 DVLOG(1) << __FUNCTION__ << "() operation:" << operation 372 DVLOG(1) << __FUNCTION__ << "() operation:" << operation
367 << " os_error:" << os_error 373 << " os_error:" << os_error
368 << " reason:" << DownloadInterruptReasonToString(reason); 374 << " reason:" << DownloadInterruptReasonToString(reason);
369 bound_net_log_.AddEvent( 375 bound_net_log_.AddEvent(
370 net::NetLog::TYPE_DOWNLOAD_FILE_ERROR, 376 net::NetLog::TYPE_DOWNLOAD_FILE_ERROR,
371 base::Bind(&FileInterruptedNetLogCallback, operation, os_error, reason)); 377 base::Bind(&FileInterruptedNetLogCallback, operation, os_error, reason));
372 return reason; 378 return reason;
373 } 379 }
374 380
375 } // namespace content 381 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | content/browser/download/base_file_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698