From 78cc324e8c9c548ca4015d9d00b0e516f2f04f3d Mon Sep 17 00:00:00 2001 From: AT Date: Sun, 28 Jul 2024 11:36:16 -0400 Subject: [PATCH] Don't compare non-numeric parts of the version string. (#2762) Signed-off-by: Adam Treat --- gpt4all-chat/download.cpp | 21 ++++++++++++++------- gpt4all-chat/modellist.cpp | 15 +++++++++++---- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/gpt4all-chat/download.cpp b/gpt4all-chat/download.cpp index a65654f5..399e6e7b 100644 --- a/gpt4all-chat/download.cpp +++ b/gpt4all-chat/download.cpp @@ -62,17 +62,24 @@ static bool operator==(const ReleaseInfo& lhs, const ReleaseInfo& rhs) static bool compareVersions(const QString &a, const QString &b) { + QRegularExpression regex("(\\d+)"); QStringList aParts = a.split('.'); QStringList bParts = b.split('.'); + Q_ASSERT(aParts.size() == 3); + Q_ASSERT(bParts.size() == 3); for (int i = 0; i < std::min(aParts.size(), bParts.size()); ++i) { - int aInt = aParts[i].toInt(); - int bInt = bParts[i].toInt(); - - if (aInt > bInt) { - return true; - } else if (aInt < bInt) { - return false; + QRegularExpressionMatch aMatch = regex.match(aParts[i]); + QRegularExpressionMatch bMatch = regex.match(bParts[i]); + Q_ASSERT(aMatch.hasMatch() && bMatch.hasMatch()); + if (aMatch.hasMatch() && bMatch.hasMatch()) { + int aInt = aMatch.captured(1).toInt(); + int bInt = bMatch.captured(1).toInt(); + if (aInt > bInt) { + return true; + } else if (aInt < bInt) { + return false; + } } } diff --git a/gpt4all-chat/modellist.cpp b/gpt4all-chat/modellist.cpp index e59d7235..7d2d349a 100644 --- a/gpt4all-chat/modellist.cpp +++ b/gpt4all-chat/modellist.cpp @@ -1444,14 +1444,21 @@ void ModelList::updateDataForSettings() static std::strong_ordering compareVersions(const QString &a, const QString &b) { + QRegularExpression regex("(\\d+)"); QStringList aParts = a.split('.'); QStringList bParts = b.split('.'); + Q_ASSERT(aParts.size() == 3); + Q_ASSERT(bParts.size() == 3); for (int i = 0; i < std::min(aParts.size(), bParts.size()); ++i) { - int aInt = aParts[i].toInt(); - int bInt = bParts[i].toInt(); - if (auto diff = aInt <=> bInt; diff != 0) { - return diff; + QRegularExpressionMatch aMatch = regex.match(aParts[i]); + QRegularExpressionMatch bMatch = regex.match(bParts[i]); + Q_ASSERT(aMatch.hasMatch() && bMatch.hasMatch()); + if (aMatch.hasMatch() && bMatch.hasMatch()) { + int aInt = aMatch.captured(1).toInt(); + int bInt = bMatch.captured(1).toInt(); + if (auto diff = aInt <=> bInt; diff != 0) + return diff; } }