Skip to content

Commit 9ac71d5

Browse files
Windows: Free credentials struct in all code paths
1 parent 990abf2 commit 9ac71d5

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

qtkeychain/keychain_win.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ std::pair<QByteArray, QString> protectData(const QByteArray &data)
7777

7878
#if defined(USE_CREDENTIAL_STORE)
7979

80+
struct CredentialDeleter
81+
{
82+
explicit CredentialDeleter(PCREDENTIALW cred) : m_cred(cred) { }
83+
~CredentialDeleter() {
84+
if (m_cred) {
85+
CredFree(m_cred);
86+
}
87+
}
88+
89+
PCREDENTIALW m_cred;
90+
};
91+
8092
/***
8193
* The credentials store has a limit of CRED_MAX_CREDENTIAL_BLOB_SIZE (5* 512)
8294
* As this might not be enough in some scenarios, for bigger payloads we use CryptProtectData which
@@ -90,6 +102,7 @@ std::pair<QByteArray, QString> protectData(const QByteArray &data)
90102
void ReadPasswordJobPrivate::scheduledStart()
91103
{
92104
PCREDENTIALW cred = {};
105+
CredentialDeleter deleter(cred);
93106

94107
if (!CredReadW(reinterpret_cast<const wchar_t *>(key.utf16()), CRED_TYPE_GENERIC, 0, &cred)) {
95108
Error err;
@@ -129,8 +142,6 @@ void ReadPasswordJobPrivate::scheduledStart()
129142
data = result.first;
130143
}
131144

132-
CredFree(cred);
133-
134145
q->emitFinished();
135146
}
136147

0 commit comments

Comments
 (0)