QSslCertificate::fromPath fix wildcard handling

The reqExp used to handle wildcards in the path was broken. So we
always searched the working directory and not the specified path.
Autotest where passing because of a hack used for Windows paths
where we removed the first two chars in the path string.

This fix will not use nativeSeparators thus removing the Windows hack
and fix the regExp to match wildcard chars.

Task-number: QTBUG-23573
Change-Id: I56fadbb67f25b8ce9c0f17cb6232e0bdb9148b1c
Reviewed-by: Shane Kearns <shane.kearns@accenture.com>
This commit is contained in:
Martin Petersson 2012-06-08 10:39:37 +02:00 committed by Qt by Nokia
parent 41064f8515
commit 6c59cdecee
2 changed files with 34 additions and 23 deletions

View File

@ -843,39 +843,46 @@ QList<QSslCertificate> QSslCertificate::fromPath(const QString &path,
QRegExp::PatternSyntax syntax)
{
// $, (,), *, +, ., ?, [, ,], ^, {, | and }.
// make sure to use the same path separators on Windows and Unix like systems.
QString sourcePath = QDir::fromNativeSeparators(path);
// Find the path without the filename
QString pathPrefix = sourcePath.left(sourcePath.lastIndexOf(QLatin1Char('/')));
// Check if the path contains any special chars
int pos = -1;
if (syntax == QRegExp::Wildcard)
pos = path.indexOf(QRegExp(QLatin1String("[^\\][\\*\\?\\[\\]]")));
pos = pathPrefix.indexOf(QRegExp(QLatin1String("[*?[]")));
else if (syntax != QRegExp::FixedString)
pos = path.indexOf(QRegExp(QLatin1String("[^\\][\\$\\(\\)\\*\\+\\.\\?\\[\\]\\^\\{\\}\\|]")));
QString pathPrefix = path.left(pos); // == path if pos < 0
if (pos != -1)
pos = sourcePath.indexOf(QRegExp(QLatin1String("[\\$\\(\\)\\*\\+\\.\\?\\[\\]\\^\\{\\}\\|]")));
if (pos != -1) {
// there was a special char in the path so cut of the part containing that char.
pathPrefix = pathPrefix.left(pos);
if (pathPrefix.contains(QLatin1Char('/')))
pathPrefix = pathPrefix.left(pathPrefix.lastIndexOf(QLatin1Char('/')));
// Special case - if the prefix ends up being nothing, use "." instead and
// chop off the first two characters from the glob'ed paths.
int startIndex = 0;
if (pathPrefix.trimmed().isEmpty()) {
if(path.startsWith(QLatin1Char('/'))) {
pathPrefix = path.left(path.indexOf(QRegExp(QLatin1String("[\\*\\?\\[]"))));
pathPrefix = path.left(path.lastIndexOf(QLatin1Char('/')));
else
pathPrefix.clear();
} else {
startIndex = 2;
pathPrefix = QLatin1String(".");
}
}
// The path is a file.
if (pos == -1 && QFileInfo(pathPrefix).isFile()) {
QFile file(pathPrefix);
// Check if the path is a file.
if (QFileInfo(sourcePath).isFile()) {
QFile file(sourcePath);
if (file.open(QIODevice::ReadOnly | QIODevice::Text))
return QSslCertificate::fromData(file.readAll(),format);
return QList<QSslCertificate>();
}
}
// Special case - if the prefix ends up being nothing, use "." instead.
int startIndex = 0;
if (pathPrefix.isEmpty()) {
pathPrefix = QLatin1String(".");
startIndex = 2;
}
// The path can be a file or directory.
QList<QSslCertificate> certs;
QRegExp pattern(path, Qt::CaseSensitive, syntax);
QRegExp pattern(sourcePath, Qt::CaseSensitive, syntax);
QDirIterator it(pathPrefix, QDir::Files, QDirIterator::FollowSymlinks | QDirIterator::Subdirectories);
while (it.hasNext()) {
QString filePath = startIndex == 0 ? it.next() : it.next().mid(startIndex);

View File

@ -534,6 +534,10 @@ void tst_QSslCertificate::fromPath_data()
QTest::newRow("\"certificates/*\" regexp pem") << QString("certificates/*") << int(QRegExp::RegExp) << true << 0;
QTest::newRow("\"certificates/*\" regexp der") << QString("certificates/*") << int(QRegExp::RegExp) << false << 0;
QTest::newRow("\"certificates/*\" wildcard pem") << QString("certificates/*") << int(QRegExp::Wildcard) << true << 5;
QTest::newRow("\"certificates/ca*\" wildcard pem") << QString("certificates/ca*") << int(QRegExp::Wildcard) << true << 1;
QTest::newRow("\"certificates/cert*\" wildcard pem") << QString("certificates/cert*") << int(QRegExp::Wildcard) << true << 4;
QTest::newRow("\"certificates/cert-[sure]*\" wildcard pem") << QString("certificates/cert-[sure]*") << int(QRegExp::Wildcard) << true << 3;
QTest::newRow("\"certificates/cert-[not]*\" wildcard pem") << QString("certificates/cert-[not]*") << int(QRegExp::Wildcard) << true << 0;
QTest::newRow("\"certificates/*\" wildcard der") << QString("certificates/*") << int(QRegExp::Wildcard) << false << 0;
QTest::newRow("\"c*/c*.pem\" fixed pem") << QString("c*/c*.pem") << int(QRegExp::FixedString) << true << 0;
QTest::newRow("\"c*/c*.pem\" fixed der") << QString("c*/c*.pem") << int(QRegExp::FixedString) << false << 0;