Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When creating explorer favorite use more specific windows functions #5690

Merged
merged 8 commits into from
Apr 27, 2017
23 changes: 19 additions & 4 deletions src/libsync/utility_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,36 @@
#include <shlobj.h>
#include <winbase.h>
#include <windows.h>
#include <shlguid.h>
#include <string>
#include <QLibrary>

static const char runPathC[] = "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run";



static void setupFavLink_private(const QString &folder)
{
// Windows Explorer: Place under "Favorites" (Links)
wchar_t path[MAX_PATH];
SHGetSpecialFolderPath(0, path, CSIDL_PROFILE, FALSE);
QString profile = QDir::fromNativeSeparators(QString::fromWCharArray(path));

QString linkName;
QDir folderDir(QDir::fromNativeSeparators(folder));
QString linkName = profile+QLatin1String("/Links/") + folderDir.dirName() + QLatin1String(".lnk");

/* Use new WINAPI functions */
PWSTR path;

if(SHGetKnownFolderPath(FOLDERID_Links, 0, NULL, &path) == S_OK) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the documentation about the path argument:

The calling process is responsible for freeing this resource once it is no longer needed by calling CoTaskMemFree.

Could you verify or fix it?

QString links = QDir::fromNativeSeparators(QString::fromWCharArray(path));
linkName = QDir(links).filePath(folderDir.dirName() + QLatin1String(".lnk"));
}
qDebug() << Q_FUNC_INFO << " creating link from " << linkName << " to " << folder;
CoTaskMemFree(path);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will crash if path is not null and yet not pointing to a valid allocation, which can happen if SHGetKnownFolderPath doesn't touch it. You could either move CoTaskMemFree into the if(){}, or initialize path to NULL.

if (!QFile::link(folder, linkName))
qDebug() << Q_FUNC_INFO << "linking" << folder << "to" << linkName << "failed!";

}


bool hasLaunchOnStartup_private(const QString &appName)
{
QString runPath = QLatin1String(runPathC);
Expand Down