Skip to content

Commit 049e0d6

Browse files
roubertFrankYFTang
authored andcommitted
ICU-23243 Bugfix: Don't call std::string_view::front() when empty.
Doing this just happens to work in most builds (which is why this bug hasn't been found until now), but it's actually undefined behavior.
1 parent 1a04de8 commit 049e0d6

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

icu4c/source/common/uloc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ ulocimp_getKeywords(std::string_view localeID,
627627
do {
628628
bool duplicate = false;
629629
/* skip leading spaces */
630-
while (localeID.front() == ' ') {
630+
while (!localeID.empty() && localeID.front() == ' ') {
631631
localeID.remove_prefix(1);
632632
}
633633
if (localeID.empty()) { /* handle trailing "; " */

icu4c/source/test/intltest/loctest.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ void LocaleTest::runIndexedTest( int32_t index, UBool exec, const char* &name, c
266266
TESTCASE_AUTO(TestBug13554);
267267
TESTCASE_AUTO(TestBug20410);
268268
TESTCASE_AUTO(TestBug20900);
269+
TESTCASE_AUTO(TestChromiumBug451657601);
269270
TESTCASE_AUTO(TestLocaleCanonicalizationFromFile);
270271
TESTCASE_AUTO(TestKnownCanonicalizedListCorrect);
271272
TESTCASE_AUTO(TestConstructorAcceptsBCP47);
@@ -5849,6 +5850,12 @@ void LocaleTest::TestBug20900() {
58495850
}
58505851
}
58515852

5853+
void LocaleTest::TestChromiumBug451657601() {
5854+
// This used to cause a crash in _LIBCPP_HARDENING_MODE.
5855+
Locale l = Locale("@x=@; ");
5856+
assertEquals("canonicalized", "@x=@", l.getName());
5857+
}
5858+
58525859
U_DEFINE_LOCAL_OPEN_POINTER(LocalStdioFilePointer, FILE, fclose);
58535860
void LocaleTest::TestLocaleCanonicalizationFromFile()
58545861
{

icu4c/source/test/intltest/loctest.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ class LocaleTest: public IntlTest {
126126
void TestBug13554();
127127
void TestBug20410();
128128
void TestBug20900();
129+
void TestChromiumBug451657601();
129130
void TestLocaleCanonicalizationFromFile();
130131
void TestKnownCanonicalizedListCorrect();
131132
void TestConstructorAcceptsBCP47();

0 commit comments

Comments
 (0)