From 4172308cd92025da36e3e1d4899a52397aae6bea Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Fri, 25 Oct 2024 18:13:52 -0700 Subject: [PATCH] pipeline: escape log command line --- src/pipeline.cc | 7 ++----- src/project.cc | 14 ++++++++++++++ src/project.hh | 1 + src/sema_manager.cc | 7 ++----- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/pipeline.cc b/src/pipeline.cc index ee3055a1..02b0490c 100644 --- a/src/pipeline.cc +++ b/src/pipeline.cc @@ -378,11 +378,8 @@ bool indexer_Parse(SemaManager *completion, WorkingFiles *wfiles, (Twine(deleted ? "delete " : "parse ") + path_to_index).toVector(msg); if (n_errs) msg += " error:" + std::to_string(n_errs) + ' ' + first_error; - if (LOG_V_ENABLED(1)) { - msg += "\n "; - for (const char *arg : entry.args) - (msg += ' ') += arg; - } + if (LOG_V_ENABLED(1)) + msg += "\n " + escapeArgs(entry.args); LOG_S(INFO) << std::string_view(msg.data(), msg.size()); } diff --git a/src/project.cc b/src/project.cc index 6910a4be..e9eb3faa 100644 --- a/src/project.cc +++ b/src/project.cc @@ -59,6 +59,20 @@ std::pair lookupExtension(std::string_view filename) { return {ret, header}; } +std::string escapeArgs(const std::vector &args) { + std::string ret; + for (const char *arg_c : args) { + if (ret.size()) + ret += ' '; + std::string_view arg(arg_c); + if (arg.find_first_of("\"()<>") != std::string::npos) + ((ret += "'") += arg) += "'"; + else + ret += arg; + } + return ret; +} + namespace { enum OptionClass { diff --git a/src/project.hh b/src/project.hh index d7bf12c7..2eace444 100644 --- a/src/project.hh +++ b/src/project.hh @@ -16,6 +16,7 @@ namespace ccls { struct WorkingFiles; std::pair lookupExtension(std::string_view filename); +std::string escapeArgs(const std::vector &args); struct Project { struct Entry { diff --git a/src/sema_manager.cc b/src/sema_manager.cc index bf806e47..a36f9504 100644 --- a/src/sema_manager.cc +++ b/src/sema_manager.cc @@ -768,11 +768,8 @@ SemaManager::ensureSession(const std::string &path, bool *created) { session = std::make_shared( project_->findEntry(path, false, false), wfiles, pch); std::string line; - if (LOG_V_ENABLED(1)) { - line = "\n "; - for (auto &arg : session->file.args) - (line += ' ') += arg; - } + if (LOG_V_ENABLED(1)) + line = "\n " + escapeArgs(session->file.args); LOG_S(INFO) << "create session for " << path << line; sessions.insert(path, session); if (created)