Skip to content

Commit 4eb0678

Browse files
tux3benapetr
authored andcommitted
fix(Message::appendText): QRegularExpression whitespace bug
Unlike QRegExp, when a QRegularExpression starts with \s, it may match whitespace *including newlines*. So when looking for the start of a section, we might match "\n\n == January 2025 ==" instead of " == January 2025 ==" Then the logic that tries to skip past the header, would instead skip past the first \n, which is still _before_ the header. Then we would think the target header is the start of the next section, and we would end up inserting a message just before the section it was intended to be appended to. QRegularExpression is available since Qt 5.0 and is recommended. Let's get rid of the old QRegExp and fix the QRegularExpression usage.
1 parent cee5d7a commit 4eb0678

File tree

1 file changed

+15
-31
lines changed

1 file changed

+15
-31
lines changed

src/huggle_core/message.cpp

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "wikisite.hpp"
2323
#include "wikiutil.hpp"
2424
#include "wikiuser.hpp"
25+
#include <QRegularExpression>
2526
#include <QUrl>
2627

2728
using namespace Huggle;
@@ -205,7 +206,7 @@ void Message::processNextStep()
205206
this->Error = MessageError_ArticleExist;
206207
} else
207208
{
208-
this->Fail(_l("error-unknown-code",ec));
209+
this->Fail(_l("error-unknown-code", ec));
209210
this->Error = MessageError_Unknown;
210211
}
211212
return;
@@ -319,17 +320,15 @@ void Message::processSend()
319320
if (this->SectionKeep)
320321
{
321322
this->Text = this->appendText(this->Text, this->originalUnmodifiedPageText, this->Title);
322-
} else
323-
{
323+
} else {
324324
// original page needs to be included in new value
325325
if (!this->originalUnmodifiedPageText.isEmpty())
326326
this->Text = this->originalUnmodifiedPageText + "\n\n" + this->Text;
327327
}
328328
this->query->Parameters = "title=" + QUrl::toPercentEncoding(User->GetTalk()) + "&summary=" + QUrl::toPercentEncoding(summary)
329329
+ "&text=" + QUrl::toPercentEncoding(this->Text) + parameters
330330
+ "&token=" + QUrl::toPercentEncoding(this->User->GetSite()->GetProjectConfig()->Token_Csrf);
331-
}else
332-
{
331+
} else {
333332
this->query->Parameters = "title=" + QUrl::toPercentEncoding(User->GetTalk()) + "&section=new&sectiontitle="
334333
+ QUrl::toPercentEncoding(this->Title) + "&summary=" + QUrl::toPercentEncoding(summary)
335334
+ "&text=" + QUrl::toPercentEncoding(this->Text) + parameters + "&token="
@@ -357,8 +356,7 @@ void Message::processTalkPageRetrieval()
357356
this->originalUnmodifiedPageText = page->Value;
358357
this->previousTalkPageRetrieved = true;
359358
return;
360-
} else
361-
{
359+
} else {
362360
if (!missing)
363361
{
364362
Huggle::Syslog::HuggleLogs->DebugLog(this->query->Result->Data);
@@ -375,12 +373,10 @@ QString Message::appendText(QString text, QString original_text, QString section
375373
// there is nothing to insert this to
376374
return original_text += "\n\n" + text + "\n\n";
377375
}
378-
#ifdef QT6_BUILD
376+
379377
QRegularExpression regex("\\s*==\\s*" + QRegularExpression::escape(section_name) + "\\s*==");
380-
#else
381-
QRegExp regex("\\s*==\\s*" + QRegExp::escape(section_name) + "\\s*==");
382-
#endif
383-
if (!original_text.contains(regex))
378+
QRegularExpressionMatch regex_match;
379+
if (!original_text.contains(regex, &regex_match))
384380
{
385381
// there is no section to append to
386382
if (!original_text.isEmpty())
@@ -390,32 +386,20 @@ QString Message::appendText(QString text, QString original_text, QString section
390386
original_text += "== " + section_name + " ==\n\n" + text;
391387
return original_text;
392388
}
393-
#ifdef QT6_BUILD
394-
QRegularExpression header("\\s*==.*==\\s*");
395-
#else
396-
QRegExp header("\\s*==.*==\\s*");
397-
#endif
398-
int start_of_desired_section = original_text.lastIndexOf(regex);
389+
399390
// we need to check if there is any other section after this one
391+
int start_of_desired_section = original_text.lastIndexOf(regex) + regex_match.capturedView().length();
400392
QString section = original_text.mid(start_of_desired_section);
401-
if (section.contains("\n"))
402-
{
403-
// cut the header text
404-
int Diff = section.indexOf("\n") + 1;
405-
start_of_desired_section += Diff;
406-
section = section.mid(Diff);
407-
}
393+
QRegularExpression next_header("\\s*==.*==\\s*");
408394
int start_point = start_of_desired_section;
409-
if (section.contains(header))
395+
if (section.contains(next_header))
410396
{
411397
// yes there is some other section, so we need to know where it is, so that we know where current
412398
// section ends (we want to append text to bottom of current section)
413-
start_point += section.indexOf(header);
414-
} else
415-
{
399+
start_point += section.indexOf(next_header);
400+
} else {
416401
start_point += section.length();
417402
}
418403
// write the text exactly after the start point, but leave some space after it
419-
original_text = original_text.insert(start_point, "\n\n" + text + "\n");
420-
return original_text;
404+
return original_text.insert(start_point, "\n\n" + text + "\n");
421405
}

0 commit comments

Comments
 (0)