Fix bugtracker #248: opening a file while QET is running does nothing

QET::splitWithSpaces() split on QRegularExpression("[^\\]?(?:\\\\)* ").
That is not a valid pattern: "[^\\]" opens a character class whose "\\]" is
an escaped bracket, so the class is never closed. QRegularExpression
reported isValid() == false, QString::split() warned "invalid
QRegularExpression object", and the function returned an EMPTY list for
every input.

It is the receiving half of the SingleApplication handshake: a secondary
instance sends "launched-with-args: " + joinWithSpaces(args) (main.cpp) and
the running instance parses it in QETApp::receiveMessage() before calling
openFiles(). With the split always empty, the running instance received no
arguments at all -- so opening a project while QET was already running
silently did nothing.

The bug is reported against filenames containing spaces, which is how it
was noticed, but it is not limited to them: plain names failed identically.

A corrected regex is not available. The separator is a space preceded by an
even-length run of backslashes, and PCRE2 has no variable-length lookbehind,
so the run cannot be expressed in a lookbehind and anything that matches it
by consumption eats the character before the space -- which is what the
"[^\\]?" was for. Scanning the string explicitly is correct and easier to
read.

tests/qttest/tst_qetstrings.cpp asserts the round trip
splitWithSpaces(joinWithSpaces(x)) == x over plain names, embedded spaces,
embedded backslashes, a trailing backslash and a mixture, plus the specific
regression that a plain argument list does not come back empty.

Verified the test fails without the fix: 9 of 11 cases fail on the old
implementation and all 11 pass with it. Full suite 5/5, Qt 5.15.18.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
ispyisail
2026-09-14 14:55:17 +12:00
parent 3cbb930751
commit fcd2a4e0e0
3 changed files with 100 additions and 5 deletions
+33 -5
View File
@@ -546,12 +546,40 @@ QString QET::joinWithSpaces(const QStringList &string_list) {
QStringList QET::splitWithSpaces(const QString &string) {
// les chaines sont separees par des espaces non echappes
// = avec un nombre nul ou pair de backslashes devant
QStringList escaped_strings = string.split(QRegularExpression("[^\\]?(?:\\\\)* "),Qt::SkipEmptyParts);
//
// This was a QRegularExpression("[^\\]?(?:\\\\)* ") split, which never
// worked: "[^\]" opens a character class whose "\]" is an escaped
// bracket, so the class is never closed and the pattern is invalid.
// QRegularExpression::isValid() was false, QString::split() warned
// "invalid QRegularExpression object" and returned an EMPTY list for
// every input -- so a second instance's file arguments were always
// dropped (bugtracker #248), not just ones containing spaces.
//
// A correct pattern is not expressible here either: the separator is a
// space preceded by an even-length run of backslashes, and PCRE2 has no
// variable-length lookbehind. Scanning explicitly is both correct and
// easier to read than the alternatives.
QStringList returned_list;
foreach(QString escaped_string, escaped_strings) {
returned_list << QET::unescapeSpaces(escaped_string);
QString current;
int backslashes = 0;
for (const QChar &c : string) {
if (c == QLatin1Char('\\')) {
++backslashes;
current += c;
continue;
}
if (c == QLatin1Char(' ') && backslashes % 2 == 0) {
if (!current.isEmpty()) {
returned_list << QET::unescapeSpaces(current);
}
current.clear();
} else {
current += c;
}
backslashes = 0;
}
if (!current.isEmpty()) {
returned_list << QET::unescapeSpaces(current);
}
return(returned_list);
}