QLocalServer: use std::unique_ptr instead of QScopedPointer

Task-number: QTBUG-132213
Change-Id: I6a18e787efa9a2ecfe89339569a9724467ac3e6a

Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Ahmad Samir 2025-05-17 15:57:14 +03:00
parent 9eb2c404ee
commit ffc5491757

View File

@ -12,6 +12,8 @@
#include <accctrl.h>
#include <sddl.h>
#include <memory>
// The buffer size need to be 0 otherwise data could be
// lost if the socket that has written data closes the connection
// before it is read. Pipewriter is used for write buffering.
@ -33,7 +35,7 @@ bool QLocalServerPrivate::addListener()
sa.bInheritHandle = FALSE; //non inheritable handle, same as default
sa.lpSecurityDescriptor = 0; //default security descriptor
QScopedPointer<SECURITY_DESCRIPTOR> pSD;
std::unique_ptr<SECURITY_DESCRIPTOR> pSD;
PSID worldSID = 0;
QByteArray aclBuffer;
QByteArray tokenUserBuffer;
@ -42,7 +44,7 @@ bool QLocalServerPrivate::addListener()
// create security descriptor if access options were specified
if ((socketOptions.value() & QLocalServer::WorldAccessOption)) {
pSD.reset(new SECURITY_DESCRIPTOR);
if (!InitializeSecurityDescriptor(pSD.data(), SECURITY_DESCRIPTOR_REVISION)) {
if (!InitializeSecurityDescriptor(pSD.get(), SECURITY_DESCRIPTOR_REVISION)) {
setError("QLocalServerPrivate::addListener"_L1);
return false;
}
@ -76,10 +78,14 @@ bool QLocalServerPrivate::addListener()
SID_NAME_USE groupNameUse;
LPWSTR groupNameSid;
LookupAccountSid(0, pTokenGroup->PrimaryGroup, 0, &groupNameSize, 0, &domainNameSize, &groupNameUse);
QScopedPointer<wchar_t, QScopedPointerArrayDeleter<wchar_t>> groupName(new wchar_t[groupNameSize]);
QScopedPointer<wchar_t, QScopedPointerArrayDeleter<wchar_t>> domainName(new wchar_t[domainNameSize]);
if (LookupAccountSid(0, pTokenGroup->PrimaryGroup, groupName.data(), &groupNameSize, domainName.data(), &domainNameSize, &groupNameUse)) {
qDebug() << "primary group" << QString::fromWCharArray(domainName.data()) << "\\" << QString::fromWCharArray(groupName.data()) << "type=" << groupNameUse;
auto groupName = std::unique_ptr<wchar_t[]>(new wchar_t[groupNameSize]);
auto domainName = std::unique_ptr<wchar_t[]>(new wchar_t[domainNameSize]);
const bool lookup = LookupAccountSid(0, pTokenGroup->PrimaryGroup, groupName.get(),
&groupNameSize, domainName.get(), &domainNameSize,
&groupNameUse);
if (lookup) {
qDebug() << "primary group" << QString::fromWCharArray(domainName.get()) << "\\"
<< QString::fromWCharArray(groupName.get()) << "type=" << groupNameUse;
}
if (ConvertSidToStringSid(pTokenGroup->PrimaryGroup, &groupNameSid)) {
qDebug() << "primary group SID" << QString::fromWCharArray(groupNameSid) << "valid" << IsValidSid(pTokenGroup->PrimaryGroup);
@ -127,15 +133,15 @@ bool QLocalServerPrivate::addListener()
return false;
}
}
SetSecurityDescriptorOwner(pSD.data(), pTokenUser->User.Sid, FALSE);
SetSecurityDescriptorGroup(pSD.data(), pTokenGroup->PrimaryGroup, FALSE);
if (!SetSecurityDescriptorDacl(pSD.data(), TRUE, acl, FALSE)) {
SetSecurityDescriptorOwner(pSD.get(), pTokenUser->User.Sid, FALSE);
SetSecurityDescriptorGroup(pSD.get(), pTokenGroup->PrimaryGroup, FALSE);
if (!SetSecurityDescriptorDacl(pSD.get(), TRUE, acl, FALSE)) {
setError("QLocalServerPrivate::addListener"_L1);
FreeSid(worldSID);
return false;
}
sa.lpSecurityDescriptor = pSD.data();
sa.lpSecurityDescriptor = pSD.get();
}
listener->handle = CreateNamedPipe(