From bcd53e003655887ee1903a1af1ff3a60fa9f0802 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Thu, 12 Oct 2023 16:50:17 +0300 Subject: [PATCH] Validate sheet and layout names before proposing file name. --- ChangeLog.txt | 1 + src/app/puzzle/vpmainwindow.cpp | 36 +++++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index 49dccb56f..daa1abb0e 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -44,6 +44,7 @@ - New option: Translate formula. - Fix filling piece label data. - New piece option Follow grainline. +- Validate sheet and layout names before proposing file name. # Valentina 0.7.52 September 12, 2022 - Fix crash when default locale is ru. diff --git a/src/app/puzzle/vpmainwindow.cpp b/src/app/puzzle/vpmainwindow.cpp index 9bb160288..277b6c105 100644 --- a/src/app/puzzle/vpmainwindow.cpp +++ b/src/app/puzzle/vpmainwindow.cpp @@ -287,6 +287,38 @@ void SetPrinterTiledPageSettings(const QSharedPointer &printer, const } } } + +//--------------------------------------------------------------------------------------------------------------------- +auto IsValidFileName(const QString &fileName) -> bool +{ + if (fileName.isNull() || fileName.isEmpty()) + { + return false; + } + + static QRegularExpression regex(QStringLiteral("[<>:\"/\\\\|?*]")); + QRegularExpressionMatch match = regex.match(fileName); + if (match.hasMatch()) + { + return false; + } + + static QRegularExpression regexReservedNames(QStringLiteral("^(CON|AUX|PRN|NUL|COM[1-9]|LPT[1-9])(\\..*)?$"), + QRegularExpression::CaseInsensitiveOption); + match = regexReservedNames.match(fileName); + if (match.hasMatch()) + { + return false; + } + + // Check the length of the file name (adjust the limit as needed) + if (fileName.length() > 255) + { + return false; + } + + return true; +} } // namespace struct VPExportData @@ -4088,7 +4120,7 @@ void VPMainWindow::on_ExportLayout() } const QString layoutTitle = m_layout->LayoutSettings().GetTitle(); - const QString fileName = layoutTitle.isEmpty() ? QFileInfo(curFile).baseName() : layoutTitle; + const QString fileName = !IsValidFileName(layoutTitle) ? QFileInfo(curFile).baseName() : layoutTitle; DialogSaveManualLayout dialog(sheets.size(), false, fileName, this); @@ -4139,7 +4171,7 @@ void VPMainWindow::on_ExportSheet() const QString sheetTitle = sheet->GetName(); const QString defaultName = not curFile.isEmpty() ? QFileInfo(curFile).baseName() : tr("sheet"); - const QString fileName = sheetTitle.isEmpty() ? defaultName : sheetTitle; + const QString fileName = !IsValidFileName(sheetTitle) ? defaultName : sheetTitle; DialogSaveManualLayout dialog(1, false, fileName, this);