From d738f0274ef5cbb438ddf5fc8105082818038d29 Mon Sep 17 00:00:00 2001 From: reuk Date: Thu, 12 Aug 2021 17:57:53 +0100 Subject: [PATCH] File: Fix quoting in openDocument() on Linux The previous implementation would fail to open directories with names that contained spaces, as the space would be escaped and then quoted. I don't think it's particularly meaningful to supply parameters when opening a file in this way (especially not quoting the parameters too!) so I've removed that functionality. --- modules/juce_core/native/juce_linux_Files.cpp | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/modules/juce_core/native/juce_linux_Files.cpp b/modules/juce_core/native/juce_linux_Files.cpp index 99893aaa0d..953d81f4b9 100644 --- a/modules/juce_core/native/juce_linux_Files.cpp +++ b/modules/juce_core/native/juce_linux_Files.cpp @@ -199,27 +199,31 @@ static bool isFileExecutable (const String& filename) bool Process::openDocument (const String& fileName, const String& parameters) { - auto cmdString = fileName.replace (" ", "\\ ", false); - cmdString << " " << parameters; - - if (cmdString.startsWithIgnoreCase ("file:") - || File::createFileWithoutCheckingPath (fileName).isDirectory() - || ! isFileExecutable (fileName)) + const auto cmdString = [&] { - StringArray cmdLines; - - for (auto browserName : { "xdg-open", "/etc/alternatives/x-www-browser", "firefox", "mozilla", - "google-chrome", "chromium-browser", "opera", "konqueror" }) + if (fileName.startsWithIgnoreCase ("file:") + || File::createFileWithoutCheckingPath (fileName).isDirectory() + || ! isFileExecutable (fileName)) { - cmdLines.add (String (browserName) + " " + cmdString.trim().quoted()); + const auto singleCommand = fileName.trim().quoted(); + + StringArray cmdLines; + + for (auto browserName : { "xdg-open", "/etc/alternatives/x-www-browser", "firefox", "mozilla", + "google-chrome", "chromium-browser", "opera", "konqueror" }) + { + cmdLines.add (String (browserName) + " " + singleCommand); + } + + return cmdLines.joinIntoString (" || "); } - cmdString = cmdLines.joinIntoString (" || "); - } + return (fileName.replace (" ", "\\ ", false) + " " + parameters).trim(); + }(); - const char* const argv[4] = { "/bin/sh", "-c", cmdString.toUTF8(), nullptr }; + const char* const argv[] = { "/bin/sh", "-c", cmdString.toUTF8(), nullptr }; - auto cpid = fork(); + const auto cpid = fork(); if (cpid == 0) {