Skip to content

Commit

Permalink
Sanitize bookmarks saving with percent-encoded strings, fixes gqrx-sd…
Browse files Browse the repository at this point in the history
  • Loading branch information
0penBrain committed Oct 27, 2023
1 parent 848acef commit 8c62788
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/qtgui/bookmarks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <QTextStream>
#include <QString>
#include <QSet>
#include <QUrl>
#include <algorithm>
#include <iostream>
#include "bookmarks.h"
Expand Down Expand Up @@ -71,6 +72,12 @@ void Bookmarks::remove(int index)

bool Bookmarks::load()
{
// define a lambda for loading percent-encoded string
auto loadEncoded = [](const QString &input){
return QUrl::fromPercentEncoding(input.toUtf8());
};


QFile file(m_bookmarksFile);
if (file.open(QIODevice::ReadOnly | QIODevice::Text))
{
Expand All @@ -94,7 +101,7 @@ bool Bookmarks::load()
QStringList strings = line.split(";");
if(strings.count() == 2)
{
TagInfo::sptr info = findOrAddTag(strings[0]);
TagInfo::sptr info = findOrAddTag(loadEncoded(strings[0]));
info->color = QColor(strings[1].trimmed());
}
else
Expand All @@ -117,15 +124,15 @@ bool Bookmarks::load()
{
BookmarkInfo info;
info.frequency = strings[0].toLongLong();
info.name = strings[1].trimmed();
info.name = loadEncoded(strings[1].trimmed());
info.modulation = strings[2].trimmed();
info.bandwidth = strings[3].toInt();
// Multiple Tags may be separated by comma.
QString strTags = strings[4];
QStringList TagList = strTags.split(",");
for(int iTag=0; iTag<TagList.size(); ++iTag)
{
info.tags.append(findOrAddTag(TagList[iTag].trimmed()));
info.tags.append(findOrAddTag(loadEncoded(TagList[iTag].trimmed())));
}

m_BookmarkList.append(info);
Expand All @@ -145,9 +152,13 @@ bool Bookmarks::load()
return false;
}

//FIXME: Commas in names
bool Bookmarks::save()
{
// define a lambda for saving to percent-encoded string
auto saveEncoded = [](const QString &input){
return QUrl::toPercentEncoding(input);
};

QFile file(m_bookmarksFile);
if(file.open(QFile::WriteOnly | QFile::Truncate | QIODevice::Text))
{
Expand All @@ -169,7 +180,7 @@ bool Bookmarks::save()
for (QMap<QString, TagInfo::sptr>::iterator i = usedTags.begin(); i != usedTags.end(); i++)
{
TagInfo::sptr info = *i;
stream << info->name.leftJustified(20) + "; " + info->color.name() << '\n';
stream << saveEncoded(info->name).leftJustified(20) + "; " + info->color.name() << '\n';
}

stream << '\n';
Expand All @@ -184,7 +195,7 @@ bool Bookmarks::save()
{
BookmarkInfo& info = m_BookmarkList[i];
QString line = QString::number(info.frequency).rightJustified(12) +
"; " + info.name.leftJustified(25) + "; " +
"; " + saveEncoded(info.name).leftJustified(25) + "; " +
info.modulation.leftJustified(20)+ "; " +
QString::number(info.bandwidth).rightJustified(10) + "; ";
for(int iTag = 0; iTag<info.tags.size(); ++iTag)
Expand All @@ -194,7 +205,7 @@ bool Bookmarks::save()
{
line.append(",");
}
line.append(tag->name);
line.append(saveEncoded(tag->name));
}

stream << line << '\n';
Expand Down

0 comments on commit 8c62788

Please sign in to comment.