diff options
author | A. Cody Schuffelen <schuffelen@google.com> | 2022-03-31 14:18:50 -0700 |
---|---|---|
committer | A. Cody Schuffelen <schuffelen@google.com> | 2022-07-08 15:56:33 -0700 |
commit | 8a25a057246fe471ae3eec1dc35ba66c5df904f9 (patch) | |
tree | 5e53aa9cdb5bb5ddec597e96b78b7d5b542286d4 | |
parent | 80ea72b461ea5928a8937360db119cc924ed746c (diff) | |
download | cuttlefish-8a25a057246fe471ae3eec1dc35ba66c5df904f9.tar.gz |
Fix crash in `ReadFile` when the input file is empty.
When the internal ifstream is in a `fail()` state `tellg()` returns -1,
and passing that directly to `std::string::resize` causes it to throw an
exception. We can avoid the exception by checking `fail()` first.
Bug: 227657872
Test: Use with aosp/2048065
Change-Id: Ibb262c753ad90d044a0d8c28d439ea699a481b2b
Merged-In: Ibb262c753ad90d044a0d8c28d439ea699a481b2b
-rw-r--r-- | common/libs/utils/files.cpp | 4 |
1 files changed, 4 insertions, 0 deletions
diff --git a/common/libs/utils/files.cpp b/common/libs/utils/files.cpp index 833806ffc..b56c70111 100644 --- a/common/libs/utils/files.cpp +++ b/common/libs/utils/files.cpp @@ -104,6 +104,10 @@ std::string ReadFile(const std::string& file) { std::string contents; std::ifstream in(file, std::ios::in | std::ios::binary); in.seekg(0, std::ios::end); + if (in.fail()) { + // TODO(schuffelen): Return a failing Result instead + return ""; + } contents.resize(in.tellg()); in.seekg(0, std::ios::beg); in.read(&contents[0], contents.size()); |