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.
This commit is contained in:
Robin Gareus 2021-12-31 01:39:39 +01:00
parent 658d8c512e
commit 1f5649ef28
Signed by: rgareus
GPG Key ID: A090BCE02CF57F04

View File

@ -39,6 +39,7 @@
# include <shellapi.h>
#else
# include <sys/types.h>
# include <sys/wait.h>
# include <unistd.h>
#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__ */