Chromium Code Reviews| Index: base/file_util.cc |
| diff --git a/base/file_util.cc b/base/file_util.cc |
| index e44f713cdb31828010ad9be92739a3d021dc4685..902c010aa48752ae0cb62450613a81f323b5f981 100644 |
| --- a/base/file_util.cc |
| +++ b/base/file_util.cc |
| @@ -10,6 +10,7 @@ |
| #include <stdio.h> |
| #include <fstream> |
| +#include <limits> |
| #include "base/files/file_enumerator.h" |
| #include "base/files/file_path.h" |
| @@ -126,7 +127,9 @@ bool TextContentsEqual(const FilePath& filename1, const FilePath& filename2) { |
| return true; |
| } |
| -bool ReadFileToString(const FilePath& path, std::string* contents) { |
| +bool ReadFileToString(const FilePath& path, |
| + std::string* contents, |
| + size_t max_size) { |
| if (path.ReferencesParent()) |
| return false; |
| FILE* file = OpenFile(path, "rb"); |
| @@ -136,7 +139,18 @@ bool ReadFileToString(const FilePath& path, std::string* contents) { |
| char buf[1 << 16]; |
| size_t len; |
| + size_t size = 0; |
| + |
| + // Many files supplied in path have incorrect size (proc files, streams etc) |
|
bartfab (slow)
2014/02/12 16:27:11
Nit 1: s/path/|path|/
Nit 2: s/etc/etc./
Nit 3: En
kaliamoorthi
2014/02/17 11:09:41
Done.
|
| + // hence the file is read sequentially as opposed to one-shot read. |
|
bartfab (slow)
2014/02/12 16:27:11
Nit 1: Add comma after "hence."
Nit 2: s/one-shot/
kaliamoorthi
2014/02/17 11:09:41
Done.
|
| while ((len = fread(buf, 1, sizeof(buf), file)) > 0) { |
| + if ((max_size - size) < len) { |
| + if (contents) |
| + contents->clear(); |
| + CloseFile(file); |
| + return false; |
| + } |
| + size += len; |
| if (contents) |
| contents->append(buf, len); |
| } |
| @@ -145,6 +159,10 @@ bool ReadFileToString(const FilePath& path, std::string* contents) { |
| return true; |
| } |
| +bool ReadFileToString(const FilePath& path, std::string* contents) { |
| + return ReadFileToString(path, contents, std::numeric_limits<size_t>::max()); |
| +} |
| + |
| bool IsDirectoryEmpty(const FilePath& dir_path) { |
| FileEnumerator files(dir_path, false, |
| FileEnumerator::FILES | FileEnumerator::DIRECTORIES); |