diff options
Diffstat (limited to 'src/zenutil/zenserverprocess.cpp')
| -rw-r--r-- | src/zenutil/zenserverprocess.cpp | 42 |
1 files changed, 36 insertions, 6 deletions
diff --git a/src/zenutil/zenserverprocess.cpp b/src/zenutil/zenserverprocess.cpp index 2d4334ffa..16232333f 100644 --- a/src/zenutil/zenserverprocess.cpp +++ b/src/zenutil/zenserverprocess.cpp @@ -15,6 +15,7 @@ #include <zencore/timer.h> #include <atomic> +#include <cctype> #include <string> #include <gsl/gsl-lite.hpp> @@ -555,6 +556,28 @@ ZenServerState::ZenServerEntry::AddSponsorProcess(uint32_t PidToAdd, uint64_t Ti static constexpr size_t kInstanceInfoSize = 4096; +// Token-aware search for a CLI flag (e.g. "--parent-session") within an +// argument string. Avoids false positives like "--parent-session-foo" by +// requiring the match to start at the beginning or after whitespace, and to +// end at the end of the string, at '=', or at whitespace. +static bool +HasCliFlag(std::string_view Args, std::string_view Flag) +{ + size_t Pos = 0; + while ((Pos = Args.find(Flag, Pos)) != std::string_view::npos) + { + const bool LeftOk = (Pos == 0) || std::isspace(static_cast<unsigned char>(Args[Pos - 1])); + const size_t End = Pos + Flag.size(); + const bool RightOk = (End == Args.size()) || (Args[End] == '=') || std::isspace(static_cast<unsigned char>(Args[End])); + if (LeftOk && RightOk) + { + return true; + } + Pos = End; + } + return false; +} + ZenServerInstanceInfo::ZenServerInstanceInfo() = default; ZenServerInstanceInfo::~ZenServerInstanceInfo() @@ -1119,9 +1142,10 @@ ZenServerInstance::SpawnServerInternal(int ChildId, std::string_view ServerArgs, ExtendableStringBuilder<512> CommandLine; { - const std::string ExeUtf8 = PathToUtf8(Executable); + ExtendableStringBuilder<260> ExeUtf8; + PathToUtf8(Executable, ExeUtf8); constexpr AsciiSet QuoteChars = " \t\""; - if (AsciiSet::HasAny(ExeUtf8.c_str(), QuoteChars)) + if (AsciiSet::HasAny(ExeUtf8.ToView(), QuoteChars)) { CommandLine << '"' << ExeUtf8 << '"'; } @@ -1147,6 +1171,11 @@ ZenServerInstance::SpawnServerInternal(int ChildId, std::string_view ServerArgs, CommandLine << " --enable-execution-history=false"; } + if (!HasCliFlag(ServerArgs, "--parent-session")) + { + CommandLine << " --parent-session " << GetSessionIdString(); + } + if (!ServerArgs.empty()) { CommandLine << " " << ServerArgs; @@ -1246,7 +1275,7 @@ ZenServerInstance::SpawnServer(int BasePort, std::string_view AdditionalServerAr CommandLine << " --test --log-id " << m_Name; CommandLine << " --no-sentry"; - if (AdditionalServerArgs.find("--system-dir") == std::string_view::npos) + if (!HasCliFlag(AdditionalServerArgs, "--system-dir")) { CommandLine << " --system-dir "; PathToUtf8((m_Env.CreateNewTestDir() / "system-dir").c_str(), CommandLine); @@ -1665,10 +1694,11 @@ StartupZenServer(LoggerRef LogRef, const StartupZenServerOptions& Options) ZenServerInstance Server(ServerEnvironment, Options.Mode); Server.SetEnableExecutionHistory(Options.EnableExecutionHistory); - std::string ServerArguments(Options.ExtraArgs); - if ((Options.Port != 0) && (ServerArguments.find("--port") == std::string::npos)) + ExtendableStringBuilder<256> ServerArguments; + ServerArguments << Options.ExtraArgs; + if (Options.Port != 0 && !HasCliFlag(ServerArguments, "--port")) { - ServerArguments.append(fmt::format(" --port {}", Options.Port)); + ServerArguments << " --port " << Options.Port; } Server.SpawnServer(ServerArguments, Options.OpenConsole, /*WaitTimeoutMs*/ 0); |