From fbed349232650f1a739acf2317a35ee06e89e1d2 Mon Sep 17 00:00:00 2001 From: Lucian Petrut Date: Wed, 18 Nov 2020 13:17:18 +0000 Subject: [PATCH] common: add trailing newline before parsing cfg Ceph fails to parse config files that lack a trailing newline. This mainly affects Windows, where text editors won't add one by default. This issue has been addressed by a previous commit [1] but that code path is no longer reached [2][3], so we need to go down a level. [1] 3b590314ac49b955b2123f03f4c4417e5534ec98 [2] http://paste.openstack.org/raw/800153/ [3] https://github.com/ceph/ceph/commit/e7dcc403b20922ffeeca114899f90a385bf25f0e Signed-off-by: Lucian Petrut --- src/common/ConfUtils.cc | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/common/ConfUtils.cc b/src/common/ConfUtils.cc index 1ab84af80e9a..6231c7a7a3da 100644 --- a/src/common/ConfUtils.cc +++ b/src/common/ConfUtils.cc @@ -136,11 +136,6 @@ int ConfFile::parse_file(const std::string &fname, std::ifstream ifs{fname}; std::string buffer{std::istreambuf_iterator(ifs), std::istreambuf_iterator()}; - #ifdef _WIN32 - // We'll need to ensure that there's a new line at the end of the file, - // otherwise the config parsing will fail. - buffer.append("\n"); - #endif if (parse_buffer(buffer, warnings)) { return 0; } else { @@ -255,17 +250,24 @@ struct IniGrammer : qi::grammar bool ConfFile::parse_buffer(std::string_view buf, std::ostream* err) { assert(err); - if (int err_pos = check_utf8(buf.data(), buf.size()); err_pos > 0) { +#ifdef _WIN32 + // We'll need to ensure that there's a new line at the end of the buffer, + // otherwise the config parsing will fail. + std::string _buf = std::string(buf) + "\n"; +#else + std::string_view _buf = buf; +#endif + if (int err_pos = check_utf8(_buf.data(), _buf.size()); err_pos > 0) { *err << "parse error: invalid UTF-8 found at line " - << std::count(buf.begin(), std::next(buf.begin(), err_pos), '\n') + 1; + << std::count(_buf.begin(), std::next(_buf.begin(), err_pos), '\n') + 1; return false; } - using iter_t = boost::spirit::line_pos_iterator; - iter_t first{buf.begin()}; + using iter_t = boost::spirit::line_pos_iterator; + iter_t first{_buf.begin()}; using skipper_t = qi::rule; IniGrammer grammar{first, *err}; skipper_t skipper = grammar.continue_marker | grammar.comment; - return qi::phrase_parse(first, iter_t{buf.end()}, + return qi::phrase_parse(first, iter_t{_buf.end()}, grammar, skipper, *this); } -- 2.47.3