Skip to content

Commit

Permalink
Fix coverity warnings
Browse files Browse the repository at this point in the history
IB-7930

Signed-off-by: Raul Metsma <[email protected]>
  • Loading branch information
metsma committed Feb 12, 2024
1 parent 65300d9 commit 759863f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 42 deletions.
34 changes: 16 additions & 18 deletions src/controller/command-handlers/certificatereader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include "application.hpp"
#include "signauthutils.hpp"
#include "utils/utils.hpp"
#include "magic_enum/magic_enum.hpp"

using namespace electronic_id;

Expand All @@ -37,9 +36,9 @@ CardCertificateAndPinInfo getCertificateWithStatusAndInfo(const CardInfo::ptr& c
{
const auto certificateBytes = card->eid().getCertificate(certificateType);

auto certificateDer = QByteArray(reinterpret_cast<const char*>(certificateBytes.data()),
int(certificateBytes.size()));
auto certificate = QSslCertificate(certificateDer, QSsl::Der);
QByteArray certificateDer(reinterpret_cast<const char*>(certificateBytes.data()),
int(certificateBytes.size()));
QSslCertificate certificate(certificateDer, QSsl::Der);
if (certificate.isNull()) {
THROW(SmartCardChangeRequiredError,
"Invalid certificate returned by electronic ID " + card->eid().name());
Expand All @@ -59,24 +58,23 @@ CardCertificateAndPinInfo getCertificateWithStatusAndInfo(const CardInfo::ptr& c
subject = QStringLiteral("%1, %2, %3").arg(surName, givenName, serialNumber);
}

auto certInfo = CertificateInfo {certificateType,
certificate.expiryDate() < QDateTime::currentDateTimeUtc(),
certificate.effectiveDate() > QDateTime::currentDateTimeUtc(),
subject,
certificate.issuerInfo(QSslCertificate::CommonName).join(' '),
certificate.effectiveDate().date().toString(Qt::ISODate),
certificate.expiryDate().date().toString(Qt::ISODate)};
auto pinInfo =
PinInfo {certificateType.isAuthentication() ? card->eid().authPinMinMaxLength()
: card->eid().signingPinMinMaxLength(),
certificateType.isAuthentication() ? card->eid().authPinRetriesLeft()
: card->eid().signingPinRetriesLeft(),
card->eid().smartcard().readerHasPinPad()};
CertificateInfo certInfo {certificateType,
certificate.expiryDate() < QDateTime::currentDateTimeUtc(),
certificate.effectiveDate() > QDateTime::currentDateTimeUtc(),
std::move(subject),
certificate.issuerInfo(QSslCertificate::CommonName).join(' '),
certificate.effectiveDate().date().toString(Qt::ISODate),
certificate.expiryDate().date().toString(Qt::ISODate)};
PinInfo pinInfo {certificateType.isAuthentication() ? card->eid().authPinMinMaxLength()
: card->eid().signingPinMinMaxLength(),
certificateType.isAuthentication() ? card->eid().authPinRetriesLeft()
: card->eid().signingPinRetriesLeft(),
card->eid().smartcard().readerHasPinPad()};
if (pinInfo.pinRetriesCount.first == 0) {
pinInfo.pinIsBlocked = true;
}

return {card, certificateDer, certificate, std::move(certInfo), std::move(pinInfo)};
return {card, std::move(certificateDer), certificate, std::move(certInfo), std::move(pinInfo)};
}

} // namespace
Expand Down
2 changes: 1 addition & 1 deletion src/ui/certificatewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ bool CertificateButton::eventFilter(QObject* object, QEvent* event)
void CertificateButton::setCertificateInfo(const CardCertificateAndPinInfo& cardCertPinInfo)
{
CertificateWidgetInfo::setCertificateInfo(cardCertPinInfo);
const auto certInfo = cardCertPinInfo.certInfo;
const auto &certInfo = cardCertPinInfo.certInfo;
setText(
tr("%1 Issuer: %2 Valid: %3 to %4")
.arg(certInfo.subject, certInfo.issuer, certInfo.effectiveDate, certInfo.expiryDate));
Expand Down
48 changes: 26 additions & 22 deletions src/ui/webeiddialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ WebEidDialog::WebEidDialog(QWidget* parent) : WebEidUI(parent), ui(new Private)
setWindowFlag(Qt::CustomizeWindowHint);
setWindowFlag(Qt::WindowTitleHint);
setWindowTitle(QApplication::applicationDisplayName());
setTrText(ui->aboutVersion,
[] { return tr("Version: %1").arg(QApplication::applicationVersion()); });
setTrText(ui->aboutVersion, []() -> QString {
return tr("Version: %1").arg(QApplication::applicationVersion());
});

ui->langButton = new QToolButton(this);
ui->langButton->setObjectName("langButton");
Expand Down Expand Up @@ -255,7 +256,7 @@ void WebEidDialog::showAboutPage()
void WebEidDialog::showFatalErrorPage()
{
auto* d = new WebEidDialog();
d->setTrText(d->ui->messagePageTitleLabel, [] { return tr("Operation failed"); });
d->setTrText(d->ui->messagePageTitleLabel, []() -> QString { return tr("Operation failed"); });
d->ui->fatalError->show();
d->ui->fatalHelp->show();
d->ui->connectCardLabel->hide();
Expand Down Expand Up @@ -289,10 +290,12 @@ void WebEidDialog::onSmartCardStatusUpdate(const RetriableError status)
{
currentCommand = CommandType::INSERT_CARD;

setTrText(ui->connectCardLabel,
[status] { return std::get<0>(retriableErrorToTextTitleAndIcon(status)); });
setTrText(ui->messagePageTitleLabel,
[status] { return std::get<1>(retriableErrorToTextTitleAndIcon(status)); });
setTrText(ui->connectCardLabel, [status]() -> QString {
return std::get<0>(retriableErrorToTextTitleAndIcon(status));
});
setTrText(ui->messagePageTitleLabel, [status]() -> QString {
return std::get<1>(retriableErrorToTextTitleAndIcon(status));
});
ui->cardChipIcon->setPixmap(std::get<2>(retriableErrorToTextTitleAndIcon(status)));

// In case the insert card page is not shown, switch back to it.
Expand Down Expand Up @@ -375,25 +378,25 @@ void WebEidDialog::onSingleCertificateReady(const QUrl& origin,
return;
case CommandType::AUTHENTICATE:
ui->pinInputCertificateInfo->setCertificateInfo(certAndPin);
setTrText(ui->pinInputPageTitleLabel, [] { return tr("Authenticate"); });
setTrText(ui->pinInputDescriptionLabel, [] {
setTrText(ui->pinInputPageTitleLabel, []() -> QString { return tr("Authenticate"); });
setTrText(ui->pinInputDescriptionLabel, []() -> QString {
return tr("By authenticating, I agree to the transfer of my name and personal "
"identification code to the service provider.");
});
setTrText(ui->pinTitleLabel, [useExternalPinDialog] {
setTrText(ui->pinTitleLabel, [useExternalPinDialog]() -> QString {
return useExternalPinDialog
? tr("Please enter PIN for authentication in the PIN dialog window that opens.")
: tr("Enter PIN1 for authentication");
});
break;
case CommandType::SIGN:
ui->pinInputCertificateInfo->setCertificateInfo(certAndPin);
setTrText(ui->pinInputPageTitleLabel, [] { return tr("Signing"); });
setTrText(ui->pinInputDescriptionLabel, [] {
setTrText(ui->pinInputPageTitleLabel, []() -> QString { return tr("Signing"); });
setTrText(ui->pinInputDescriptionLabel, []() -> QString {
return tr("By signing, I agree to the transfer of my name and personal identification "
"code to the service provider.");
});
setTrText(ui->pinTitleLabel, [useExternalPinDialog] {
setTrText(ui->pinTitleLabel, [useExternalPinDialog]() -> QString {
return useExternalPinDialog
? tr("Please enter PIN for signing in the PIN dialog window that opens.")
: tr("Enter PIN2 for signing");
Expand Down Expand Up @@ -443,7 +446,7 @@ void WebEidDialog::onVerifyPinFailed(const VerifyPinFailed::Status status, const
// FIXME: don't allow retry in case of UNKNOWN_ERROR
switch (status) {
case Status::RETRY_ALLOWED:
message = [retriesLeft] {
message = [retriesLeft]() -> QString {
return retriesLeft == -1 ? tr("Incorrect PIN.")
: tr("Incorrect PIN, %n attempts left.", nullptr, retriesLeft);
};
Expand Down Expand Up @@ -516,9 +519,9 @@ template <typename Text>
void WebEidDialog::onRetryImpl(Text text)
{
setTrText(ui->connectCardLabel, std::forward<Text>(text));
setTrText(ui->messagePageTitleLabel, [] { return tr("Operation failed"); });
setTrText(ui->messagePageTitleLabel, []() -> QString { return tr("Operation failed"); });
ui->cardChipIcon->setPixmap(pixmap("no-id-card"_L1));
setupOK([this] { emit retry(); }, [] { return tr("Try again"); }, true);
setupOK([this] { emit retry(); }, []() -> QString { return tr("Try again"); }, true);
ui->pageStack->setCurrentIndex(int(Page::ALERT));
}

Expand All @@ -534,7 +537,7 @@ void WebEidDialog::setTrText(QWidget* label, Text text) const
void WebEidDialog::connectOkToCachePinAndEmitSelectedCertificate(
const CardCertificateAndPinInfo& certAndPin)
{
setupOK([this, certAndPin] {
setupOK([this, certAndPin = std::forward<const CardCertificateAndPinInfo&>(certAndPin)] {
ui->pinInput->hide();
ui->pinTitleLabel->hide();
ui->pinErrorLabel->hide();
Expand Down Expand Up @@ -585,7 +588,7 @@ void WebEidDialog::setupPinPrompt(const PinInfo& pinInfo)
ui->pinErrorLabel->setVisible(showPinError);
showPinInputWarning(showPinError);
if (showPinError) {
setTrText(ui->pinErrorLabel, [pinInfo] {
setTrText(ui->pinErrorLabel, [pinInfo]() -> QString {
return tr("The PIN has been entered incorrectly at least once. %n attempts left.",
nullptr, int(pinInfo.pinRetriesCount.first));
});
Expand All @@ -602,7 +605,7 @@ void WebEidDialog::setupPinPadProgressBarAndEmitWait(const CardCertificateAndPin
ui->selectAnotherCertificate->hide();
ui->pinTimeRemaining->setText(
tr("Time remaining: <b>%1</b>").arg(ui->pinEntryTimeoutProgressBar->maximum()));
setTrText(ui->pinTitleLabel, [this] {
setTrText(ui->pinTitleLabel, [this]() -> QString {
return tr("Please enter %1 in PinPad reader")
.arg(currentCommand == CommandType::AUTHENTICATE ? tr("PIN1 for authentication")
: tr("PIN2 for signing"));
Expand All @@ -629,7 +632,7 @@ void WebEidDialog::setupPinInput(const CardCertificateAndPinInfo& certAndPin)
// 4. Special characters
// (ASCII 0x20...0x2F, space../ + 0x3A...0x40, :..@ + 0x5B...0x60, [..` + 0x7B...0x7F, {..~).
// 5. We additionally allow uppercase and lowercase Unicode letters.
const auto regexpWithOrWithoutLetters =
const auto &regexpWithOrWithoutLetters =
certAndPin.cardInfo->eid().allowsUsingLettersAndSpecialCharactersInPin()
? QStringLiteral("[0-9 -/:-@[-`{-~\\p{L}]{%1,%2}")
: QStringLiteral("[0-9]{%1,%2}");
Expand All @@ -650,7 +653,7 @@ void WebEidDialog::setupOK(Func&& func, const std::function<QString()>& text, bo
ui->okButton->show();
ui->okButton->setEnabled(enabled);
setTrText(
ui->okButton, text ? text : [] { return tr("Confirm"); });
ui->okButton, text ? text : []() -> QString { return tr("Confirm"); });
ui->cancelButton->show();
ui->cancelButton->setEnabled(true);
ui->helpButton->hide();
Expand All @@ -663,7 +666,8 @@ void WebEidDialog::displayPinBlockedError()
ui->pinTimeoutTimer->stop();
ui->pinTimeRemaining->hide();
ui->pinEntryTimeoutProgressBar->hide();
setTrText(ui->pinErrorLabel, [] { return tr("PIN is locked. Unblock and try again."); });
setTrText(ui->pinErrorLabel,
[]() -> QString { return tr("PIN is locked. Unblock and try again."); });
ui->pinErrorLabel->show();
ui->okButton->hide();
ui->cancelButton->setEnabled(true);
Expand Down

0 comments on commit 759863f

Please sign in to comment.