Skip to content

Commit

Permalink
Merge pull request #7698 from fredrik-eriksson/netrcfix
Browse files Browse the repository at this point in the history
Fix .netrc parsing
  • Loading branch information
mgallien authored Jan 3, 2025
2 parents 6b4fb46 + 294bb1a commit 1e91c0e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
25 changes: 14 additions & 11 deletions src/cmd/netrcparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@

#include <QDir>
#include <QFile>
#include <QTextStream>
#include <QStringTokenizer>
#include <QRegularExpression>

#include <QDebug>

Expand Down Expand Up @@ -57,33 +56,37 @@ bool NetrcParser::parse()
return false;
}
QString content = netrc.readAll();
if (content.isEmpty()) {
return false;
}

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

LoginPair pair;
QString machine;
bool isDefault = false;
for(auto itToken = tokenizer.cbegin(); itToken != tokenizer.cend(); ++itToken) {
const auto key = *itToken;
for(int i=0; i<tokens.count(); i++) {
const auto key = tokens[i];
if (key == defaultKeyword) {
tryAddEntryAndClear(machine, pair, isDefault);
isDefault = true;
continue; // don't read a value
}

if (itToken != tokenizer.cend()) {
i++;
if (i >= tokens.count()) {
qDebug() << "error fetching value for" << key;
return false;
break;
}
auto value = *(++itToken);
auto value = tokens[i];

if (key == machineKeyword) {
tryAddEntryAndClear(machine, pair, isDefault);
machine = value.toString();
machine = value;
} else if (key == loginKeyword) {
pair.first = value.toString();
pair.first = value;
} else if (key == passwordKeyword) {
pair.second = value.toString();
pair.second = value;
} // ignore unsupported tokens
}
tryAddEntryAndClear(machine, pair, isDefault);
Expand Down
3 changes: 1 addition & 2 deletions test/testnetrcparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ private slots:

void testValidNetrc() {
NetrcParser parser(testfileC);
QEXPECT_FAIL("", "test currently broken, eventually will be fixed", Abort);
QVERIFY(parser.parse());
QCOMPARE(parser.find("foo"), qMakePair(QString("bar"), QString("baz")));
QCOMPARE(parser.find("broken"), qMakePair(QString("bar2"), QString()));
QCOMPARE(parser.find("funnysplit"), qMakePair(QString("bar3"), QString("baz3")));
QEXPECT_FAIL("", "Current implementation do not support spaces in username or password", Continue);
QCOMPARE(parser.find("frob"), qMakePair(QString("user with spaces"), QString("space pwd")));
}

Expand All @@ -69,7 +69,6 @@ private slots:

void testValidNetrcWithDefault() {
NetrcParser parser(testfileWithDefaultC);
QEXPECT_FAIL("", "test currently broken, eventually will be fixed", Abort);
QVERIFY(parser.parse());
QCOMPARE(parser.find("foo"), qMakePair(QString("bar"), QString("baz")));
QCOMPARE(parser.find("dontknow"), qMakePair(QString("user"), QString("pass")));
Expand Down

0 comments on commit 1e91c0e

Please sign in to comment.