Prevent unsafe narrowing: base/files
Bug: 1292951
Change-Id: Ie7489503b41003ec3f4ec97991b39e4d82009c58
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3691384
Reviewed-by: danakj <danakj@chromium.org>
Owners-Override: danakj <danakj@chromium.org>
Commit-Queue: danakj <danakj@chromium.org>
Auto-Submit: Peter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1011590}
diff --git a/base/files/file_util.cc b/base/files/file_util.cc
index a186a6c..b3d4ffc5 100644
--- a/base/files/file_util.cc
+++ b/base/files/file_util.cc
@@ -101,7 +101,8 @@
std::vector<char> buffer(kBufferSize);
for (;;) {
- int bytes_read = infile.ReadAtCurrentPos(buffer.data(), buffer.size());
+ int bytes_read =
+ infile.ReadAtCurrentPos(buffer.data(), static_cast<int>(buffer.size()));
if (bytes_read < 0) {
return false;
}
@@ -112,7 +113,8 @@
int bytes_written_per_read = 0;
do {
int bytes_written_partial = outfile.WriteAtCurrentPos(
- &buffer[bytes_written_per_read], bytes_read - bytes_written_per_read);
+ &buffer[static_cast<size_t>(bytes_written_per_read)],
+ bytes_read - bytes_written_per_read);
if (bytes_written_partial < 0) {
return false;
}
@@ -228,8 +230,8 @@
// Many files have incorrect size (proc files etc). Hence, the file is read
// sequentially as opposed to a one-shot read, using file size as a hint for
// chunk size if available.
- constexpr int64_t kDefaultChunkSize = 1 << 16;
- int64_t chunk_size = kDefaultChunkSize - 1;
+ constexpr size_t kDefaultChunkSize = 1 << 16;
+ size_t chunk_size = kDefaultChunkSize - 1;
ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
#if BUILDFLAG(IS_WIN)
BY_HANDLE_FILE_INFORMATION file_info = {};
@@ -237,25 +239,25 @@
reinterpret_cast<HANDLE>(_get_osfhandle(_fileno(stream))),
&file_info)) {
LARGE_INTEGER size;
- size.HighPart = file_info.nFileSizeHigh;
+ size.HighPart = static_cast<LONG>(file_info.nFileSizeHigh);
size.LowPart = file_info.nFileSizeLow;
if (size.QuadPart > 0)
- chunk_size = size.QuadPart;
+ chunk_size = static_cast<size_t>(size.QuadPart);
}
#else // BUILDFLAG(IS_WIN)
// In cases where the reported file size is 0, use a smaller chunk size to
// minimize memory allocated and cost of string::resize() in case the read
// size is small (i.e. proc files). If the file is larger than this, the read
// loop will reset |chunk_size| to kDefaultChunkSize.
- constexpr int64_t kSmallChunkSize = 4096;
+ constexpr size_t kSmallChunkSize = 4096;
chunk_size = kSmallChunkSize - 1;
stat_wrapper_t file_info = {};
if (!File::Fstat(fileno(stream), &file_info) && file_info.st_size > 0)
- chunk_size = file_info.st_size;
+ chunk_size = static_cast<size_t>(file_info.st_size);
#endif // BUILDFLAG(IS_WIN)
// We need to attempt to read at EOF for feof flag to be set so here we
// use |chunk_size| + 1.
- chunk_size = std::min<uint64_t>(chunk_size, max_size) + 1;
+ chunk_size = std::min(chunk_size, max_size) + 1;
size_t bytes_read_this_pass;
size_t bytes_read_so_far = 0;
bool read_status = true;
@@ -345,7 +347,7 @@
bool TouchFile(const FilePath& path,
const Time& last_accessed,
const Time& last_modified) {
- int flags = File::FLAG_OPEN | File::FLAG_WRITE_ATTRIBUTES;
+ uint32_t flags = File::FLAG_OPEN | File::FLAG_WRITE_ATTRIBUTES;
#if BUILDFLAG(IS_WIN)
// On Windows, FILE_FLAG_BACKUP_SEMANTICS is needed to open a directory.