Skip to content

Commit 1e91c0e

Browse files
authored
Merge pull request #7698 from fredrik-eriksson/netrcfix
Fix .netrc parsing
2 parents 6b4fb46 + 294bb1a commit 1e91c0e

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

src/cmd/netrcparser.cpp

+14-11
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414

1515
#include <QDir>
1616
#include <QFile>
17-
#include <QTextStream>
18-
#include <QStringTokenizer>
17+
#include <QRegularExpression>
1918

2019
#include <QDebug>
2120

@@ -57,33 +56,37 @@ bool NetrcParser::parse()
5756
return false;
5857
}
5958
QString content = netrc.readAll();
59+
if (content.isEmpty()) {
60+
return false;
61+
}
6062

61-
auto tokenizer = QStringTokenizer{content, u" \n\t"};
63+
auto tokens = content.split(QRegularExpression("\\s+"));
6264

6365
LoginPair pair;
6466
QString machine;
6567
bool isDefault = false;
66-
for(auto itToken = tokenizer.cbegin(); itToken != tokenizer.cend(); ++itToken) {
67-
const auto key = *itToken;
68+
for(int i=0; i<tokens.count(); i++) {
69+
const auto key = tokens[i];
6870
if (key == defaultKeyword) {
6971
tryAddEntryAndClear(machine, pair, isDefault);
7072
isDefault = true;
7173
continue; // don't read a value
7274
}
7375

74-
if (itToken != tokenizer.cend()) {
76+
i++;
77+
if (i >= tokens.count()) {
7578
qDebug() << "error fetching value for" << key;
76-
return false;
79+
break;
7780
}
78-
auto value = *(++itToken);
81+
auto value = tokens[i];
7982

8083
if (key == machineKeyword) {
8184
tryAddEntryAndClear(machine, pair, isDefault);
82-
machine = value.toString();
85+
machine = value;
8386
} else if (key == loginKeyword) {
84-
pair.first = value.toString();
87+
pair.first = value;
8588
} else if (key == passwordKeyword) {
86-
pair.second = value.toString();
89+
pair.second = value;
8790
} // ignore unsupported tokens
8891
}
8992
tryAddEntryAndClear(machine, pair, isDefault);

test/testnetrcparser.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ private slots:
5353

5454
void testValidNetrc() {
5555
NetrcParser parser(testfileC);
56-
QEXPECT_FAIL("", "test currently broken, eventually will be fixed", Abort);
5756
QVERIFY(parser.parse());
5857
QCOMPARE(parser.find("foo"), qMakePair(QString("bar"), QString("baz")));
5958
QCOMPARE(parser.find("broken"), qMakePair(QString("bar2"), QString()));
6059
QCOMPARE(parser.find("funnysplit"), qMakePair(QString("bar3"), QString("baz3")));
60+
QEXPECT_FAIL("", "Current implementation do not support spaces in username or password", Continue);
6161
QCOMPARE(parser.find("frob"), qMakePair(QString("user with spaces"), QString("space pwd")));
6262
}
6363

@@ -69,7 +69,6 @@ private slots:
6969

7070
void testValidNetrcWithDefault() {
7171
NetrcParser parser(testfileWithDefaultC);
72-
QEXPECT_FAIL("", "test currently broken, eventually will be fixed", Abort);
7372
QVERIFY(parser.parse());
7473
QCOMPARE(parser.find("foo"), qMakePair(QString("bar"), QString("baz")));
7574
QCOMPARE(parser.find("dontknow"), qMakePair(QString("user"), QString("pass")));

0 commit comments

Comments
 (0)