Commit 69a36d04 authored by Ingo Klöcker's avatar Ingo Klöcker
Browse files

Do not let the validators be fooled by leading or trailing whitespace

Leading and trailing whitespace is strip from the entered values, so
that we shouldn't take it into account when validating the values.
This fixes the problem that the name validator which checks for a
string of at least 5 characters could be fooled by whitespace.

GnuPG-bug-id: 5916
parent aedb5797
Pipeline #161189 passed with stage
in 3 minutes and 21 seconds
......@@ -25,6 +25,20 @@ using namespace Kleo;
namespace
{
template<class Validator>
class TrimmingValidator : public Validator
{
public:
using Validator::Validator;
QValidator::State validate(QString &str, int &pos) const override
{
auto trimmed = str.trimmed();
auto posCopy = pos;
return Validator::validate(trimmed, posCopy);
}
};
template<class Validator>
class EmptyIsAcceptableValidator : public Validator
{
......@@ -33,7 +47,7 @@ public:
QValidator::State validate(QString &str, int &pos) const override
{
if (str.trimmed().isEmpty()) {
if (str.isEmpty()) {
return QValidator::Acceptable;
}
return Validator::validate(str, pos);
......@@ -58,9 +72,9 @@ public:
QValidator *regularExpressionValidator(Validation::Flags flags, const QString &regexp, QObject *parent)
{
if (flags & Validation::Required) {
return new QRegularExpressionValidator{QRegularExpression{regexp}, parent};
return new TrimmingValidator<QRegularExpressionValidator>{QRegularExpression{regexp}, parent};
} else {
return new EmptyIsAcceptableValidator<QRegularExpressionValidator>{QRegularExpression{regexp}, parent};
return new TrimmingValidator<EmptyIsAcceptableValidator<QRegularExpressionValidator>>{QRegularExpression{regexp}, parent};
}
}
......@@ -69,9 +83,9 @@ QValidator *regularExpressionValidator(Validation::Flags flags, const QString &r
QValidator *Validation::email(Flags flags, QObject *parent)
{
if (flags & Required) {
return new EMailValidator{parent};
return new TrimmingValidator<EMailValidator>{parent};
} else {
return new EmptyIsAcceptableValidator<EMailValidator>{parent};
return new TrimmingValidator<EmptyIsAcceptableValidator<EMailValidator>>{parent};
}
}
......@@ -83,12 +97,8 @@ QValidator *Validation::email(const QString &addRX, Flags flags, QObject *parent
QValidator *Validation::pgpName(Flags flags, QObject *parent)
{
// this regular expression is modeled after gnupg/g10/keygen.c:ask_user_id:
static const auto name_rx = QRegularExpression{QStringLiteral("[^0-9<>][^<>@]{4,}")};
if (flags & Required) {
return new QRegularExpressionValidator{name_rx, parent};
} else {
return new EmptyIsAcceptableValidator<QRegularExpressionValidator>{name_rx, parent};
}
static const QString name_rx{QLatin1String{"[^0-9<>][^<>@]{4,}"}};
return regularExpressionValidator(flags, name_rx, parent);
}
QValidator *Validation::pgpName(const QString &addRX, Flags flags, QObject *parent)
......@@ -99,12 +109,8 @@ QValidator *Validation::pgpName(const QString &addRX, Flags flags, QObject *pare
QValidator *Validation::pgpComment(Flags flags, QObject *parent)
{
// this regular expression is modeled after gnupg/g10/keygen.c:ask_user_id:
static const auto comment_rx = QRegularExpression{QStringLiteral("[^()]*")};
if (flags & Required) {
return new QRegularExpressionValidator{comment_rx, parent};
} else {
return new EmptyIsAcceptableValidator<QRegularExpressionValidator>{comment_rx, parent};
}
static const QString comment_rx{QLatin1String{"[^()]*"}};
return regularExpressionValidator(flags, comment_rx, parent);
}
QValidator *Validation::pgpComment(const QString &addRX, Flags flags, QObject *parent)
......
Supports Markdown
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