From dba27c3d346211afbd5e5b5793cb4ef060683ff5 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Tue, 21 Nov 2017 21:23:17 -0800 Subject: [PATCH] Catch ios_base::failure exceptions in ReadContent `filename` may be a directory (the latest Emacs lsp-mode sometimes sends a `textDocument/didOpen` message with an empty filename) or the file cannot be read. --- src/utils.cc | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/utils.cc b/src/utils.cc index fec94328..be83fa04 100644 --- a/src/utils.cc +++ b/src/utils.cc @@ -229,11 +229,13 @@ std::istream& SafeGetline(std::istream& is, std::string& t) { optional ReadContent(const std::string& filename) { std::ifstream cache; cache.open(filename); - if (!cache.good()) - return nullopt; - return std::string(std::istreambuf_iterator(cache), - std::istreambuf_iterator()); + try { + return std::string(std::istreambuf_iterator(cache), + std::istreambuf_iterator()); + } catch (std::ios_base::failure&) { + return nullopt; + } } std::vector ReadLines(std::string filename) {