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

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
« no previous file with comments | « base/file_util.h ('k') | base/file_util_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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);
« no previous file with comments | « base/file_util.h ('k') | base/file_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698