Commit c5fa8cc8 authored by Ahmad Samir's avatar Ahmad Samir
Browse files

SmtpJob: Fix crash in startLoginJob

- The user tries to connect to an smtp server, the password dialog is shown
  starting a nested eventloop with exec()
- the connection is lost, the smtpjob is killed (due to connect() in startSmtpJob)
- the dialog::exec returns, and the code tries to setError(KilledJobError)
  on an already killed job which leads to a crash

fix the issue by using QDialog::open() to make the code async.

BUG: 421930

!4
(cherry picked from commit 14894bef)
parent 7eafe0ef
Pipeline #39922 failed with stage
in 12 minutes and 46 seconds
......@@ -69,6 +69,8 @@ public:
{
}
void doLogin();
SmtpJob *const q;
KSmtp::Session *session = nullptr;
KSmtp::SessionUiProxy::Ptr uiProxy;
......@@ -228,7 +230,6 @@ void SmtpJob::startLoginJob()
return;
}
auto login = new KSmtp::LoginJob(d->session);
auto user = transport()->userName();
auto passwd = transport()->password();
if ((user.isEmpty() || passwd.isEmpty())
......@@ -238,6 +239,7 @@ void SmtpJob::startLoginJob()
nullptr,
KPasswordDialog::ShowUsernameLine
|KPasswordDialog::ShowKeepPassword);
dlg->setAttribute(Qt::WA_DeleteOnClose, true);
dlg->setPrompt(i18n("You need to supply a username and a password "
"to use this SMTP server."));
dlg->setKeepPassword(transport()->storePassword());
......@@ -245,33 +247,41 @@ void SmtpJob::startLoginJob()
dlg->setUsername(user);
dlg->setPassword(passwd);
bool gotIt = false;
if (dlg->exec()) {
connect(this, &KJob::result, dlg, &QDialog::reject);
connect(dlg, &QDialog::finished, this, [this, dlg](const int result) {
if (result == QDialog::Rejected) {
setError(KilledJobError);
emitResult();
return;
}
transport()->setUserName(dlg->username());
transport()->setPassword(dlg->password());
transport()->setStorePassword(dlg->keepPassword());
transport()->save();
gotIt = true;
}
delete dlg;
if (!gotIt) {
setError(KilledJobError);
emitResult();
return;
}
d->doLogin();
});
dlg->open();
return;
}
// If dlg was Accepted, update passwd
passwd = transport()->password();
d->doLogin();
}
if (transport()->authenticationType() == Transport::EnumAuthenticationType::XOAUTH2) {
void SmtpJobPrivate::doLogin()
{
QString passwd = q->transport()->password();
if (q->transport()->authenticationType() == Transport::EnumAuthenticationType::XOAUTH2) {
passwd = passwd.left(passwd.indexOf(QLatin1Char('\001')));
}
login->setUserName(transport()->userName());
auto login = new KSmtp::LoginJob(session);
login->setUserName(q->transport()->userName());
login->setPassword(passwd);
switch (transport()->authenticationType()) {
switch (q->transport()->authenticationType()) {
case TransportBase::EnumAuthenticationType::PLAIN:
login->setPreferedAuthMode(KSmtp::LoginJob::Plain);
break;
......@@ -294,11 +304,11 @@ void SmtpJob::startLoginJob()
login->setPreferedAuthMode(KSmtp::LoginJob::GSSAPI);
break;
default:
qCWarning(MAILTRANSPORT_SMTP_LOG) << "Unknown authentication mode" << transport()->authenticationTypeString();
qCWarning(MAILTRANSPORT_SMTP_LOG) << "Unknown authentication mode" << q->transport()->authenticationTypeString();
break;
}
switch (transport()->encryption()) {
switch (q->transport()->encryption()) {
case Transport::EnumEncryption::None:
login->setEncryptionMode(KSmtp::LoginJob::Unencrypted);
break;
......@@ -309,12 +319,12 @@ void SmtpJob::startLoginJob()
login->setEncryptionMode(KSmtp::LoginJob::SSLorTLS);
break;
default:
qCWarning(MAILTRANSPORT_SMTP_LOG) << "Unknown encryption mode" << transport()->encryption();
qCWarning(MAILTRANSPORT_SMTP_LOG) << "Unknown encryption mode" << q->transport()->encryption();
break;
}
connect(login, &KJob::result, this, &SmtpJob::slotResult);
addSubjob(login);
q->connect(login, &KJob::result, q, &SmtpJob::slotResult);
q->addSubjob(login);
login->start();
qCDebug(MAILTRANSPORT_SMTP_LOG) << "Login started";
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment