Skip to content

Commit

Permalink
Windows: use QSystemLibrary instead of LoadLibrary directly (qt#77)
Browse files Browse the repository at this point in the history
* Windows: use QSystemLibrary instead of LoadLibrary directly

Using QSystemLibrary ensures that it will only use the expected copy of
the system library and not one that has been placed in the application's
working directory or elsewhere in the PATH environment variable.

Pick-to: 5.15 6.2 6.3
Change-Id: Ic4234334f73482b38ee5f06345bf11f8c029edc5
Reviewed-by: Laszlo Agocs <laszlo.agocs@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>

* Windows: use QSystemLibrary instead of LoadLibrary directly (5.15)

This is the rest of the patch that is specific to Qt 5.15.

Using QSystemLibrary ensures that it will only use the expected copy of
the system library and not one that has been placed in the application's
working directory or elsewhere in the PATH environment variable.

Change-Id: Ic559957f5ca2e53193a1c230577f877b09e90859

Co-authored-by: Andy Shaw <andy.shaw@qt.io>
Co-authored-by: Keith Kyzivat <keith.kyzivat@qt.io>
  • Loading branch information
3 people authored and GitHub Enterprise committed Sep 23, 2022
1 parent a64e5f8 commit 0e4edc8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/corelib/io/qlockfile_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
#include "QtCore/qdebug.h"
#include "QtCore/qthread.h"

#include "private/qsystemlibrary_p.h"

QT_BEGIN_NAMESPACE

static inline bool fileExists(const wchar_t *fileName)
Expand Down Expand Up @@ -150,7 +152,7 @@ QString QLockFilePrivate::processNameByPid(qint64 pid)
#if !defined(Q_OS_WINRT)
typedef DWORD (WINAPI *GetModuleFileNameExFunc)(HANDLE, HMODULE, LPTSTR, DWORD);

HMODULE hPsapi = LoadLibraryA("psapi");
HMODULE hPsapi = QSystemLibrary::load(L"psapi");
if (!hPsapi)
return QString();
GetModuleFileNameExFunc qGetModuleFileNameEx = reinterpret_cast<GetModuleFileNameExFunc>(
Expand Down
3 changes: 2 additions & 1 deletion src/network/kernel/qauthenticator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include <qstring.h>
#include <qdatetime.h>
#include <qrandom.h>
#include "private/qsystemlibrary_p.h"

#ifdef Q_OS_WIN
#include <qmutex.h>
Expand Down Expand Up @@ -1542,7 +1543,7 @@ static bool q_SSPI_library_load()

// Initialize security interface
if (pSecurityFunctionTable == nullptr) {
securityDLLHandle = LoadLibrary(L"secur32.dll");
securityDLLHandle = QSystemLibrary::load(L"secur32");
if (securityDLLHandle != nullptr) {
INIT_SECURITY_INTERFACE pInitSecurityInterface =
reinterpret_cast<INIT_SECURITY_INTERFACE>(
Expand Down
17 changes: 12 additions & 5 deletions src/plugins/platforms/windows/qwindowsglcontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include <qpa/qplatformnativeinterface.h>
#include <QtPlatformHeaders/qwglnativecontext.h>

#include <private/qsystemlibrary_p.h>
#include <algorithm>

#include <wingdi.h>
Expand Down Expand Up @@ -162,19 +163,25 @@ QFunctionPointer QWindowsOpengl32DLL::resolve(const char *name)

bool QWindowsOpengl32DLL::init(bool softwareRendering)
{
const QByteArray opengl32 = QByteArrayLiteral("opengl32.dll");
const QByteArray swopengl = QByteArrayLiteral("opengl32sw.dll");
const QByteArray opengl32 = QByteArrayLiteral("opengl32");
const QByteArray swopengl = QByteArrayLiteral("opengl32sw");
bool useSystemLib = false;

QByteArray openglDll = qgetenv("QT_OPENGL_DLL");
if (openglDll.isEmpty())
if (openglDll.isEmpty()) {
openglDll = softwareRendering ? swopengl : opengl32;
useSystemLib = !softwareRendering;
}

openglDll = openglDll.toLower();
m_nonOpengl32 = openglDll != opengl32;

qCDebug(lcQpaGl) << "Qt: Using WGL and OpenGL from" << openglDll;

m_lib = ::LoadLibraryA(openglDll.constData());
if (useSystemLib)
m_lib = QSystemLibrary::load((wchar_t*)(QString::fromLatin1(openglDll).utf16()));
else
m_lib = LoadLibraryA(openglDll.constData());
if (!m_lib) {
qErrnoWarning(::GetLastError(), "Failed to load %s", openglDll.constData());
return false;
Expand All @@ -184,7 +191,7 @@ bool QWindowsOpengl32DLL::init(bool softwareRendering)
// Load opengl32.dll always. GDI functions like ChoosePixelFormat do
// GetModuleHandle for opengl32.dll and behave differently (and call back into
// opengl32) when the module is present. This is fine for dummy contexts and windows.
::LoadLibraryA("opengl32.dll");
QSystemLibrary::load(L"opengl32");
}

wglCreateContext = reinterpret_cast<HGLRC (WINAPI *)(HDC)>(resolve("wglCreateContext"));
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/platforms/windows/qwindowsopengltester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#include <QtCore/qstandardpaths.h>
#include <QtCore/qlibraryinfo.h>
#include <QtCore/qhash.h>
#include <private/qsystemlibrary_p.h>

#ifndef QT_NO_OPENGL
#include <private/qopengl_p.h>
Expand Down Expand Up @@ -396,7 +397,7 @@ bool QWindowsOpenGLTester::testDesktopGL()

// Test #1: Load opengl32.dll and try to resolve an OpenGL 2 function.
// This will typically fail on systems that do not have a real OpenGL driver.
lib = LoadLibraryA("opengl32.dll");
lib = QSystemLibrary::load(L"opengl32");
if (lib) {
CreateContext = reinterpret_cast<CreateContextType>(
reinterpret_cast<QFunctionPointer>(::GetProcAddress(lib, "wglCreateContext")));
Expand Down

0 comments on commit 0e4edc8

Please sign in to comment.