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

Unified Diff: base/file_util.cc

Issue 157593005: Added new ReadFileToString API with a max_size argument (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added new ReadFileToString API with a max_size argument Created 6 years, 10 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 side-by-side diff with in-line comments
Download patch
Index: base/file_util.cc
diff --git a/base/file_util.cc b/base/file_util.cc
index e44f713cdb31828010ad9be92739a3d021dc4685..5301e74a485dea2f6b236281dec1dcaa4947c323 100644
--- a/base/file_util.cc
+++ b/base/file_util.cc
@@ -126,7 +126,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,
bartfab (slow) 2014/02/11 12:51:12 Nit: Align the arguments.
kaliamoorthi 2014/02/11 16:29:16 Done.
+ size_t maxsize) {
if (path.ReferencesParent())
return false;
FILE* file = OpenFile(path, "rb");
@@ -136,7 +138,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/11 12:51:12 Nit: What do you mean by incorrect size? IIUC, the
kaliamoorthi 2014/02/11 16:29:16 Some files especially procfs have incorrect size (
+ // hence the file is read sequentially as opposed to one-shot read.
while ((len = fread(buf, 1, sizeof(buf), file)) > 0) {
+ if ((maxsize - size) < len) {
bartfab (slow) 2014/02/11 12:51:12 Nit: A matter of taste but I think |len > maxsize
kaliamoorthi 2014/02/11 16:29:16 Since it is not a coding guideline, I think the cu
+ if (contents)
+ contents->clear();
+ CloseFile(file);
+ return false;
+ }
+ size += len;
if (contents)
contents->append(buf, len);
}
@@ -145,6 +158,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());
bartfab (slow) 2014/02/11 12:51:12 Nit: #include <limits>
kaliamoorthi 2014/02/11 16:29:16 Done.
+}
+
bool IsDirectoryEmpty(const FilePath& dir_path) {
FileEnumerator files(dir_path, false,
FileEnumerator::FILES | FileEnumerator::DIRECTORIES);

Powered by Google App Engine
This is Rietveld 408576698