From 358df462a03f658277d6ce79d7846d3d53d8d05e Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Tue, 4 May 2021 10:46:17 +0200 Subject: [PATCH] Fix assertion on matchingLocales(Abhkazian, Any, Any) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CLDR v39 has no locales for Abkhazian, so the locale_index[] entry for it actually indexes the last entry before the next language up the enum. This has m_language_id less than Abkhazian. Change-Id: If8b88f30476a981b3ee00ff8760a46ede0b7aab7 Reviewed-by: Morten Johan Sørvig --- src/corelib/text/qlocale.cpp | 6 +++--- tests/auto/corelib/text/qlocale/tst_qlocale.cpp | 8 +++++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/corelib/text/qlocale.cpp b/src/corelib/text/qlocale.cpp index 8c0ce2434e..50e2b352f4 100644 --- a/src/corelib/text/qlocale.cpp +++ b/src/corelib/text/qlocale.cpp @@ -2640,15 +2640,15 @@ QList QLocale::matchingLocales(QLocale::Language language, QLocale::Scr result.reserve(locale_data_size); quint16 index = locale_index[language]; - Q_ASSERT(filter.acceptLanguage(locale_data[index].m_language_id)); - do { + // There may be no matches, for some languages (e.g. Abkhazian at CLDR v39). + while (filter.acceptLanguage(locale_data[index].m_language_id)) { const QLocaleId id = locale_data[index].id(); if (filter.acceptScriptTerritory(id)) { result.append(QLocale(*(id.language_id == C ? c_private() : new QLocalePrivate(locale_data + index, index)))); } ++index; - } while (filter.acceptLanguage(locale_data[index].m_language_id)); + } return result; } diff --git a/tests/auto/corelib/text/qlocale/tst_qlocale.cpp b/tests/auto/corelib/text/qlocale/tst_qlocale.cpp index 56eef7ac99..785bfdef44 100644 --- a/tests/auto/corelib/text/qlocale/tst_qlocale.cpp +++ b/tests/auto/corelib/text/qlocale/tst_qlocale.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2020 The Qt Company Ltd. +** Copyright (C) 2021 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the test suite of the Qt Toolkit. @@ -716,6 +716,12 @@ void tst_QLocale::matchingLocales() QVERIFY(!locales.isEmpty()); QVERIFY(!locales.contains(c)); QVERIFY(locales.contains(ru_RU)); + + // Regression check for assertion failure when no locales match: + locales = QLocale::matchingLocales(QLocale::Abkhazian, QLocale::AnyScript, QLocale::AnyTerritory); + // Empty in CLDR v39, but don't require that. + QVERIFY(!locales.contains(c)); + QVERIFY(!locales.contains(ru_RU)); } void tst_QLocale::unixLocaleName_data()