Skip to content

Commit

Permalink
More changes to address review comments and other tidy ups I noticed.
Browse files Browse the repository at this point in the history
  • Loading branch information
gmcode committed May 15, 2015
1 parent 97251a9 commit f655753
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 174 deletions.
19 changes: 6 additions & 13 deletions src/common/FileSystem.cpp
Expand Up @@ -600,20 +600,13 @@ std::string File::ReadAll(std::error_code& err) const
return out;
out.resize(length);
auto actual = Read(&out[0], length, err);
// The size of data read may not always match the file size even if just obtained.
// One reason is that if a file is opened in text mode on Windows \r\n's
// are translated into shorter \n on read/fread resulting in an overall
// smaller string in memory than what is on disk.
// So we resize the string to lose the excess data and ensure the
// string's length() matches it's valid content as actually read.
out.resize(actual);
// The size of data read might not always match the file size reported
// before reading. There can be various reasons for that but one example
// is a file opened in text mode on Windows will result in
// transparent translations of \r\n to \n on read. When that occurs the data
// read will be smaller as the \r\n's that are on disk have become
// just \n in the data returned and the size returned from read/fread
// will reflect that.
// We have to try to account for at least this scenario by resizing the
// string to the size actually read from the size we anticipated.
// If we don't do this, the buffer will be padded with potentially random
// data and even if this is all zeros, the length of the string will
// also account for that and that excess data and will end up being
// processed by the caller which would be an errant thing for it to do.
return out;
}
void File::CopyTo(const File& dest, std::error_code& err) const
Expand Down

0 comments on commit f655753

Please sign in to comment.