From e5affa381702ac5a183582f123f34ab1893a002e Mon Sep 17 00:00:00 2001 From: Fons Rademakers Date: Mon, 8 Jun 2026 11:41:26 +0200 Subject: [PATCH 1/4] Fix obsolete sprinf() calls and other unsafe old C++ constructs. --- tutorials/legacy/rootalias.C | 174 +++++++++++++++++++++++++++-------- 1 file changed, 135 insertions(+), 39 deletions(-) diff --git a/tutorials/legacy/rootalias.C b/tutorials/legacy/rootalias.C index a830bdfcd30f9..7bf17660c133e 100644 --- a/tutorials/legacy/rootalias.C +++ b/tutorials/legacy/rootalias.C @@ -11,71 +11,167 @@ /// /// \author Rene Brun +#include +#include +#include +#include +#include + +#include +#include // std::getenv +#include // std::strcmp +#include // std::replace +#include // std::printf + +namespace { + inline bool IsWindows() { return std::strcmp(gSystem->GetName(), "WinNT") == 0; } + inline bool IsMac() { return std::strcmp(gSystem->GetName(), "Macosx") == 0; } + + // Minimal, pragmatic shell quoting for filenames/paths. + // Good enough for normal paths; not a full shell-escaping library. + std::string QuoteForShell(const std::string& s) + { + if (s.empty()) return "''"; + + if (IsWindows()) { + // Wrap in double quotes; replace embedded " with ' (rare in paths) + std::string q = s; + std::replace(q.begin(), q.end(), '"', '\''); + return "\"" + q + "\""; + } else { + // POSIX: single-quote, escape internal single quotes with '"'"' + std::string out; out.reserve(s.size() + 2); + out.push_back('\''); + for (char c : s) { + if (c == '\'') out += "'\"'\"'"; + else out.push_back(c); + } + out.push_back('\''); + return out; + } + } + + std::string GetEnvOrEmpty(const char* name) + { + if (const char* v = std::getenv(name)) return std::string(v); + return {}; + } + + // Build a command that (on POSIX) returns immediately to keep the ROOT prompt usable. + std::string MaybeBackground(std::string cmd) + { + if (!IsWindows()) cmd += " &"; + return cmd; + } +} + //______________________________________________________________________________ -void edit(char *file) +// Open a file in the user's editor, with robust cross-platform fallbacks. +void edit(const char* file) { - char s[64], *e; - if (!strcmp(gSystem->GetName(), "WinNT")) { - if ((e = std::getenv("EDITOR"))) - sprintf(s, "start %s %s", e, file); + const std::string f = (file ? file : ""); + const std::string qf = QuoteForShell(f); + const std::string editor = GetEnvOrEmpty("EDITOR"); + + std::string cmd; + + if (IsWindows()) { + // Use "start" to detach a new window (cmd.exe builtin). + if (!editor.empty()) + cmd = "start " + editor + " " + qf; else - sprintf(s, "start notepad %s", file); + cmd = "start notepad " + qf; + } else if (IsMac()) { + // macOS: prefer $EDITOR, else TextEdit + if (!editor.empty()) + cmd = MaybeBackground(editor + " " + qf); + else + cmd = MaybeBackground("open -e " + qf); } else { - if ((e = std::getenv("EDITOR"))) - sprintf(s, "%s %s", e, file); + // Linux/Unix: $EDITOR if set; else xdg-open; else xterm+vi + if (!editor.empty()) + cmd = MaybeBackground(editor + " " + qf); else - sprintf(s, "xterm -e vi %s &", file); + cmd = MaybeBackground("(command -v xdg-open >/dev/null 2>&1 && xdg-open " + qf + + ") || (xterm -e vi " + qf + ")"); } - gSystem->Exec(s); + + gSystem->Exec(cmd.c_str()); } //______________________________________________________________________________ -void ls(char *path=0) +// List a directory in a compact, friendly way. +void ls(const char* path = nullptr) { - char s[256]; - strcpy(s, (!strcmp(gSystem->GetName(), "WinNT")) ? "dir /w " : "ls "); - if (path) strcat(s,path); - gSystem->Exec(s); + std::string cmd = IsWindows() ? "dir /w" : "ls"; + if (path && *path) { + cmd += " "; + cmd += QuoteForShell(path); + } + gSystem->Exec(cmd.c_str()); } //______________________________________________________________________________ -void dir(char *path=0) +// More verbose directory view (traditional Unix-y default). +void dir(const char* path = nullptr) { - char s[256]; - strcpy(s,(!strcmp(gSystem->GetName(), "WinNT")) ? "dir " : "ls -l "); - if (path) strcat(s,path); - gSystem->Exec(s); + std::string cmd = IsWindows() ? "dir" : "ls -alF"; + if (path && *path) { + cmd += " "; + cmd += QuoteForShell(path); + } + gSystem->Exec(cmd.c_str()); } //______________________________________________________________________________ -const char *pwd() +// Return current working directory (keeps macro API stable: returns const char*). +const char* pwd() { - return gSystem->WorkingDirectory(); + static std::string wd; // static so c_str() stays valid after return + wd = gSystem->WorkingDirectory(); + return wd.c_str(); } //______________________________________________________________________________ -const char *cd(char *path=0) +// Change directory; if no path is given, just report where we are. +const char* cd(const char* path = nullptr) { - if (path) - gSystem->ChangeDirectory(path); - return pwd(); + if (path && *path) gSystem->ChangeDirectory(path); + return pwd(); } -TCanvas *bench = 0; +// === +// The following benchmark helper (seen in your file) is kept as-is in spirit, +// just minor cleanups for clarity. If you have more helpers in your local copy, +// you can apply the same style: std::string, const-correctness, early returns. +// === + +TCanvas *bench = nullptr; + //______________________________________________________________________________ -void bexec2(char *macro) +// Colorize a macro name in the summary before/after execution and run it. +void bexec2(const char* macro) { - printf("in bexec dir=%s\n",pwd()); - if (gROOT->IsBatch()) printf("Processing benchmark: %s\n",macro); - TPaveText *summary = (TPaveText*)bench->GetPrimitive("TPave"); - TText *tmacro = summary->GetLineWith(macro); - if (tmacro) tmacro->SetTextColor(4); - bench->Modified(); bench->Update(); + std::printf("in bexec dir=%s\n", pwd()); + if (gROOT->IsBatch()) std::printf("Processing benchmark: %s\n", macro); + + if (!bench) { + // If bench isn't prepared yet, just run the macro. + gROOT->Macro(macro); + return; + } + + auto* summary = dynamic_cast(bench->GetPrimitive("TPave")); + if (summary) { + if (auto* tmacro = summary->GetLineWith(macro)) tmacro->SetTextColor(4); + bench->Modified(); bench->Update(); + } gROOT->Macro(macro); - TPaveText *summary2 = (TPaveText*)bench->GetPrimitive("TPave"); - TText *tmacro2 = summary2->GetLineWith(macro); - if (tmacro2) tmacro2->SetTextColor(2); - bench->Modified(); bench->Update(); -} + auto* summary2 = dynamic_cast(bench->GetPrimitive("TPave")); + if (summary2) { + if (auto* tmacro2 = summary2->GetLineWith(macro)) tmacro2->SetTextColor(2); + bench->Modified(); bench->Update(); + } +} \ No newline at end of file From 90112cf41e2acb928ee4a17303cf320723075e64 Mon Sep 17 00:00:00 2001 From: Fons Rademakers Date: Mon, 8 Jun 2026 15:59:20 +0200 Subject: [PATCH 2/4] fixed up by clang-format. --- tutorials/legacy/rootalias.C | 132 ++++++++++++++++++++--------------- 1 file changed, 75 insertions(+), 57 deletions(-) diff --git a/tutorials/legacy/rootalias.C b/tutorials/legacy/rootalias.C index 7bf17660c133e..4d4cec1d6f600 100644 --- a/tutorials/legacy/rootalias.C +++ b/tutorials/legacy/rootalias.C @@ -18,58 +18,70 @@ #include #include -#include // std::getenv -#include // std::strcmp -#include // std::replace -#include // std::printf +#include // std::getenv +#include // std::strcmp +#include // std::replace +#include // std::printf namespace { - inline bool IsWindows() { return std::strcmp(gSystem->GetName(), "WinNT") == 0; } - inline bool IsMac() { return std::strcmp(gSystem->GetName(), "Macosx") == 0; } - - // Minimal, pragmatic shell quoting for filenames/paths. - // Good enough for normal paths; not a full shell-escaping library. - std::string QuoteForShell(const std::string& s) - { - if (s.empty()) return "''"; - - if (IsWindows()) { - // Wrap in double quotes; replace embedded " with ' (rare in paths) - std::string q = s; - std::replace(q.begin(), q.end(), '"', '\''); - return "\"" + q + "\""; - } else { - // POSIX: single-quote, escape internal single quotes with '"'"' - std::string out; out.reserve(s.size() + 2); - out.push_back('\''); - for (char c : s) { - if (c == '\'') out += "'\"'\"'"; - else out.push_back(c); - } - out.push_back('\''); - return out; +inline bool IsWindows() +{ + return std::strcmp(gSystem->GetName(), "WinNT") == 0; +} +inline bool IsMac() +{ + return std::strcmp(gSystem->GetName(), "Macosx") == 0; +} + +// Minimal, pragmatic shell quoting for filenames/paths. +// Good enough for normal paths; not a full shell-escaping library. +std::string QuoteForShell(const std::string &s) +{ + if (s.empty()) + return "''"; + + if (IsWindows()) { + // Wrap in double quotes; replace embedded " with ' (rare in paths) + std::string q = s; + std::replace(q.begin(), q.end(), '"', '\''); + return "\"" + q + "\""; + } else { + // POSIX: single-quote, escape internal single quotes with '"'"' + std::string out; + out.reserve(s.size() + 2); + out.push_back('\''); + for (char c : s) { + if (c == '\'') + out += "'\"'\"'"; + else + out.push_back(c); } + out.push_back('\''); + return out; } +} - std::string GetEnvOrEmpty(const char* name) - { - if (const char* v = std::getenv(name)) return std::string(v); - return {}; - } +std::string GetEnvOrEmpty(const char *name) +{ + if (const char *v = std::getenv(name)) + return std::string(v); + return {}; +} - // Build a command that (on POSIX) returns immediately to keep the ROOT prompt usable. - std::string MaybeBackground(std::string cmd) - { - if (!IsWindows()) cmd += " &"; - return cmd; - } +// Build a command that (on POSIX) returns immediately to keep the ROOT prompt usable. +std::string MaybeBackground(std::string cmd) +{ + if (!IsWindows()) + cmd += " &"; + return cmd; } +} // namespace //______________________________________________________________________________ // Open a file in the user's editor, with robust cross-platform fallbacks. -void edit(const char* file) +void edit(const char *file) { - const std::string f = (file ? file : ""); + const std::string f = (file ? file : ""); const std::string qf = QuoteForShell(f); const std::string editor = GetEnvOrEmpty("EDITOR"); @@ -92,8 +104,8 @@ void edit(const char* file) if (!editor.empty()) cmd = MaybeBackground(editor + " " + qf); else - cmd = MaybeBackground("(command -v xdg-open >/dev/null 2>&1 && xdg-open " + qf + - ") || (xterm -e vi " + qf + ")"); + cmd = + MaybeBackground("(command -v xdg-open >/dev/null 2>&1 && xdg-open " + qf + ") || (xterm -e vi " + qf + ")"); } gSystem->Exec(cmd.c_str()); @@ -101,7 +113,7 @@ void edit(const char* file) //______________________________________________________________________________ // List a directory in a compact, friendly way. -void ls(const char* path = nullptr) +void ls(const char *path = nullptr) { std::string cmd = IsWindows() ? "dir /w" : "ls"; if (path && *path) { @@ -113,7 +125,7 @@ void ls(const char* path = nullptr) //______________________________________________________________________________ // More verbose directory view (traditional Unix-y default). -void dir(const char* path = nullptr) +void dir(const char *path = nullptr) { std::string cmd = IsWindows() ? "dir" : "ls -alF"; if (path && *path) { @@ -125,18 +137,19 @@ void dir(const char* path = nullptr) //______________________________________________________________________________ // Return current working directory (keeps macro API stable: returns const char*). -const char* pwd() +const char *pwd() { - static std::string wd; // static so c_str() stays valid after return + static std::string wd; // static so c_str() stays valid after return wd = gSystem->WorkingDirectory(); return wd.c_str(); } //______________________________________________________________________________ // Change directory; if no path is given, just report where we are. -const char* cd(const char* path = nullptr) +const char *cd(const char *path = nullptr) { - if (path && *path) gSystem->ChangeDirectory(path); + if (path && *path) + gSystem->ChangeDirectory(path); return pwd(); } @@ -150,10 +163,11 @@ TCanvas *bench = nullptr; //______________________________________________________________________________ // Colorize a macro name in the summary before/after execution and run it. -void bexec2(const char* macro) +void bexec2(const char *macro) { std::printf("in bexec dir=%s\n", pwd()); - if (gROOT->IsBatch()) std::printf("Processing benchmark: %s\n", macro); + if (gROOT->IsBatch()) + std::printf("Processing benchmark: %s\n", macro); if (!bench) { // If bench isn't prepared yet, just run the macro. @@ -161,17 +175,21 @@ void bexec2(const char* macro) return; } - auto* summary = dynamic_cast(bench->GetPrimitive("TPave")); + auto *summary = dynamic_cast(bench->GetPrimitive("TPave")); if (summary) { - if (auto* tmacro = summary->GetLineWith(macro)) tmacro->SetTextColor(4); - bench->Modified(); bench->Update(); + if (auto *tmacro = summary->GetLineWith(macro)) + tmacro->SetTextColor(4); + bench->Modified(); + bench->Update(); } gROOT->Macro(macro); - auto* summary2 = dynamic_cast(bench->GetPrimitive("TPave")); + auto *summary2 = dynamic_cast(bench->GetPrimitive("TPave")); if (summary2) { - if (auto* tmacro2 = summary2->GetLineWith(macro)) tmacro2->SetTextColor(2); - bench->Modified(); bench->Update(); + if (auto *tmacro2 = summary2->GetLineWith(macro)) + tmacro2->SetTextColor(2); + bench->Modified(); + bench->Update(); } } \ No newline at end of file From 7c25e30c903aa93003a4bdb1e3f1d501a92ba254 Mon Sep 17 00:00:00 2001 From: Fons Rademakers Date: Mon, 8 Jun 2026 15:42:20 +0200 Subject: [PATCH 3/4] add newline. --- tutorials/legacy/rootalias.C | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tutorials/legacy/rootalias.C b/tutorials/legacy/rootalias.C index 4d4cec1d6f600..3f064ac185c67 100644 --- a/tutorials/legacy/rootalias.C +++ b/tutorials/legacy/rootalias.C @@ -192,4 +192,4 @@ void bexec2(const char *macro) bench->Modified(); bench->Update(); } -} \ No newline at end of file +} From 297bbdc374c6fd598dd91ec13912c0bbf6bf4047 Mon Sep 17 00:00:00 2001 From: Fons Rademakers Date: Mon, 8 Jun 2026 16:00:18 +0200 Subject: [PATCH 4/4] fixup! add newline.