more langtag length limits

Signed-off-by: Caolán McNamara <caolan.mcnamara@collabora.com>
Change-Id: Ie6a84f836be42e4edc16871d8d4dd12a99046d38
Reviewed-on: https://gerrit.collaboraoffice.com/c/online/+/1592
Tested-by: Jenkins CPCI <releng@collaboraoffice.com>
(cherry picked from commit cca41c7cfbfb77777fe8a58be53f6f2ba84638ab)
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/207068
Tested-by: Jenkins
Reviewed-by: Julien Nabet <serval2412@yahoo.fr>
This commit is contained in:
Caolán McNamara
2026-06-21 20:30:25 +02:00
committed by Julien Nabet
parent 297cf4e138
commit afc4a48212
2 changed files with 42 additions and 2 deletions
@@ -19,6 +19,7 @@
#include <i18nlangtag/languagetagicu.hxx> #include <i18nlangtag/languagetagicu.hxx>
#include <rtl/ustring.hxx> #include <rtl/ustring.hxx>
#include <rtl/ustrbuf.hxx>
#include <com/sun/star/lang/Locale.hpp> #include <com/sun/star/lang/Locale.hpp>
#include <com/sun/star/i18n/ScriptType.hpp> #include <com/sun/star/i18n/ScriptType.hpp>
@@ -37,6 +38,8 @@ public:
void testDisplayNames(); void testDisplayNames();
void testLanguagesWithoutHyphenation(); void testLanguagesWithoutHyphenation();
void testLongBcp47ToStackString(); void testLongBcp47ToStackString();
void testLongLanguageToStackString();
void testLongLanguageCountryToStackString();
CPPUNIT_TEST_SUITE(TestLanguageTag); CPPUNIT_TEST_SUITE(TestLanguageTag);
CPPUNIT_TEST(testAllTags); CPPUNIT_TEST(testAllTags);
@@ -44,6 +47,8 @@ public:
CPPUNIT_TEST(testDisplayNames); CPPUNIT_TEST(testDisplayNames);
CPPUNIT_TEST(testLanguagesWithoutHyphenation); CPPUNIT_TEST(testLanguagesWithoutHyphenation);
CPPUNIT_TEST(testLongBcp47ToStackString); CPPUNIT_TEST(testLongBcp47ToStackString);
CPPUNIT_TEST(testLongLanguageToStackString);
CPPUNIT_TEST(testLongLanguageCountryToStackString);
CPPUNIT_TEST_SUITE_END(); CPPUNIT_TEST_SUITE_END();
}; };
@@ -1109,6 +1114,37 @@ void TestLanguageTag::testLongBcp47ToStackString()
CPPUNIT_ASSERT_EQUAL(sal_Int32(0), aBuf.getLength()); CPPUNIT_ASSERT_EQUAL(sal_Int32(0), aBuf.getLength());
} }
OUString makeRepeated(sal_Unicode c, sal_Int32 n)
{
OUStringBuffer aBuf(n);
for (sal_Int32 i = 0; i < n; ++i)
aBuf.append(c);
return aBuf.makeStringAndClear();
}
void TestLanguageTag::testLongLanguageToStackString()
{
// A non-qlt locale with an overlong Language field and no Country
// must not overflow the StackString64 buffer.
lang::Locale aLocale;
aLocale.Language = makeRepeated(u'a', 80);
StackString64 aBuf;
LanguageTag::convertToBcp47(aBuf, aLocale, false);
CPPUNIT_ASSERT_EQUAL(sal_Int32(0), aBuf.getLength());
}
void TestLanguageTag::testLongLanguageCountryToStackString()
{
// A non-qlt locale whose Language + "-" + Country exceeds the
// StackString64 buffer must not overflow.
lang::Locale aLocale;
aLocale.Language = makeRepeated(u'a', 40);
aLocale.Country = makeRepeated(u'B', 40);
StackString64 aBuf;
LanguageTag::convertToBcp47(aBuf, aLocale, false);
CPPUNIT_ASSERT_EQUAL(sal_Int32(0), aBuf.getLength());
}
CPPUNIT_TEST_SUITE_REGISTRATION( TestLanguageTag ); CPPUNIT_TEST_SUITE_REGISTRATION( TestLanguageTag );
} }
@@ -2892,6 +2892,7 @@ static void appendAscii(StackString64& rBuf, const OUString& s)
// static // static
void LanguageTagImpl::convertToBcp47( StackString64& rBuf, const css::lang::Locale& rLocale ) void LanguageTagImpl::convertToBcp47( StackString64& rBuf, const css::lang::Locale& rLocale )
{ {
assert(rBuf.getLength() == 0);
if (rLocale.Language.isEmpty()) if (rLocale.Language.isEmpty())
{ {
// aBcp47 stays empty // aBcp47 stays empty
@@ -2907,8 +2908,11 @@ void LanguageTagImpl::convertToBcp47( StackString64& rBuf, const css::lang::Loca
* now just concatenate language and country. In case we stumbled over * now just concatenate language and country. In case we stumbled over
* variant aware code we'd have to take care of that. */ * variant aware code we'd have to take care of that. */
if (rLocale.Country.isEmpty()) if (rLocale.Country.isEmpty())
appendAscii(rBuf, rLocale.Language); {
else if (rLocale.Language.getLength() < 64)
appendAscii(rBuf, rLocale.Language);
}
else if (rLocale.Language.getLength() + 1 + rLocale.Country.getLength() < 64)
{ {
appendAscii(rBuf, rLocale.Language); appendAscii(rBuf, rLocale.Language);
rBuf.append("-"); rBuf.append("-");