From 6f27d706054c2d1bd07a25d24f27cd819de0dbeb Mon Sep 17 00:00:00 2001 From: Comma Device Date: Tue, 13 Apr 2021 13:56:08 -0700 Subject: [PATCH] fix reading procfs files old-commit-hash: 6f21993915dd64ac145ec8a17686b62d909fc8f1 --- selfdrive/common/util.cc | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/selfdrive/common/util.cc b/selfdrive/common/util.cc index 86f29968f6..161a6557c2 100644 --- a/selfdrive/common/util.cc +++ b/selfdrive/common/util.cc @@ -1,4 +1,5 @@ #include +#include #include "common/util.h" @@ -51,11 +52,20 @@ std::string read_file(const std::string& fn) { std::ifstream ifs(fn, std::ios::binary | std::ios::ate); if (ifs) { std::ifstream::pos_type pos = ifs.tellg(); - std::string result; - result.resize(pos); - ifs.seekg(0, std::ios::beg); - ifs.read(result.data(), pos); - if (ifs) return result; + if (pos != std::ios::beg) { + std::string result; + result.resize(pos); + ifs.seekg(0, std::ios::beg); + ifs.read(result.data(), pos); + if (ifs) { + return result; + } + } else { + // handle files created on read, e.g. procfs + std::stringstream buffer; + buffer << ifs.rdbuf(); + return buffer.str(); + } } return ""; }