From 1f5649ef2819c846c1d53c8749a91daaef1b9711 Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Fri, 31 Dec 2021 01:39:39 +0100 Subject: [PATCH] Fix vfork edge-case Calling c_str() after vfork is prohibited after a successful vfork. Also the string needs to remain in scope until exec() completed. --- libs/pbd/openuri.cc | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/libs/pbd/openuri.cc b/libs/pbd/openuri.cc index 336ec79d75..451f37cb35 100644 --- a/libs/pbd/openuri.cc +++ b/libs/pbd/openuri.cc @@ -39,6 +39,7 @@ # include #else # include +# include # include #endif @@ -70,9 +71,18 @@ PBD::open_uri (const char* uri) while (s.find("\"") != std::string::npos) s.replace(s.find("\\"), 1, "\\\""); - if (::vfork () == 0) { - ::execlp ("xdg-open", "xdg-open", s.c_str(), (char*)NULL); + char const* arg = s.c_str(); + + pid_t pid = ::vfork (); + + if (pid == 0) { + ::execlp ("xdg-open", "xdg-open", arg, (char*)NULL); _exit (EXIT_SUCCESS); + } else if (pid > 0) { + /* wait until started, keep std::string s in scope */ + ::waitpid (pid, 0, 0); + } else { + return false; } #endif /* not PLATFORM_WINDOWS and not __APPLE__ */