Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: simplify file path validation #131

Merged
merged 2 commits into from
Feb 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 10 additions & 25 deletions src/service/filehander.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,8 @@ struct STreePlatInfo {
void serializationTree_helper_1(QDataStream &countStream, int &bzItemCount, int &groupCount,
const CGroupBzItemsTreeInfo &tree, std::function<void(int, int)> f = nullptr)
{
countStream << tree.childGroups.size();
// Note: Use int to ensure serialization consistency (after Qt6, size() uses qsizetype, default 8 bytes)
countStream << int(tree.childGroups.size());

qDebug() << "save group count = " << tree.childGroups.size();

Expand All @@ -387,7 +388,7 @@ void serializationTree_helper_1(QDataStream &countStream, int &bzItemCount, int
serializationTree_helper_1(countStream, bzItemCount, groupCount, p, f);
}

countStream << tree.bzItems.size();
countStream << int(tree.bzItems.size());

qDebug() << "save item count = " << tree.bzItems.size();

Expand Down Expand Up @@ -543,35 +544,19 @@ QStringList FileHander::supDdfStuffix()
static QStringList supDdfSuffixs = QStringList() << "ddf";
return supDdfSuffixs;
}

/**
@brief check input file path is valid, must be a file name and dir exits.
*/
bool FileHander::isLegalFile(const QString &path)
{
if (path.isEmpty()) {
return false;
}

// QRegExp regExp("[:\\*\\?\"<>\\|]");

// if (path.contains(regExp)) {
// return false;
// }

QRegularExpression splitExp("[/\\\\]");

int pos = splitExp.match(path).hasMatch() ? 0 : -1;

while (pos != -1) {
QString dirStr = path.left(pos + 1);
if (dirStr.size() > 1) {
QDir dir(dirStr);
if (!dir.exists()) {
return false;
}
}
pos = splitExp.match(path.mid(pos + 1)).hasMatch() ? pos + 1 : -1;
}

bool isdir = (path.endsWith('/') || path.endsWith('\\'));
return !isdir;
QFileInfo info(path);
QDir dir = info.absoluteDir();
return dir.exists() && !info.isDir();
}

QString FileHander::toLegalFile(const QString &filePath)
Expand Down