Skip to content

Commit

Permalink
Fix coverity warnings (open-eid#1247)
Browse files Browse the repository at this point in the history
IB-7930

Signed-off-by: Raul Metsma <[email protected]>
  • Loading branch information
metsma authored Mar 1, 2024
1 parent fb81c88 commit 5693de9
Show file tree
Hide file tree
Showing 21 changed files with 99 additions and 91 deletions.
19 changes: 9 additions & 10 deletions client/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ class DigidocConf final: public digidoc::XmlConfCurrent
std::vector<digidoc::X509Cert> TSLCerts() const final
{
std::vector<digidoc::X509Cert> tslcerts = toCerts(QLatin1String("TSL-CERTS"));
return tslcerts.empty() ? digidoc::XmlConfCurrent::TSLCerts() : tslcerts;
return tslcerts.empty() ? digidoc::XmlConfCurrent::TSLCerts() : std::move(tslcerts);
}

digidoc::X509Cert verifyServiceCert() const final
Expand Down Expand Up @@ -334,7 +334,7 @@ Application::Application( int &argc, char **argv )

#ifdef CONFIG_URL
d->conf = new Configuration(this);
QTimer::singleShot(0, this, [this] {
QMetaObject::invokeMethod(this, [this] {
auto lessThanVersion = [](QLatin1String key) {
return QVersionNumber::fromString(applicationVersion()) <
QVersionNumber::fromString(confValue(key).toString());
Expand Down Expand Up @@ -456,7 +456,7 @@ Application::Application( int &argc, char **argv )
return;
}

QTimer::singleShot(0, this, [this] {
QMetaObject::invokeMethod(this, [this] {
#ifdef Q_OS_MAC
if(!Settings::PLUGINS.isSet())
{
Expand Down Expand Up @@ -486,7 +486,7 @@ Application::Application( int &argc, char **argv )
});

if( !args.isEmpty() || topLevelWindows().isEmpty() )
parseArgs( args );
parseArgs(std::move(args));
}

Application::~Application()
Expand Down Expand Up @@ -626,7 +626,7 @@ bool Application::event(QEvent *event)
case QEvent::FileOpen:
{
QString fileName = static_cast<QFileOpenEvent*>(event)->file().normalized(QString::NormalizationForm_C);
QTimer::singleShot(0, this, [fileName] {
QMetaObject::invokeMethod(this, [fileName = std::move(fileName)] {
parseArgs({ fileName });
});
return true;
Expand Down Expand Up @@ -669,7 +669,7 @@ void Application::mailTo( const QUrl &url )
{
QUrlQuery q(url);
#if defined(Q_OS_WIN)
if(QLibrary lib("mapi32"); LPMAPISENDMAILW mapi = LPMAPISENDMAILW(lib.resolve("MAPISendMailW")))
if(QLibrary lib("mapi32"); auto mapi = LPMAPISENDMAILW(lib.resolve("MAPISendMailW")))
{
QString file = q.queryItemValue( "attachment", QUrl::FullyDecoded );
QString filePath = QDir::toNativeSeparators( file );
Expand Down Expand Up @@ -704,9 +704,8 @@ void Application::mailTo( const QUrl &url )
"--group", "PROFILE_Default",
"--key", "EmailClient"});
p.waitForFinished();
QByteArray data = p.readAllStandardOutput().trimmed();
if( data.contains("thunderbird") )
thunderbird = data;
if(QByteArray data = p.readAllStandardOutput().trimmed(); data.contains("thunderbird"))
thunderbird = std::move(data);
}
else if(env.indexOf(QRegularExpression(QStringLiteral("GNOME_DESKTOP_SESSION_ID.*"))) != -1)
{
Expand Down Expand Up @@ -800,7 +799,7 @@ void Application::parseArgs( const QString &msg )
QUrl url( param, QUrl::StrictMode );
params.append(param != QLatin1String("-crypto") && !url.toLocalFile().isEmpty() ? url.toLocalFile() : param);
}
parseArgs( params );
parseArgs(std::move(params));
}

void Application::parseArgs(QStringList args)
Expand Down
8 changes: 4 additions & 4 deletions client/CDoc2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,10 +425,10 @@ CDoc2::CDoc2(const QString &path)
return;
setLastError({});

auto toByteArray = [](const flatbuffers::Vector<uint8_t> *data) {
auto toByteArray = [](const flatbuffers::Vector<uint8_t> *data) -> QByteArray {
return data ? QByteArray((const char*)data->Data(), int(data->size())) : QByteArray();
};
auto toString = [](const flatbuffers::String *data) {
auto toString = [](const flatbuffers::String *data) -> QString {
return data ? QString::fromUtf8(data->c_str(), data->size()) : QString();
};
for(const auto *recipient: *recipients){
Expand Down Expand Up @@ -734,9 +734,9 @@ QByteArray CDoc2::transportKey(const CKey &_key)
QByteArray key_material = QByteArray::fromBase64(
json.value(QLatin1String("ephemeral_key_material")).toString().toLatin1());
if(json.value(QLatin1String("capsule_type")) == QLatin1String("rsa"))
key.encrypted_kek = key_material;
key.encrypted_kek = std::move(key_material);
else
key.publicKey = key_material;
key.publicKey = std::move(key_material);
}
#ifndef NDEBUG
qDebug() << "publicKeyDer" << key.key.toHex();
Expand Down
2 changes: 1 addition & 1 deletion client/CryptoDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ void CDocumentModel::open(int row)
d->tempFiles.append(path);
FileDialog::setReadOnly(path);
if(FileDialog::isSignedPDF(path))
Application::showClient({ path }, false, false, true);
Application::showClient({ std::move(path) }, false, false, true);
else
QDesktopServices::openUrl(QUrl::fromLocalFile(path));
}
Expand Down
4 changes: 2 additions & 2 deletions client/DigiDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ void SDocumentModel::open(int row)
doc->m_tempFiles.append(path);
FileDialog::setReadOnly(path);
if(!doc->fileName().endsWith(QLatin1String(".pdf"), Qt::CaseInsensitive) && FileDialog::isSignedPDF(path))
qApp->showClient({ path }, false, false, true);
qApp->showClient({ std::move(path) }, false, false, true);
else
QDesktopServices::openUrl(QUrl::fromLocalFile(path));
}
Expand All @@ -372,7 +372,7 @@ bool SDocumentModel::removeRow(int row)
emit removed(row);
return true;
}
catch( const Exception &e ) { doc->setLastError( tr("Failed remove document from container"), e ); }
catch( const Exception &e ) { DigiDoc::setLastError(tr("Failed remove document from container"), e); }
return false;
}

Expand Down
32 changes: 20 additions & 12 deletions client/IKValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,20 @@
#include <QtCore/QDate>
#include <QtCore/QStringList>

QDate IKValidator::birthDate(const QString &ik)
inline int toInt(QStringView str) {
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
return QLocale::system().toInt(str); // QLocale, workaround for Qt 5
#else
return str.toInt();
#endif
}

QDate IKValidator::birthDate(QStringView ik)
{
if(ik.size() != 11) return {};

quint16 year = 0;
switch(ik.left(1).toUInt())
switch(toInt(ik.left(1)))
{
case 1: case 2: year = 1800; break;
case 3: case 4: year = 1900; break;
Expand All @@ -37,13 +45,13 @@ QDate IKValidator::birthDate(const QString &ik)
}

QDate date(
ik.mid(1, 2).toInt() + year,
ik.mid(3, 2).toInt(),
ik.mid(5, 2).toInt());
toInt(ik.mid(1, 2)) + year,
toInt(ik.mid(3, 2)),
toInt(ik.mid(5, 2)));
return date.isValid() ? date : QDate();
}

bool IKValidator::isValid(const QString &ik)
bool IKValidator::isValid(QStringView ik)
{
if(ik.size() != 11)
return false;
Expand Down Expand Up @@ -72,18 +80,18 @@ bool IKValidator::isValid(const QString &ik)
int sum1 = 0, sum2 = 0;
for(int i = 0, pos1 = 1, pos2 = 3; i < 10; ++i)
{
sum1 += ik.mid(i, 1).toInt() * pos1;
sum2 += ik.mid(i, 1).toInt() * pos2;
sum1 += toInt(ik.mid(i, 1)) * pos1;
sum2 += toInt(ik.mid(i, 1)) * pos2;
pos1 = pos1 == 9 ? 1 : pos1 + 1;
pos2 = pos2 == 9 ? 1 : pos2 + 1;
}

int result;
int result = 0;
if((result = sum1 % 11) >= 10 &&
(result = sum2 % 11) >= 10)
result = 0;

return ik.right( 1 ).toInt() == result;
return toInt(ik.right(1)) == result;
}


Expand All @@ -95,10 +103,10 @@ NumberValidator::NumberValidator(QObject *parent)
NumberValidator::State NumberValidator::validate(QString &input, int & /*pos*/) const
{
QString out;
QRegularExpression rx(QStringLiteral("(\\d+)"));
static const QRegularExpression rx(QStringLiteral("(\\d+)"));
QRegularExpressionMatch match = rx.match(input);
for(int i = 0; i < match.lastCapturedIndex(); ++i)
out += match.captured(i);
input = out;
input = std::move(out);
return Acceptable;
}
10 changes: 5 additions & 5 deletions client/IKValidator.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@
class IKValidator
{
public:
static QDate birthDate(const QString &ik);
static bool isValid(const QString &ik);
static QDate birthDate(QStringView ik);
static bool isValid(QStringView ik);
};



class NumberValidator: public QValidator
class NumberValidator final: public QValidator
{
Q_OBJECT
public:
explicit NumberValidator(QObject *parent = 0);
explicit NumberValidator(QObject *parent = nullptr);

State validate(QString &input, int &pos) const;
State validate(QString &input, int &pos) const final;
};
4 changes: 2 additions & 2 deletions client/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ void MainWindow::sign(F &&sign)
if(!sign(city, state, zip, country, role))
{
resetDigiDoc(nullptr, false);
navigateToPage(SignDetails, {wrappedFile}, false);
navigateToPage(SignDetails, {std::move(wrappedFile)}, false);
return;
}
}
Expand Down Expand Up @@ -1145,7 +1145,7 @@ void MainWindow::containerSummary()
return;
}
#endif
QPrintPreviewDialog *dialog = new QPrintPreviewDialog( this );
auto *dialog = new QPrintPreviewDialog( this );
dialog->printer()->setPageSize( QPageSize( QPageSize::A4 ) );
dialog->printer()->setPageOrientation( QPageLayout::Portrait );
dialog->setMinimumHeight( 700 );
Expand Down
2 changes: 1 addition & 1 deletion client/QSigner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ void QSigner::run()
std::sort(cache.begin(), cache.end(), cardsOrder);
if(cache != d->cache)
{
d->cache = cache;
d->cache = std::move(cache);
Q_EMIT cacheChanged();
}
for(const TokenData &t: d->cache)
Expand Down
8 changes: 4 additions & 4 deletions client/QSmartCard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ QSmartCard::ErrorType QSmartCard::Private::handlePinResult(QPCSCReader *reader,
case 0x6402: return QSmartCard::DifferentError;
case 0x6403: return QSmartCard::LenghtError;
case 0x6983: return QSmartCard::BlockedError;
case 0x6985: return QSmartCard::OldNewPinSameError;
case 0x6985:
case 0x6A80: return QSmartCard::OldNewPinSameError;
default: return QSmartCard::UnknownError;
}
Expand Down Expand Up @@ -635,7 +635,7 @@ void QSmartCard::reload()
t->data.clear();
t->authCert = QSslCertificate();
t->signCert = QSslCertificate();
d->t.d = t;
d->t.d = std::move(t);
Q_EMIT dataChanged(d->t);
QTimer::singleShot(0, this, [this] { reloadCard(d->token); });
}
Expand All @@ -659,7 +659,7 @@ void QSmartCard::reloadCard(const TokenData &token)
t->data.clear();
t->authCert = QSslCertificate();
t->signCert = QSslCertificate();
d->t.d = t;
d->t.d = std::move(t);
}

if(!d->t.isNull() || token.reader().isEmpty())
Expand Down Expand Up @@ -690,7 +690,7 @@ void QSmartCard::reloadCard(const TokenData &token)
d->card = new EstEIDCard();
if(d->card->loadPerso(selectedReader.data(), t))
{
d->t.d = t;
d->t.d = std::move(t);
emit dataChanged(d->t);
}
else
Expand Down
7 changes: 4 additions & 3 deletions client/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,22 @@ namespace {
}).detach();
l.exec();
if(exception)
std::rethrow_exception(exception);
std::rethrow_exception(std::move(exception));
return result;
}

template <typename F, class... Args>
inline auto dispatchToMain(F&& function, Args&& ...args) {
QEventLoop l;
if constexpr (std::is_void_v<std::invoke_result_t<F,Args...>>) {
using result_t = typename std::invoke_result_t<F,Args...>;
if constexpr (std::is_void_v<result_t>) {
QMetaObject::invokeMethod(qApp, [&, function = std::forward<F>(function)] {
std::invoke(function, args...);
l.exit();
}, Qt::QueuedConnection);
l.exec();
} else {
std::invoke_result_t<F,Args...> result{};
result_t result{};
QMetaObject::invokeMethod(qApp, [&, function = std::forward<F>(function)] {
result = std::invoke(function, args...);
l.exit();
Expand Down
4 changes: 3 additions & 1 deletion client/dialogs/PinUnblock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ PinUnblock::PinUnblock(WorkMode mode, QWidget *parent, QSmartCardData::PinType t
connect(ui->puk, &QLineEdit::textEdited, ui->errorPuk, [this, setError] {
setError(ui->puk, ui->errorPuk, {});
});
connect(ui->change, &QPushButton::clicked, this, [=] {
connect(ui->change, &QPushButton::clicked, this, [=,
regexpNewCode = std::move(regexpNewCode),
regexpValidateCode = std::move(regexpValidateCode)] {
const static QString SEQUENCE_ASCENDING = QStringLiteral("1234567890123456789012");
const static QString SEQUENCE_DESCENDING = QStringLiteral("0987654321098765432109");
ui->puk->setStyleSheet({});
Expand Down
4 changes: 2 additions & 2 deletions client/dialogs/RoleAddressDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ RoleAddressDialog::RoleAddressDialog(QWidget *parent)
list.first()->setFocus();
for(QLineEdit *line: list)
{
Settings::Option<QStringList> s{line->objectName()};
Settings::Option<QStringList> s{line->objectName(), {}};
auto *completer = new QCompleter(s, line);
completer->setMaxVisibleItems(10);
completer->setCompletionMode(QCompleter::PopupCompletion);
completer->setCaseSensitivity(Qt::CaseInsensitive);
line->setText(QStringList(s).value(0));
line->setFont(regularFont);
line->setCompleter(completer);
connect(line, &QLineEdit::editingFinished, this, [=] {
connect(line, &QLineEdit::editingFinished, this, [line, s = std::move(s)] {
QStringList list = s;
list.removeAll(line->text());
list.insert(0, line->text());
Expand Down
8 changes: 4 additions & 4 deletions client/dialogs/SettingsDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ SettingsDialog::SettingsDialog(int page, QWidget *parent)
WarningDialog::show(this, tr("Checking updates has failed.") + "<br />" + tr("Please try again."), error);
return;
}
auto dlg = WarningDialog::show(this, tr("DigiDoc4 Client configuration update was successful."));
auto *dlg = WarningDialog::show(this, tr("DigiDoc4 Client configuration update was successful."));
new Overlay(dlg);
#ifdef Q_OS_WIN
QString path = QApplication::applicationDirPath() + QLatin1String("/id-updater.exe");
Expand Down Expand Up @@ -424,7 +424,7 @@ void SettingsDialog::initFunctionality()
ui->txtTimeStamp->setEnabled(ui->rdTimeStampCustom->isChecked());
ui->txtTimeStamp->setPlaceholderText(Application::confValue(Settings::TSA_URL.KEY).toString());
QString TSA_URL = Settings::TSA_URL.value(Application::confValue(Application::TSAUrl));
ui->txtTimeStamp->setText(ui->txtTimeStamp->placeholderText() == TSA_URL ? QString() : TSA_URL);
ui->txtTimeStamp->setText(ui->txtTimeStamp->placeholderText() == TSA_URL ? QString() : std::move(TSA_URL));
ui->wgtTSACert->setDisabled(Settings::TSA_CERT.isLocked());
ui->wgtTSACert->setVisible(ui->rdTimeStampCustom->isChecked());
connect(ui->rdTimeStampCustom, &QRadioButton::toggled, ui->txtTimeStamp, [this](bool checked) {
Expand Down Expand Up @@ -481,7 +481,7 @@ void SettingsDialog::initFunctionality()
ui->txtSiVa->setEnabled(ui->rdSiVaCustom->isChecked());
ui->txtSiVa->setPlaceholderText(Application::confValue(Settings::SIVA_URL.KEY).toString());
QString SIVA_URL = Settings::SIVA_URL.value(Application::confValue(Application::SiVaUrl));
ui->txtSiVa->setText(ui->txtSiVa->placeholderText() == SIVA_URL ? QString() : SIVA_URL);
ui->txtSiVa->setText(ui->txtSiVa->placeholderText() == SIVA_URL ? QString() : std::move(SIVA_URL));
ui->wgtSiVaCert->setDisabled(Settings::SIVA_CERT.isLocked());
ui->wgtSiVaCert->setVisible(ui->rdSiVaCustom->isChecked());
connect(ui->rdSiVaCustom, &QRadioButton::toggled, ui->txtSiVa, [this](bool checked) {
Expand Down Expand Up @@ -650,7 +650,7 @@ void SettingsDialog::updateDiagnostics()
ui->btnNavSaveReport->setDisabled(true);

QApplication::setOverrideCursor( Qt::WaitCursor );
Diagnostics *worker = new Diagnostics();
auto *worker = new Diagnostics();
connect(worker, &Diagnostics::update, ui->txtDiagnostics, &QTextBrowser::insertHtml, Qt::QueuedConnection);
connect(worker, &Diagnostics::destroyed, this, [=]{
ui->txtDiagnostics->setEnabled(true);
Expand Down
Loading

0 comments on commit 5693de9

Please sign in to comment.